// Append a file to a zip file. The zipFile must have been already opened. fileName is the name that the file // will be given within the zip file. ba is the data that will be in the file. bool ZipFile::AppendFile(QString fileName, const QByteArray& ba, QString comment) { if (!zf) return false; zip_fileinfo zi; memset(&zi, 0, sizeof(zi)); int r = zipOpenNewFileInZip(zf, fileName.toAscii().constData(), &zi, NULL, 0, NULL, 0, comment.toAscii().constData(), Z_DEFLATED, 9); // Compression level (9=max) if (r != ZIP_OK) return false; r = zipWriteInFileInZip(zf, ba.constData(), ba.size()); if (r != ZIP_OK) return false; r = zipCloseFileInZip(zf); if (r != ZIP_OK) return false; return true; }
bool write_ZIP_file(char *file_path) { time_t time_secs; tm *local_time_ptr; zip_fileinfo file_info; // If there is no file open, do nothing. if (top_file_ptr == NULL) return(false); // Open the new file in the ZIP archive. time(&time_secs); local_time_ptr = localtime(&time_secs); file_info.tmz_date.tm_sec = local_time_ptr->tm_sec; file_info.tmz_date.tm_min = local_time_ptr->tm_min; file_info.tmz_date.tm_hour = local_time_ptr->tm_hour; file_info.tmz_date.tm_mday = local_time_ptr->tm_mday; file_info.tmz_date.tm_mon = local_time_ptr->tm_mon; file_info.tmz_date.tm_year = local_time_ptr->tm_year; file_info.dosDate = 0; file_info.internal_fa = 0; file_info.external_fa = 0; zipOpenNewFileInZip(zip_handle, file_path, &file_info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); // Write the top file buffer to the new file. zipWriteInFileInZip(zip_handle, top_file_ptr->file_buffer, top_file_ptr->file_size); zipCloseFileInZip(zip_handle); return(true); }
void FileUtils::addToZip(const char* archivePath, const char* path, const char* content, int length) throw (IOException) { int result = ZIP_OK; int appendMode = fileExists(archivePath) ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE; zipFile archive = zipOpen(archivePath, appendMode); result = zipOpenNewFileInZip(archive, path, 0 /* file attributes */, 0 /* extra field */, 0 /* extra field size */, 0/* global extra field */, 0 /* global extra field size */, 0 /* comment */, Z_DEFLATED /* method */, Z_DEFAULT_COMPRESSION /* level */); if (result != ZIP_OK) { throw IOException("Unable to add new file to zip archive"); } result = zipWriteInFileInZip(archive, content, static_cast<unsigned int>(length)); if (result != ZIP_OK) { throw IOException("Unable to write file data to zip archive"); } result = zipCloseFileInZip(archive); if (result != ZIP_OK) { throw IOException("Unable to close file in zip archive"); } result = zipClose(archive, 0 /* global comment */); if (result != ZIP_OK) { throw IOException("Unable to close zip archive"); } }
Error EditorExportPlatform::_save_zip_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) { String path = p_path.replace_first("res://", ""); ZipData *zd = (ZipData *)p_userdata; zipFile zip = (zipFile)zd->zip; zipOpenNewFileInZip(zip, path.utf8().get_data(), NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); zipWriteInFileInZip(zip, p_data.ptr(), p_data.size()); zipCloseFileInZip(zip); if (zd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false)) { return ERR_SKIP; } return OK; }
/* Trying to write offset and vertices data to file. */ void CPathEstimator::WriteFile(string name) { // We need this directory to exist boost::filesystem::path f("./maps/paths"); if (!boost::filesystem::exists(f)) boost::filesystem::create_directories(f); unsigned int hash = Hash(); char hashString[50]; sprintf(hashString,"%u",hash); string filename = string("maps/paths/") + stupidGlobalMapname.substr(0, stupidGlobalMapname.find('.') + 1) + hashString + "." + name + ".zip"; zipFile file = zipOpen(filename.c_str(), APPEND_STATUS_CREATE); if (file) { zipOpenNewFileInZip(file, "pathinfo", NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); //Write hash. unsigned int hash = Hash(); zipWriteInFileInZip(file, (void*)&hash, 4); //Write block-center-offsets. int blocknr; for(blocknr = 0; blocknr < nbrOfBlocks; blocknr++) { zipWriteInFileInZip(file, (void*)blockState[blocknr].sqrCenter, moveinfo->moveData.size() * sizeof(int2)); //file.write((char*)blockState[blocknr].sqrCenter, moveinfo->moveData.size() * sizeof(int2)); } //Write vertices. zipWriteInFileInZip(file, (void*)vertex, nbrOfVertices * sizeof(float)); //file.write((char*)vertex, nbrOfVertices * sizeof(float)); zipCloseFileInZip(file); zipClose(file, NULL); } }
int DumpMemToPack(struct ExecutionLogging* pEL,const char* filename_to_store,const void* buf,unsigned int size) { zip_fileinfo zi; zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; zi.dosDate = 0; zi.internal_fa = 0; zi.external_fa = 0; zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = 9; zi.tmz_date.tm_year = 2009; zi.tmz_date.tm_mon--; int err; err = zipOpenNewFileInZip(pEL->zf,filename_to_store,&zi,NULL,0,NULL,0,NULL /* param_list */, 0,0); if (err==0) { err = zipWriteInFileInZip (pEL->zf,buf,size); err = zipCloseFileInZip(pEL->zf); } return (err==0) ? 1 : 0; }
void addFileToZip(zipFile z, String filePath, String pathInZip, bool silent) { if(!silent) printf("Packaging %s as %s\n", filePath.c_str(), pathInZip.c_str()); zip_fileinfo zi; zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; zi.dosDate = 0; zi.internal_fa = 0; zi.external_fa = 0; filetime(filePath.c_str(),&zi.tmz_date,&zi.dosDate); zipOpenNewFileInZip(z, pathInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 2); FILE *f = fopen(filePath.c_str(), "rb"); fseek(f, 0, SEEK_END); long fileSize = ftell(f); fseek(f, 0, SEEK_SET); char *buf = (char*) malloc(fileSize); fread(buf, fileSize, 1, f); zipWriteInFileInZip(z, buf, fileSize); free(buf); fclose(f); zipCloseFileInZip(z); }
bool Zipped::zipItem(const MappedFile& item, const wchar_t* basename, int compressionLevel, unsigned long long itemFiletime) { char s[MAX_BASENAME_LENGTH * 3 + 1]; //assume worst-case non-ASCII 3 bytes per character WideCharToMultiByte(CP_UTF8, 0, basename, -1, s, sizeof(s), 0, 0); zip_fileinfo fi; const zip_fileinfo* fip = setFileInfo(&fi, itemFiletime); zipOpenNewFileInZip(zip_, s, fip, 0, 0, 0, 0, 0, Z_DEFLATED, compressionLevel); unsigned int numMaps = item.numMaps(); unsigned int bytesToWrite = (numMaps > 1)? mapSize_: static_cast<unsigned int>(item.size()); unsigned long long remainingBytes = item.size(); for (unsigned int i = 0; i < numMaps;) { const unsigned char* src = item.map(i); if (++i == numMaps) { bytesToWrite = static_cast<unsigned int>(remainingBytes); } zipWriteInFileInZip(zip_, src, bytesToWrite); remainingBytes -= bytesToWrite; } zipCloseFileInZip(zip_); bool ok = true; return ok; }
bool CDialogPackage::AddFileToPackage(const WCHAR* filePath, const WCHAR* zipPath) { std::string zipPathUTF8 = StringUtil::NarrowUTF8(zipPath); for (int i = 0, isize = zipPathUTF8.length(); i < isize; ++i) { if (zipPathUTF8[i] == '\\') { zipPathUTF8[i] = '/'; } } int open = zipOpenNewFileInZip(m_ZipFile, zipPathUTF8.c_str(), NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); if (open != ZIP_OK) { return false; } bool result = true; if (filePath) { HANDLE file = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (file == INVALID_HANDLE_VALUE) { result = false; } else { do { const DWORD bufferSize = 16 * 1024; BYTE buffer[bufferSize]; DWORD readSize; if (!ReadFile(file, buffer, bufferSize, &readSize, NULL)) { result = false; } else if (readSize != 0) { result = zipWriteInFileInZip(m_ZipFile, buffer, (UINT)readSize) == ZIP_OK; } else { // EOF break; } } while (result); CloseHandle(file); } } else { // Directory entry, so nothing needs to be written. } return zipCloseFileInZip(m_ZipFile) == ZIP_OK && result; }
BOOL fsUploadMgr::Zip_AddFile(void *z, LPCSTR pszFile, LPCSTR pszNameInZip) { zipFile zip = (zipFile) z; bool result = false; if (pszNameInZip == NULL) { pszNameInZip = strrchr (pszFile, '\\'); if (pszNameInZip) pszNameInZip++; if (pszNameInZip == NULL) pszNameInZip = pszFile; } zip_fileinfo zfileinfo = {0}; struct _stat file_stat = {0}; FILE* file = fopen (pszFile, "rb"); _fstat (_fileno (file), &file_stat); struct tm* file_time = localtime (&file_stat.st_mtime); tm_zip* zip_time = &zfileinfo.tmz_date; memcpy (zip_time, file_time, sizeof (tm_zip)); const int READ_BUFFER_SIZE = 65535; char read_buf[READ_BUFFER_SIZE]; wchar_t wsz [30000]; MultiByteToWideChar (CP_ACP, 0, pszNameInZip, -1, wsz, 30000); char szNameInZip [30000]; WideCharToMultiByte (CP_OEMCP, 0, wsz, -1, szNameInZip, 30000, NULL, NULL); if (ZIP_OK == zipOpenNewFileInZip (zip, szNameInZip, &zfileinfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, m_pkg.m_iZipCompressMethod)) { while (!feof (file)) { result = false; size_t count = fread (read_buf, sizeof(char), READ_BUFFER_SIZE, file); if (!ferror (file)) { if (ZIP_OK == zipWriteInFileInZip (zip, read_buf, count)) { result = true; continue; } else break; } else break; } result = result && (Z_OK == zipCloseFileInZip(zip)); } result = result && (0 == fclose (file)); return result; }
//----------------------------------------------------------------------------- // CZLib::AddFile // // Adds a file to the zip archive // BOOL CZLib::AddFile(string f_file) { BOOL bReturn = FALSE; // Open file being added HANDLE hFile = NULL; hFile = CreateFile(f_file.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile) { // Get file creation date FILETIME ft = CUtility::getLastWriteFileTime(f_file); zip_fileinfo zi = {0}; FileTimeToDosDateTime( &ft, // last write FILETIME ((LPWORD)&zi.dosDate)+1, // dos date ((LPWORD)&zi.dosDate)+0); // dos time // Trim path off file name string sFileName = f_file.substr(f_file.find_last_of(_T('\\')) + 1); // Start a new file in Zip if (ZIP_OK == zipOpenNewFileInZip(m_zf, sFileName.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION)) { // Write file to Zip in 4 KB chunks const DWORD BUFFSIZE = 4096; TCHAR buffer[BUFFSIZE] = _T(""); DWORD dwBytesRead = 0; while (ReadFile(hFile, &buffer, BUFFSIZE, &dwBytesRead, NULL) && dwBytesRead) { if (ZIP_OK == zipWriteInFileInZip(m_zf, buffer, dwBytesRead) && dwBytesRead < BUFFSIZE) { // Success bReturn = TRUE; } } bReturn &= (ZIP_OK == zipCloseFileInZip(m_zf)); } bReturn &= CloseHandle(hFile); } return bReturn; }
//by stone bool FileSystemZip::Init_zMemory(std::string NameInZip, unsigned char* buf, int size, int append) { unzFile zf; zip_fileinfo zi; time_t tm_t; struct tm* filedate; int opt_compress_level = Z_BEST_COMPRESSION; //use best compress if( m_compress_buf ) { delete m_compress_buf; m_compress_buf = NULL; } m_compress_buf = new byte[size]; zf = ZipOpen_Memory(m_compress_buf, size, append); if (!zf) { LogError("Cannot create memory using"); return false; } LogMsg("deflate zip as memory"); memset(&zi,0,sizeof(zip_fileinfo)); tm_t = time(NULL); filedate = localtime(&tm_t); zi.tmz_date.tm_sec = filedate->tm_sec; zi.tmz_date.tm_min = filedate->tm_min; zi.tmz_date.tm_hour = filedate->tm_hour; zi.tmz_date.tm_mday = filedate->tm_mday; zi.tmz_date.tm_mon = filedate->tm_mon ; zi.tmz_date.tm_year = filedate->tm_year; zipOpenNewFileInZip(zf, NameInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, opt_compress_level); zipWriteInFileInZip(zf,buf,size); zipCloseFileInZip(zf); m_zip_size = zipClose(zf, NULL); //m_zipFileName = zipFileName; //save it in case we need to spawn more zip readers for streaming operations //CacheIndex(); return true; //success }
int hts_extract_meta(const char* path) { char catbuff[CATBUFF_SIZE]; unzFile zFile = unzOpen(fconcat(catbuff,path,"hts-cache/new.zip")); zipFile zFileOut = zipOpen(fconcat(catbuff,path,"hts-cache/meta.zip"), 0); if (zFile != NULL && zFileOut != NULL) { if (unzGoToFirstFile(zFile) == Z_OK) { zip_fileinfo fi; unz_file_info ufi; char BIGSTK filename[HTS_URLMAXSIZE * 4]; char BIGSTK comment[8192]; memset(comment, 0, sizeof(comment)); // for truncated reads memset(&fi, 0, sizeof(fi)); memset(&ufi, 0, sizeof(ufi)); do { int readSizeHeader; filename[0] = '\0'; comment[0] = '\0'; if (unzOpenCurrentFile(zFile) == Z_OK) { if ( (readSizeHeader = unzGetLocalExtrafield(zFile, comment, sizeof(comment) - 2)) > 0 && unzGetCurrentFileInfo(zFile, &ufi, filename, sizeof(filename) - 2, NULL, 0, NULL, 0) == Z_OK ) { comment[readSizeHeader] = '\0'; fi.dosDate = ufi.dosDate; fi.internal_fa = ufi.internal_fa; fi.external_fa = ufi.external_fa; if (zipOpenNewFileInZip(zFileOut, filename, &fi, NULL, 0, NULL, 0, NULL, /* comment */ Z_DEFLATED, Z_DEFAULT_COMPRESSION) == Z_OK) { if (zipWriteInFileInZip(zFileOut, comment, (int) strlen(comment)) != Z_OK) { } if (zipCloseFileInZip(zFileOut) != Z_OK) { } } } unzCloseCurrentFile(zFile); } } while( unzGoToNextFile(zFile) == Z_OK ); } zipClose(zFileOut, "Meta-data extracted by HTTrack/"HTTRACK_VERSION); unzClose(zFile); return 1; } return 0; }
static int writezip_cb(lua_State* L) { const char* zipname = luaL_checkstring(L, 1); luaL_checktype(L, 2, LUA_TTABLE); int result = 0; zipFile zf = zipOpen(zipname, APPEND_STATUS_CREATE); if (zf) { result = 1; lua_pushnil(L); while (lua_next(L, 2) != 0) { const char* key = lua_tostring(L, -2); size_t valuelen; const char* value = lua_tolstring(L, -1, &valuelen); int i = zipOpenNewFileInZip(zf, key, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); if (i != ZIP_OK) { result = 0; break; } i = zipWriteInFileInZip(zf, value, valuelen); if (i != ZIP_OK) { result = 0; break; } i = zipCloseFileInZip(zf); if (i != ZIP_OK) { result = 0; break; } lua_pop(L, 1); /* leave key on stack */ } zipClose(zf, NULL); } if (!result) return 0; lua_pushboolean(L, true); return 1; }
void ZipArchiveImpl::AddFolder(const bfs::path& path, const bfs::path& archive_path) { ++m_Info.folderCount; zip_fileinfo fileinfo; fileinfo.internal_fa = 0; #ifdef _WIN32 fileinfo.external_fa = ::GetFileAttributesW(path.native().c_str()); #else { struct stat path_stat; if (::stat(path.native().c_str(), &path_stat) == 0) { fileinfo.external_fa = path_stat.st_mode; } } #endif fileinfo.dosDate = 0; // Read the time from the filesystem and convert it auto fsTime = bfs::last_write_time(path); auto posixTime = boost::posix_time::from_time_t(fsTime); auto tm = boost::posix_time::to_tm(posixTime); /* TODO: this is how to get the time for a physfs file boost::posix_time::ptime posixTime; auto milisPastEpoc = PHYSFS_getLastModTime(path.generic_string().c_str()); if (milisPastEpoc >= 0) time = boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1), boost::posix_time::milliseconds(milisPastEpoc)); else time = boost::posix_time::second_clock::local_time(); */ fileinfo.tmz_date.tm_hour = tm.tm_hour; fileinfo.tmz_date.tm_min = tm.tm_min; fileinfo.tmz_date.tm_sec = tm.tm_sec; fileinfo.tmz_date.tm_year = tm.tm_year; fileinfo.tmz_date.tm_mon = tm.tm_mon; fileinfo.tmz_date.tm_mday = tm.tm_mday; auto r = zipOpenNewFileInZip(m_File, (archive_path.generic_string() + "/").c_str(), &fileinfo, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_BEST_SPEED); zipCloseFileInZip(m_File); for (bfs::directory_iterator it(path), end = bfs::directory_iterator(); it != end; ++it) { AddPath(it->path(), archive_path / it->path().filename()); } }
ZipStreamWriter::ZipStreamWriter(zipFile zip, const char* file) { _zip = NULL; if (zip) { if (zipOpenNewFileInZip(zip, file, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION) == ZIP_OK) { _zip = zip; } } }
static int save_archive(char *filename, char *buffer, int size) { zipFile fd = NULL; int ret = 0; fd=zipOpen(filename,0); if(!fd) { printf("Failed to create zip\r\n"); return (0); } ret=zipOpenNewFileInZip(fd,"SNAPSHOT", NULL, NULL,0, NULL,0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); if(ret != ZIP_OK) { zipClose(fd,NULL); printf("Failed to create file in zip\r\n"); return (0); } ret=zipWriteInFileInZip(fd,buffer,size); if(ret != ZIP_OK) { zipCloseFileInZip(fd); zipClose(fd,NULL); printf("Failed to write file in zip\r\n"); return (0); } ret=zipCloseFileInZip(fd); if(ret != ZIP_OK) { zipClose(fd,NULL); printf("Failed to close file in zip\r\n"); return (0); } ret=zipClose(fd,NULL); if(ret != ZIP_OK) { printf("Failed to close zip\r\n"); return (0); } return(1); }
bool CreateZipFromPathDeflatedAndDefaultCompression(string const & filePath, string const & zipFilePath) { // 2. Open zip file for writing. MY_SCOPE_GUARD(outFileGuard, bind(&my::DeleteFileX, cref(zipFilePath))); ZipHandle zip(zipFilePath); if (!zip.Handle()) return false; zip_fileinfo zipInfo = {}; CreateTMZip(zipInfo.tmz_date); string fileName = filePath; my::GetNameFromFullPath(fileName); if (!strings::IsASCIIString(fileName)) fileName = "MapsMe.kml"; if (zipOpenNewFileInZip(zip.Handle(), fileName.c_str(), &zipInfo, NULL, 0, NULL, 0, "ZIP from MapsWithMe", Z_DEFLATED, Z_DEFAULT_COMPRESSION) < 0) { return false; } // Write source file into zip file. try { my::FileData file(filePath, my::FileData::OP_READ); uint64_t const fileSize = file.Size(); uint64_t currSize = 0; char buffer[ZIP_FILE_BUFFER_SIZE]; while (currSize < fileSize) { unsigned int const toRead = min(ZIP_FILE_BUFFER_SIZE, static_cast<unsigned int>(fileSize - currSize)); file.Read(currSize, buffer, toRead); if (ZIP_OK != zipWriteInFileInZip(zip.Handle(), buffer, toRead)) return false; currSize += toRead; } } catch (Reader::Exception const & ex) { LOG(LERROR, ("Error reading file:", filePath, ex.Msg())); return false; } // Success. outFileGuard.release(); return true; }
static bool CreateZip(const std::string &path, const std::vector<std::string> &files, const std::string &pb2BasePath) { std::ifstream input; std::vector<char> buffer; bool ok = true; zipFile zf = zipOpen(path.c_str(), APPEND_STATUS_CREATE); if (zf == NULL) return false; for (size_t i = 0; i < files.size(); i++) { input.open(pb2BasePath + files[i], std::ios::binary | std::ios::ate); if (!input.is_open()) { ok = false; input.close(); break; } std::streamoff size = input.tellg(); buffer.resize(static_cast<unsigned int>(size)); input.seekg(0, std::ios::beg); input.read((char*)&buffer[0], size); input.close(); zip_fileinfo zfi = { 0 }; if (S_OK == zipOpenNewFileInZip(zf, files[i].c_str(), &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION)) { if (0 != zipWriteInFileInZip(zf, &buffer[0], buffer.size())) ok = false; if (0 != zipCloseFileInZip(zf)) ok = false; } else { ok = false; } } if (0 != zipClose(zf, NULL)) { return false; } else { return ok; } }
/** * Try to write offset and vertex data to file. */ void CPathEstimator::WriteFile(const std::string& cacheFileName, const std::string& map) { // We need this directory to exist if (!filesystem.CreateDirectory(pathDir)) return; const unsigned int hash = Hash(); char hashString[64] = {0}; sprintf(hashString, "%u", hash); const std::string filename = std::string(pathDir) + map + hashString + "." + cacheFileName + ".zip"; zipFile file; // open file for writing in a suitable location file = zipOpen(filesystem.LocateFile(filename, FileSystem::WRITE).c_str(), APPEND_STATUS_CREATE); if (file) { zipOpenNewFileInZip(file, "pathinfo", NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); // Write hash. zipWriteInFileInZip(file, (void*) &hash, 4); // Write block-center-offsets. for (int blocknr = 0; blocknr < blockStates.GetSize(); blocknr++) zipWriteInFileInZip(file, (void*) &blockStates[blocknr].nodeOffsets[0], moveinfo->moveData.size() * sizeof(int2)); // Write vertices. zipWriteInFileInZip(file, &vertices[0], vertices.size() * sizeof(float)); zipCloseFileInZip(file); zipClose(file, NULL); // get the CRC over the written path data CArchiveZip* pfile = new CArchiveZip(filesystem.LocateFile(filename)); if (!pfile || !pfile->IsOpen()) { delete pfile; return; } std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); const unsigned fid = file.FindFile("pathinfo"); assert(fid < file.NumFiles()); pathChecksum = file.GetCrc32(fid); } }
int zipAppendFile(const char* fileName, void* buffer, int size) { zip_fileinfo zi; int err; memset(&zi, 0, sizeof(zi)); err = zipOpenNewFileInZip(g_zipWrite->zip, fileName, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); if (err == ZIP_OK) { err = zipWriteInFileInZip(g_zipWrite->zip, buffer, size); } return err >= 0; }
bool MkInterfaceForZipFileWriting::Write(const MkPathName& filePath, const MkByteArray& srcArray) { if (m_ZipFile == NULL) return false; std::string charPath; filePath.ExportMultiByteString(charPath); if (zipOpenNewFileInZip(m_ZipFile, charPath.c_str(), NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK) return false; if (zipWriteInFileInZip(m_ZipFile, srcArray.GetPtr(), srcArray.GetSize()) < 0) return false; zipCloseFileInZip(m_ZipFile); return true; }
/* * try to write offset and vertex data to file */ void CPathEstimator::WriteFile(std::string name) { // We need this directory to exist if (!filesystem.CreateDirectory("maps/paths")) return; unsigned int hash = Hash(); char hashString[50]; sprintf(hashString,"%u",hash); std::string filename = std::string("maps/paths/") + stupidGlobalMapname.substr(0, stupidGlobalMapname.find_last_of('.') + 1) + hashString + "." + name + ".zip"; zipFile file; // open file for writing in a suitable location file = zipOpen(filesystem.LocateFile(filename, FileSystem::WRITE).c_str(), APPEND_STATUS_CREATE); if (file) { zipOpenNewFileInZip(file, "pathinfo", NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); // Write hash. unsigned int hash = Hash(); zipWriteInFileInZip(file, (void*) &hash, 4); // Write block-center-offsets. for (int blocknr = 0; blocknr < nbrOfBlocks; blocknr++) { zipWriteInFileInZip(file, (void*) blockState[blocknr].sqrCenter, moveinfo->moveData.size() * sizeof(int2)); } // Write vertices. zipWriteInFileInZip(file, (void*) vertex, nbrOfVertices * sizeof(float)); zipCloseFileInZip(file); zipClose(file, NULL); // get the CRC over the written path data CArchiveZip* pfile = SAFE_NEW CArchiveZip(filesystem.LocateFile(filename)); if (!pfile || !pfile->IsOpen()) { delete pfile; return; } std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); pathChecksum = file.GetCrc32("pathinfo"); } }
int zopen(const char *filename) { zip_cached_len = 0; if (zip_mode == ZIP_READOPEN) { if (!zipfile) { FILE *fp; strcpy(basedirend, filename); if ((fp = fopen(basedir, "rb")) != NULL) { zip_fd = (int)fp; return zip_fd; } return -1; } if (unzLocateFile(zipfile, filename, 0) == UNZ_OK) if (unzOpenCurrentFile(zipfile) == UNZ_OK) return (int)zipfile; } else { zip_fileinfo zip_info; struct tm *nowtime; time_t ltime; memset(&zip_info, 0, sizeof(zip_info)); time(<ime); nowtime = localtime(<ime); zip_info.tmz_date.tm_sec = nowtime->tm_sec; zip_info.tmz_date.tm_min = nowtime->tm_min; zip_info.tmz_date.tm_hour = nowtime->tm_hour; zip_info.tmz_date.tm_mday = nowtime->tm_mday; zip_info.tmz_date.tm_mon = nowtime->tm_mon ; zip_info.tmz_date.tm_year = nowtime->tm_year; if (zipOpenNewFileInZip(zipfile, filename, &zip_info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9) == ZIP_OK) return (int)zipfile; } return -1; }
dmz::Boolean dmz::WriterZip::open_file (const String &FileName) { Boolean result (False); if (_state.zf) { close_file (); zip_fileinfo zinfo; zinfo.tmz_date.tm_sec = zinfo.tmz_date.tm_min = zinfo.tmz_date.tm_hour = zinfo.tmz_date.tm_mday = zinfo.tmz_date.tm_min = zinfo.tmz_date.tm_year = 0; zinfo.dosDate = 0; zinfo.internal_fa = 0; zinfo.external_fa = 0; time_t tm = time (0); struct tm *tms = localtime (&tm); if (tms) { zinfo.tmz_date.tm_sec = tms->tm_sec; zinfo.tmz_date.tm_min = tms->tm_min; zinfo.tmz_date.tm_hour = tms->tm_hour; zinfo.tmz_date.tm_mday = tms->tm_mday; zinfo.tmz_date.tm_mon = tms->tm_mon; zinfo.tmz_date.tm_year = tms->tm_year + 1900; } result = _state.zip_error (zipOpenNewFileInZip ( _state.zf, FileName.get_buffer (), &zinfo, 0, 0, 0, 0, 0, // No comments Z_DEFLATED, _state.level)); if (result) { _state.fileName = FileName; } } else { _state.error.flush () << "Zip archive not open."; } return result; }
bool nuiZipWriter::AddFile(nglIStream* pStream, const nglString& rPathInZip, const nglString& rComment, bool OwnStream) { NGL_ASSERT(IsValid()); zip_fileinfo info; nglTime tm; nglTimeInfo t; tm.GetLocalTime(t); info.tmz_date.tm_sec = t.Seconds; info.tmz_date.tm_min = t.Minutes; info.tmz_date.tm_hour = t.Hours; info.tmz_date.tm_mday = t.Day; info.tmz_date.tm_mon = t.Month; info.tmz_date.tm_year = t.Year; info.dosDate = 0; info.internal_fa = 0; info.external_fa = 0; bool res = zipOpenNewFileInZip(mpZip, rPathInZip.IsNull() ? NULL : rPathInZip.GetStdString().c_str(), &info, NULL, 0, NULL, 0, rComment.IsNull() ? NULL : rComment.GetStdString().c_str(), 0, 0) == Z_OK; if (!res) return res; const uint32 bufsize = 4096; uint8 buf[bufsize]; uint64 todo = pStream->Available(); uint32 _read = -1; while (todo && _read) { uint32 toread = MIN(todo, bufsize); _read = pStream->Read(buf, toread, 1); res = zipWriteInFileInZip(mpZip, buf, _read) == Z_OK; todo -= _read; if (_read != toread) return false; } res = zipCloseFileInZip(mpZip) == Z_OK; if (OwnStream) delete pStream; return res; }
// ---------------------------------------------------------------------------- // Compress // ---------------------------------------------------------------------------- bool archive_Compress(std::string zipFilename, std::string filename, const byte* data, uint size) { if(zipFilename.empty( ) || zipFilename.size( ) == 0) { logger_LogError("Zip filename is invalid.", ARCHIVE_SOURCE); return false; } if(filename.empty( ) || filename.size( ) == 0) { logger_LogError("Filename is invalid.", ARCHIVE_SOURCE); return false; } if(data == NULL) { logger_LogError("Data parameter is invalid.", ARCHIVE_SOURCE); return false; } zipFile file = zipOpen(zipFilename.c_str( ), APPEND_STATUS_CREATE); if(file == NULL) { logger_LogInfo("Failed to create the zip file " + zipFilename + ".", ARCHIVE_SOURCE); return false; } zip_fileinfo fileInfo = {0}; fileInfo.dosDate = 1; int result = zipOpenNewFileInZip(file, filename.c_str( ), &fileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); if(result != ZIP_OK) { logger_LogInfo("Failed to open a new file within the zip file " + filename + ".", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); zipClose(file, "Failed to compress."); return false; } result = zipWriteInFileInZip(file, data, size); if(result != ZIP_OK) { logger_LogInfo("Failed to write data to the zip file " + filename + ".", ARCHIVE_SOURCE); zipCloseFileInZip(file); zipClose(file, "Failed to compress."); return false; } zipCloseFileInZip(file); zipClose(file, "Comment"); return true; }
int oneZipFile(zipFile & zf, zip_fileinfo & zi, std::wstring & file_name, std::wstring & zip_file_name, int method, int compressionLevel) { int err = -1; NSFile::CFileBinary oFile; if(oFile.OpenFile(file_name)) { DWORD dwSizeRead; BYTE* pData = new BYTE[oFile.GetFileSize()]; if(oFile.ReadFile(pData, oFile.GetFileSize(), dwSizeRead)) { std::string zipFileNameA = codepage_issue_fixToOEM(zip_file_name); err = zipOpenNewFileInZip( zf, zipFileNameA.c_str(), &zi, NULL, 0, NULL, 0, NULL, method, compressionLevel ); err = zipWriteInFileInZip( zf, pData, dwSizeRead ); err = zipCloseFileInZip( zf ); } RELEASEARRAYOBJECTS(pData); } return 0; }
void ZipFile::addFile(const openstudio::path &t_localPath, const openstudio::path &t_destinationPath) { if (zipOpenNewFileInZip(m_zipFile, openstudio::toString(t_destinationPath).c_str(), 0, 0, 0, 0, 0, 0, Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK) { throw std::runtime_error("Unable to create new file in archive: " + openstudio::toString(t_destinationPath)); } try { std::ifstream ifs(openstudio::toString(t_localPath).c_str(), std::ios_base::in | std::ios_base::binary); if (!ifs.is_open() || ifs.fail()) { throw std::runtime_error("Unable to open local file: " + openstudio::toString(t_localPath)); } while (!ifs.eof()) { std::vector<char> buffer(1024); ifs.read(&buffer.front(), buffer.size()); std::streamsize bytesread = ifs.gcount(); if (ifs.fail() && !ifs.eof()) { throw std::runtime_error("Error reading from local file: " + openstudio::toString(t_localPath)); } zipWriteInFileInZip(m_zipFile, &buffer.front(), static_cast<unsigned int>(bytesread)); } } catch (...) { zipCloseFileInZip(m_zipFile); throw; } zipCloseFileInZip(m_zipFile); }
// Static. MgByteReader * WriteKmz(const std::string& kml) { zlib_filefunc_def fdef; fill_memory_filefunc(&fdef); char buff[256]; int outsize = kml.length() + 256;; char* outbuff = (char*)malloc(outsize); ourmemory_t *mem = (ourmemory_t *)malloc(sizeof(*mem)); mem->base = outbuff; mem->size = outsize; mem->limit = 0; mem->cur_offset = 0; sprintf(buff,"%p",mem); zipFile zipfile_ = zipOpen2(buff, 0,0,&fdef); if (!zipfile_) { return false; } zipOpenNewFileInZip(zipfile_, "doc.kml", 0, 0, 0, 0, 0, 0, Z_DEFLATED, Z_DEFAULT_COMPRESSION); //zipWriteInFileInZip(zipfile_, (void* const)kml.data(), zipWriteInFileInZip(zipfile_, static_cast<const void*>(kml.data()), static_cast<unsigned int>(kml.size())); zipCloseFileInZip(zipfile_); zipClose(zipfile_, 0); MgByteReader *breader = new MgByteReader((unsigned char*)outbuff,mem->limit,MgMimeType::Kmz); free(outbuff); free(mem); return breader; }