note : create zip and add file to it
2014-03-27 02:15
330 查看
在csdn上看了一段将整个目录压缩成一个zip文件的代码片段.
我的应用是建立一个zip, 向里面压入几个文件.
所以我整理了一下,那个csdner的代码,使之符合我的应用.
调用端代码:
zip操作功能函数实现
这应用,是建立一个新zip文件, 一次性向里面压入文件.
如果是新建一个zip文件,然后关闭,再打开后,向里面压入文件。发现新建的zip里必须要有一个文件(e.g. readme.txt), 才能成功压入新的文件.
还没有去验证,是否新建一个文件后,建立一个"/"文件后, 是否可以在关闭再打开后,顺利压入新文件.
我的应用是建立一个zip, 向里面压入几个文件.
所以我整理了一下,那个csdner的代码,使之符合我的应用.
调用端代码:
LONG WINAPI ExceptionProc(struct _EXCEPTION_POINTERS* ExceptionInfo) { zipFile hZip = NULL; std::wstring strDumpZipFilePathName = L""; std::wstring strDumpBinFilePathName = L""; std::wstring strDumpTxtFilePathName = L""; ns_base::GenerateCrashFilePathName( strDumpZipFilePathName, strDumpBinFilePathName, strDumpTxtFilePathName); RecordExceptionInfo( ExceptionInfo, L"ExceptionProc", strDumpBinFilePathName.c_str(), strDumpTxtFilePathName.c_str()); if (ns_base::CreateZipFile(strDumpZipFilePathName.c_str(), hZip, TRUE)) { ns_base::AddFileToZip(hZip, strDumpBinFilePathName.c_str()); ns_base::DeleteFilePro(strDumpBinFilePathName.c_str()); ns_base::AddFileToZip(hZip, strDumpTxtFilePathName.c_str()); ns_base::DeleteFilePro(strDumpTxtFilePathName.c_str()); ns_base::CloseZipFile(hZip, L"create by ExceptionProc"); /// use this zip file...(e.g. upload crash info to developer) } return 0; }
zip操作功能函数实现
BOOL CreateZipFile(const WCHAR * pcNewZipFilePathName, zipFile & hZip, BOOL bCreateNewZip) { USES_CONVERSION; hZip = NULL; if (NULL == pcNewZipFilePathName) return FALSE; hZip = zipOpen(W2CA(pcNewZipFilePathName), bCreateNewZip ? APPEND_STATUS_CREATE : APPEND_STATUS_ADDINZIP); return (NULL != hZip); } BOOL AddFileToZip(zipFile & hZip, const WCHAR * pcFileAddToZip) { USES_CONVERSION; FILE* srcfp = NULL; std::wstring strDriver = L""; std::wstring strDir = L""; std::wstring strFileName = L""; std::wstring strFileNamePostfix = L""; zip_fileinfo zi; ZeroMemory(&zi, sizeof(zip_fileinfo)); char szFileNameInZip[MAX_PATH]; ZeroMemory(szFileNameInZip, sizeof(szFileNameInZip)); strcat(szFileNameInZip, "/"); if (NULL == pcFileAddToZip) return FALSE; ns_base::SplitFilePathName( pcFileAddToZip, strDriver, strDir, strFileName, strFileNamePostfix); strcat(szFileNameInZip, W2A(strFileName.c_str())); strcat(szFileNameInZip, W2A(strFileNamePostfix.c_str())); if (ZIP_OK != zipOpenNewFileInZip(hZip, szFileNameInZip, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION)) return FALSE; if (NULL != pcFileAddToZip) { srcfp = fopen(W2A(pcFileAddToZip), "rb"); if (NULL == srcfp) return FALSE; char buf[64 * 1024]; int numBytes = 0; while (!feof(srcfp)) { numBytes = fread(buf, 1, sizeof(buf), srcfp); zipWriteInFileInZip(hZip, buf, numBytes); if(ferror(srcfp)) break; } fclose(srcfp); } zipCloseFileInZip(hZip); return TRUE; } BOOL CloseZipFile(zipFile & hZip, const WCHAR * pcComment) { USES_CONVERSION; if (NULL == hZip) return FALSE; zipClose(hZip, (NULL == pcComment) ? "" : W2CA(pcComment)); return TRUE; }
这应用,是建立一个新zip文件, 一次性向里面压入文件.
如果是新建一个zip文件,然后关闭,再打开后,向里面压入文件。发现新建的zip里必须要有一个文件(e.g. readme.txt), 才能成功压入新的文件.
还没有去验证,是否新建一个文件后,建立一个"/"文件后, 是否可以在关闭再打开后,顺利压入新文件.
相关文章推荐
- Home not found. Define system property "openfireHome" or create and add the openfire_init.xml file to the classpath
- create sqlserver login user and add it to certain db user (sqlserver authentication)
- how to create image file and format it
- add cool toolbar 256corler is easy,only two file and onCreate func is needed also the MainFrame.toolbar must resize to what real need and also the load width is same to it.
- After press print button, it will displays Print To File; and the progress always 0%
- How to generate core dump file and debug with it?
- How To create Netbeans Platform and add to netbeans IDE by netbean platform manager
- Add a file to a Document Library and update metadata properties in a single method添加文档的方法
- BOM API - uses the BOM_BO_PUB.PROCESS_BOM API to create bill header and add components to the bill
- Create stored procedure and integrate it into linq to entities
- Using ZipLib to create a Zip File in C#
- How to create .lib file when you only have .dll and .h files
- 警告: git command could not be found. Please create an alias or add it to yo
- 警告: git command could not be found. Please create an alias or add it to yo
- [Drupal] How to add the js file and js code block in Drupal
- Ubuntu Linux Create and Add Swap File Tutorial
- How to Create the CAPL DLL and integrated it into CANOE
- Configuration of DLL usage in VS2005--how to add additionl lib and head file
- How to create and execute JAR file in Java – Command line Eclipse Netbeans
- PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath s