ErrorCode MinizipUtils::add_file(zipFile zf, const std::string &name, const std::vector<unsigned char> &contents) { // Obviously never true, but we'll keep it here just in case bool zip64 = static_cast<uint64_t>(contents.size()) >= ((1ull << 32) - 1); zip_fileinfo zi; memset(&zi, 0, sizeof(zi)); int ret = zipOpenNewFileInZip2_64( zf, // file name.c_str(), // filename &zi, // zip_fileinfo nullptr, // extrafield_local 0, // size_extrafield_local nullptr, // extrafield_global 0, // size_extrafield_global nullptr, // comment Z_DEFLATED, // method Z_DEFAULT_COMPRESSION, // level 0, // raw zip64 // zip64 ); if (ret != ZIP_OK) { LOGE("minizip: Failed to open inner file: %s", zip_error_string(ret).c_str()); return ErrorCode::ArchiveWriteDataError; } // Write data to file ret = zipWriteInFileInZip(zf, contents.data(), static_cast<uint32_t>(contents.size())); if (ret != ZIP_OK) { LOGE("minizip: Failed to write inner file data: %s", zip_error_string(ret).c_str()); zipCloseFileInZip(zf); return ErrorCode::ArchiveWriteDataError; } ret = zipCloseFileInZip(zf); if (ret != ZIP_OK) { LOGE("minizip: Failed to close inner file: %s", zip_error_string(ret).c_str()); return ErrorCode::ArchiveWriteDataError; } return ErrorCode::NoError; }
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); }
static bool AppendFileToZip(zipFile& zf, const WCHAR *nameInZip, const char *fileData, size_t fileSize) { ScopedMem<char> nameInZipUtf(str::conv::ToUtf8(nameInZip)); str::TransChars(nameInZipUtf, "\\", "/"); zip_fileinfo zi = { 0 }; int err = zipOpenNewFileInZip64(zf, nameInZipUtf, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 1); if (ZIP_OK == err) { err = zipWriteInFileInZip(zf, fileData, fileSize); if (ZIP_OK == err) err = zipCloseFileInZip(zf); else zipCloseFileInZip(zf); } return ZIP_OK == err; }
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"); } }
extern "C" bool ArchCompressMemoryW(HZIP hZip,void *lpMem,int dwSize,WCHAR *pstrFile) { bool r=false; if ((hZip) && (((ZIPCOMPRESSION *)hZip)->bHandleType == HT_COMPRESSOR) && (lpMem) && (dwSize) && (pstrFile)) { ZIPCOMPRESSION *p=(ZIPCOMPRESSION *)hZip; char *file=UnicodeToOemEx(pstrFile,-1); if (file) { p->bInMem=true; p->lpMem=(byte*)lpMem; p->dwSize=dwSize; zip_fileinfo zi={0}; ///filetime(pstrSourceFile,&zi.tmz_date,&zi.dosDate); char *lpPassword=NULL; unsigned long crcFile=0; if (p->bEncrypted) { crcFile=crc32(0,(byte*)lpMem,dwSize); lpPassword=p->szPassword; } int err=zipOpenNewFileInZip3_64(p->hZip,file,&zi,NULL,0,NULL,0,NULL,(p->dwCompLevel>0) ? Z_DEFLATED:0,p->dwCompLevel,0,-MAX_WBITS,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY,lpPassword,crcFile,0); if (err == ZIP_OK) { if (zipWriteInFileInZip(p->hZip,lpMem,dwSize) == ZIP_OK) r=true; err=zipCloseFileInZip(p->hZip); } } } else ArchSetLastError(ARCH_INVALID_PARAMETER); return r; }
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 addByteArray( zipFile &zf, QString name, QByteArray arr, int compression=0 ){ zip_fileinfo zi; zi.dosDate = 0; zi.internal_fa = 0; zi.external_fa = 0; QDateTime time = QDateTime::currentDateTimeUtc(); setTime( zi.tmz_date, time.date(), time.time() ); //Start file int err = zipOpenNewFileInZip3_64( zf, name.toUtf8().constData(), &zi , NULL, 0, NULL, 0, NULL // comment , (compression != 0) ? Z_DEFLATED : 0, compression, 0 //, 0, 0, 0 , -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY , NULL, 0, 0 // password, crcFile, zip64); ); //Write file zipWriteInFileInZip( zf, arr.constData(), arr.size() ); //Finish file return zipCloseFileInZip( zf ) == ZIP_OK;; }
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; }
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; }
ZipStreamWriter::~ZipStreamWriter() { if (_zip) { zipCloseFileInZip(_zip); } }
void ZipWriter::addFile(std::string name, const void* ptr, unsigned size) { auto t = time(nullptr); auto tm = *localtime(&t); zip_fileinfo info{{(unsigned)tm.tm_sec, (unsigned)tm.tm_min, (unsigned)tm.tm_hour, (unsigned)tm.tm_mday, (unsigned)tm.tm_mon, (unsigned)tm.tm_year + 1900}, 0, 0, 0}; auto ret = zipOpenNewFileInZip64(_zip, name.c_str(), &info, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 1); if (ret) throw std::runtime_error("can't add a new file to zip"); ret = zipWriteInFileInZip(_zip, ptr, size); if (ret) throw std::runtime_error("can't write to zip"); ret = zipCloseFileInZip(_zip); if (ret) throw std::runtime_error("can't save zip"); }
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); }
// 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; }
int ZipArchiveWriter::AddFileItem(IStreamBase* inputStream, const std::string& strNameInZip, int compressFlag) { if (!inputStream || !inputStream->Open()) return -1; zip_fileinfo zi = {0}; zi.dosDate = (uLong)time(nullptr); m_errCode = zipOpenNewFileInZip64(m_hZip, strNameInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, compressFlag, Z_DEFAULT_COMPRESSION, 1); if (m_errCode == ZIP_OK) { int nRead = 0; unsigned char buffer[32768]; while (m_errCode == ZIP_OK) { nRead = inputStream->Read(buffer, sizeof(buffer)/sizeof(buffer[0])); if (nRead <= 0) break; m_errCode = zipWriteInFileInZip(m_hZip, buffer, nRead); } zipCloseFileInZip(m_hZip); if (m_errCode == ZIP_OK) { return 0; } } return -2; }
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); } }
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; }
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; }
static int hb_zipStoreFileHandle( zipFile hZip, HB_FHANDLE hFile, const char * szName, const char * szPassword, const char * szComment ) { char * szZipName; HB_SIZE nLen; zip_fileinfo zfi; int iResult; HB_BOOL fText; HB_U32 ulCRC; if( hFile == FS_ERROR || szName == NULL ) return -200; /* change path separators to '/' */ szZipName = hb_strdup( szName ); nLen = strlen( szZipName ); while( nLen-- ) { if( szZipName[ nLen ] == '\\' ) szZipName[ nLen ] = '/'; } memset( &zfi, 0, sizeof( zfi ) ); zfi.external_fa = 0x81B60020; zfi.tmz_date.tm_sec = 0; zfi.tmz_date.tm_min = 0; zfi.tmz_date.tm_hour = 0; zfi.tmz_date.tm_mday = 1; zfi.tmz_date.tm_mon = 0; zfi.tmz_date.tm_year = 0; ulCRC = 0; fText = HB_FALSE; if( szPassword && hb_zipGetFileInfoFromHandle( hFile, &ulCRC, &fText ) ) zfi.internal_fa = fText ? 1 : 0; else /* TODO: zip.exe test: 0 for binary file, 1 for text. Does not depend on extension. We should analyse content of file to determine this??? */ zfi.internal_fa = 0; iResult = zipOpenNewFileInZip3( hZip, szZipName, &zfi, NULL, 0, NULL, 0, szComment, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, szPassword, ulCRC ); if( iResult == 0 ) { char * pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); hb_fsSeek( hFile, 0, FS_SET ); while( ( nLen = hb_fsReadLarge( hFile, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) zipWriteInFileInZip( hZip, pString, ( unsigned ) nLen ); hb_xfree( pString ); zipCloseFileInZip( hZip ); } hb_xfree( szZipName ); return iResult; }
PatcherError FileUtils::mzAddFile(zipFile zf, const std::string &name, const std::vector<unsigned char> &contents) { // Obviously never true, but we'll keep it here just in case bool zip64 = (uint64_t) contents.size() >= ((1ull << 32) - 1); zip_fileinfo zi; memset(&zi, 0, sizeof(zi)); int ret = zipOpenNewFileInZip2_64( zf, // file name.c_str(), // filename &zi, // zip_fileinfo nullptr, // extrafield_local 0, // size_extrafield_local nullptr, // extrafield_global 0, // size_extrafield_global nullptr, // comment Z_DEFLATED, // method Z_DEFAULT_COMPRESSION, // level 0, // raw zip64 // zip64 ); if (ret != ZIP_OK) { FLOGW("minizip: Failed to add file (error code: {}): [memory]", ret); return PatcherError::createArchiveError( ErrorCode::ArchiveWriteDataError, name); } // Write data to file ret = zipWriteInFileInZip(zf, contents.data(), contents.size()); if (ret != ZIP_OK) { FLOGW("minizip: Failed to write data (error code: {}): [memory]", ret); zipCloseFileInZip(zf); return PatcherError::createArchiveError( ErrorCode::ArchiveWriteDataError, name); } zipCloseFileInZip(zf); return PatcherError(); }
void ZipFile::close() { if (openMode() == NotOpen) return; zipCloseFileInZip(d->archive->d->zip); d->archive->d->currentZipFile = 0; setOpenMode(NotOpen); }
//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 }
//----------------------------------------------------------------------------- // 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; }
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; }
// ---------------------------------------------------------------------------- // 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; }
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()); } }
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 status_t WriteZipFileAux(zipFile zf, const String & baseName, const Message & msg, int compressionLevel, zip_fileinfo * fileInfo) { for (MessageFieldNameIterator iter = msg.GetFieldNameIterator(); iter.HasData(); iter++) { const String & fn = iter.GetFieldName(); uint32 fieldType; if (msg.GetInfo(fn, &fieldType) != B_NO_ERROR) return B_ERROR; switch(fieldType) { case B_MESSAGE_TYPE: { String newBaseName = baseName; if ((newBaseName.HasChars())&&(newBaseName.EndsWith('/') == false)) newBaseName += '/'; newBaseName += fn; // Message fields we treat as sub-directories MessageRef subMsg; for (int32 i=0; msg.FindMessage(fn, i, subMsg) == B_NO_ERROR; i++) if (WriteZipFileAux(zf, newBaseName, *subMsg(), compressionLevel, fileInfo) != B_NO_ERROR) return B_ERROR; } break; case B_RAW_TYPE: { String fileName = baseName; if ((fileName.HasChars())&&(fileName.EndsWith('/') == false)) fileName += '/'; fileName += fn; const void * data; uint32 numBytes; for (int32 i=0; msg.FindData(fn, B_RAW_TYPE, i, &data, &numBytes) == B_NO_ERROR; i++) { if (zipOpenNewFileInZip2(zf, fileName(), // file name fileInfo, // file info NULL, // const void* extrafield_local, 0, // uInt size_extrafield_local, NULL, // const void* extrafield_global, 0, // uInt size_extrafield_global, NULL, // const char* comment, (compressionLevel>0)?Z_DEFLATED:0, // int method, compressionLevel, // int compressionLevel 0) != ZIP_OK) return B_ERROR; if (zipWriteInFileInZip(zf, data, numBytes) != ZIP_OK) return B_ERROR; if (zipCloseFileInZip(zf) != ZIP_OK) return B_ERROR; } } break; } } return B_NO_ERROR; }
static duk_ret_t dukzip_zip_close(duk_context *ctx) { int res = ZIP_OK; zipFile archive = dukzip_zip_from_this(ctx); res = zipCloseFileInZip(archive); if (res == ZIP_OK) { duk_push_true(ctx); } else { duk_push_false(ctx); } return 1; }