bit7z是一个对7-zip的静态wrapper库(GitHub - rikyoz/bit7z: A C++ static library offering a clean and simple interface to the 7-zip shared libraries. · GitHub),大大简化了文件压缩与解压的过程,提供一个简单的示例。7-zip支持市面上绝大多数压缩文件格式(zip,7z,tar,xz,...),都可以通过bit7z进行调用。
#pragma once #include <bit7z/bitarchivereader.hpp> #include <bit7z/bitarchivewriter.hpp> #include <io.h> #include <cstdio> typedef std::function<void(double progress)> ProgressCallback; typedef std::function<void(bit7z::tstring filename)> MyFileCallback; typedef std::function<void(const bit7z::BitException&)> BitExceptionCallback; template<class T> class FZHelper { public: FZHelper() :TotalSize(0), m_pProgressFunc(nullptr), m_pFileFunc(nullptr), m_pExpFunc(nullptr) {} inline void TotalSizeNotify(uint64_t total_size) { return static_cast<T*>(this)->TotalSizeCB(total_size); } inline bool ProgressNotify(uint64_t size) { return static_cast<T*>(this)->ProgressCB(size); } inline void FileNameNotify(bit7z::tstring filename) { return static_cast<T*>(this)->FileNameCB(filename); } inline void TotalSizeCB(uint64_t total_size) { TotalSize = total_size; printf("TotalCallback: %lld.\n", total_size); } inline bool ProgressCB(uint64_t size) { double progress = ((1.0 * size) / TotalSize); printf("ProgressCallback: %.4f.\n", TotalSize == 0 ? 0.0 : (100 * 1.0 * size) / TotalSize); if (m_pProgressFunc) { m_pProgressFunc(progress); } return true; } inline void FileNameCB(bit7z::tstring filename) { printf("Begin Process File %s.\n", filename.c_str()); if (m_pFileFunc) { m_pFileFunc(filename); } } protected: uint64_t TotalSize; private: ProgressCallback m_pProgressFunc; MyFileCallback m_pFileFunc; BitExceptionCallback m_pExpFunc; }; template <typename T> bool ExtractFileTo(const char* zipSrcPath, const char* dstDir, const bit7z::BitInOutFormat& bitFormat, FZHelper<T>* hlp) { bool bRet = false; DWORD attr = 0; if (_access(dstDir, 0) == -1) return bRet; if (_access(zipSrcPath, 0) == -1) return bRet; attr = GetFileAttributesA(dstDir); if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) return bRet; try { // bit7z classes can throw BitException objects using namespace bit7z; Bit7zLibrary lib{ "7z.dll" };//find 7z.dll on your computer,if you don't have 7z.dll, then install 7z software to get one. // Opening the archive BitArchiveReader archive{ lib, zipSrcPath, BitFormat::SevenZip }; if (hlp != nullptr) { bit7z::TotalCallback tcb = [hlp](uint64_t sz) {hlp->TotalSizeNotify(sz); }; bit7z::FileCallback fcb = [hlp](bit7z::tstring filename) {hlp->FileNameNotify(filename); }; bit7z::ProgressCallback pcb = [hlp](uint64_t sz) {return hlp->ProgressNotify(sz); }; archive.setTotalCallback(tcb); archive.setFileCallback(fcb); archive.setProgressCallback(pcb); } //archive.setPassword("123456"); // Testing the archive archive.test(); // Extracting the archive archive.extractTo(dstDir); bRet = true; } catch (const bit7z::BitException& ex) { std::error_code code = ex.code(); int errCode = code.value(); if (errCode == 126) printf("Please Get the 7z mudule firstly.\n"); printf("error: %s[%d].\n", ex.what(), errCode); } return bRet; } template <typename T> bool CompressFolderTo(const char* folderPath, const char* zipDstPath, const bit7z::BitInOutFormat& bitFormat, FZHelper<T>* hlp) { bool bRet = false; try { // bit7z classes can throw BitException objects using namespace bit7z; Bit7zLibrary lib{ "7z.dll" };//find 7z.dll on your computer,if you don't have 7z.dll, then install 7z software to get one. BitArchiveWriter archive{ lib, BitFormat::SevenZip }; if (hlp != nullptr) { bit7z::TotalCallback tcb = [hlp](uint64_t sz) {hlp->TotalSizeNotify(sz); }; bit7z::FileCallback fcb = [hlp](bit7z::tstring filename) {hlp->FileNameNotify(filename); }; bit7z::ProgressCallback pcb = [hlp](uint64_t sz) {return hlp->ProgressNotify(sz); }; archive.setTotalCallback(tcb); archive.setFileCallback(fcb); archive.setProgressCallback(pcb); } //archive.setPassword("123456"); // Adding the items to be compressed (no compression is performed here) //archive.addFile("C:/Users/project/test/file1.txt", "A/B/file1.txt"); //archive.addFile("C:/Users/project/test/file2.txt", "A/file2.txt"); archive.addDirectory(folderPath);//add the whole test folder(include the all subfolders and files of test) if (_access(zipDstPath, 0) == 0)//delete if already existed. { if (remove(zipDstPath) != 0) { goto end; } } // Compressing the added items to the output archive archive.compressTo(zipDstPath); bRet = true; } catch (const bit7z::BitException& ex) { std::error_code code = ex.code(); int errCode = code.value(); if (errCode == 126) printf("Please Get the 7z mudule firstly.\n"); printf("error: %s[%d].\n", ex.what(), errCode); } end: return bRet; }