int StudioFrame::CreateZip(std::string zfile, std::vector<std::string> files) { struct archive *zip; zip = archive_write_new(); if (zip == NULL) { wxMessageBox(_("archive_write_new error")); return -1; } archive_write_set_format_zip(zip); // archive_write_zip_set_compression_store(zip); archive_write_open_filename(zip, zfile.c_str()); if (WriteDefsToZip(zip)) { archive_write_close(zip); archive_write_finish(zip); return -1; } for (size_t i=0; i<files.size(); i++) { if (WriteFileToZip(zip, files[i])) { archive_write_close(zip); archive_write_finish(zip); return -1; } } archive_write_close(zip); archive_write_finish(zip); return 0; }
// //////////////////////////////////////////////////////////////////////////////// // @global 将文件夹内文件添加进 zip ( 递归 ) // void AddFileToZip(zipFile& zf, LPCWSTR wzPath, LPCWSTR wzParentPath, PBYTE buf) { WIN32_FIND_DATAW ffd = {0}; WCHAR wzFindStr[MAX_PATH] = {0}; wsprintf(wzFindStr, L"%s\\*", wzPath); HANDLE hFind = FindFirstFileW(wzFindStr, &ffd); do { if ( 0 == wcscmp(ffd.cFileName, L".") || 0 == wcscmp(ffd.cFileName, L"..") ) { continue; } if ( ffd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ) { WCHAR wzSubDir[MAX_PATH] = {0}; wsprintf(wzSubDir, L"%s\\%s", wzPath, ffd.cFileName); AddFileToZip(zf, wzSubDir, wzParentPath, buf); continue; } WCHAR wzFullPath[MAX_PATH] = {0}; wsprintf(wzFullPath, L"%s\\%s", wzPath, ffd.cFileName); WriteFileToZip(ffd, zf, wzFullPath, wzParentPath, buf); DebugTools::OutputDebugPrintfW(L"[ZipTools] [AddFileToZip] Add File Success. [%s]\r\n", wzFullPath); } while ( 0 != FindNextFileW(hFind, &ffd)); }