支持的功能•入門•下載•要求•構建和使用•捐贈•許可證
BIT7Z是一個跨平台C ++靜態庫,允許通過清潔,簡單的包裝器接口壓縮/提取檔案文件,從7-ZIP項目中的動態庫進行壓縮/提取。
它支持往返文件系統或內存的壓縮和提取,讀取檔案元數據,更新現有的檔案,創建多批量檔案,操作進度回調以及許多其他功能。
上述某些功能的存在是否取決於與Bit7Z一起使用的特定共享庫。
例如,7z.dll應支持所有這些功能,7za.dll只能與7Z文件格式一起使用,而7zxa.dll只能提取7Z文件。有關7-ZIP DLL的更多信息,請檢查此Wiki頁面。
最後,默認情況下禁用了其他一些其他功能(例如,使用正則表達式的自動格式檢測和選擇性提取),並且在編譯過程中必須使用宏定義以使它們可用(Wiki)。
以下是一些示例,顯示瞭如何使用BIT7Z的一些主要功能。
# include < bit7z/bitfileextractor.hpp >
try { // bit7z classes can throw BitException objects
using namespace bit7z ;
Bit7zLibrary lib{ " 7za.dll " };
BitFileExtractor extractor{ lib, BitFormat::SevenZip };
// Extracting a simple archive
extractor. extract ( " path/to/archive.7z " , " out/dir/ " );
// Extracting a specific file inside an archive
extractor. extractMatching ( " path/to/archive.7z " , " file.pdf " , " out/dir/ " );
// Extracting the first file of an archive to a buffer
std::vector< byte_t > buffer;
extractor. extract ( " path/to/archive.7z " , buffer );
// Extracting an encrypted archive
extractor. setPassword ( " password " );
extractor. extract ( " path/to/another/archive.7z " , " out/dir/ " );
} catch ( const bit7z::BitException& ex ) { /* Do something with ex.what()... */ }另外,如果您只需要在一個存檔上工作:
# include < bit7z/bitarchivereader.hpp >
try { // bit7z classes can throw BitException objects
using namespace bit7z ;
Bit7zLibrary lib{ " 7z.dll " };
// Opening the archive
BitArchiveReader archive{ lib, " path/to/archive.gz " , BitFormat::GZip };
// Testing the archive
archive. test ();
// Extracting the archive
archive. extractTo ( " out/dir/ " );
} catch ( const bit7z::BitException& ex ) { /* Do something with ex.what()... */ }# include < bit7z/bitfilecompressor.hpp >
try { // bit7z classes can throw BitException objects
using namespace bit7z ;
Bit7zLibrary lib{ " 7z.dll " };
BitFileCompressor compressor{ lib, BitFormat::Zip };
std::vector< std::string > files = { " path/to/file1.jpg " , " path/to/file2.pdf " };
// Creating a simple zip archive
compressor. compress ( files, " output_archive.zip " );
// Creating a zip archive with a custom directory structure
std::map< std::string, std::string > files_map = {
{ " path/to/file1.jpg " , " alias/path/file1.jpg " },
{ " path/to/file2.pdf " , " alias/path/file2.pdf " }
};
compressor. compress ( files_map, " output_archive2.zip " );
// Compressing a directory
compressor. compressDirectory ( " dir/path/ " , " dir_archive.zip " );
// Creating an encrypted zip archive of two files
compressor. setPassword ( " password " );
compressor. compressFiles ( files, " protected_archive.zip " );
// Updating an existing zip archive
compressor. setUpdateMode ( UpdateMode::Append );
compressor. compressFiles ( files, " existing_archive.zip " );
// Compressing a single file into a buffer
std::vector< bit7z:: byte_t > buffer;
BitFileCompressor compressor2{ lib, BitFormat::BZip2 };
compressor2. compressFile ( files[ 0 ], buffer );
} catch ( const bit7z::BitException& ex ) { /* Do something with ex.what()... */ }另外,如果您只需要在一個存檔上工作:
# include < bit7z/bitarchivewriter.hpp >
try { // bit7z classes can throw BitException objects
using namespace bit7z ;
Bit7zLibrary lib{ " 7z.dll " };
BitArchiveWriter archive{ lib, BitFormat::SevenZip };
// Adding the items to be compressed (no compression is performed here)
archive. addFile ( " path/to/file.txt " );
archive. addDirectory ( " path/to/dir/ " );
// Compressing the added items to the output archive
archive. compressTo ( " output.7z " );
} catch ( const bit7z::BitException& ex ) { /* Do something with ex.what()... */ }# include < bit7z/bitarchivereader.hpp >
try { // bit7z classes can throw BitException objects
using namespace bit7z ;
Bit7zLibrary lib{ " 7za.dll " };
BitArchiveReader arc{ lib, " archive.7z " , BitFormat::SevenZip };
// Printing archive metadata
std::cout << " Archive properties n " ;
std::cout << " Items count: " << arc. itemsCount () << ' n ' ;
std::cout << " Folders count: " << arc. foldersCount () << ' n ' ;
std::cout << " Files count: " << arc. filesCount () << ' n ' ;
std::cout << " Size: " << arc. size () << ' n ' ;
std::cout << " Packed size: " << arc. packSize () << " nn " ;
// Printing the metadata of the archived items
std::cout << " Archived items " ;
for ( const auto & item : arc ) {
std::cout << ' n ' ;
std::cout << " Item index: " << item. index () << ' n ' ;
std::cout << " Name: " << item. name () << ' n ' ;
std::cout << " Extension: " << item. extension () << ' n ' ;
std::cout << " Path: " << item. path () << ' n ' ;
std::cout << " IsDir: " << item. isDir () << ' n ' ;
std::cout << " Size: " << item. size () << ' n ' ;
std::cout << " Packed size: " << item. packSize () << ' n ' ;
std::cout << " CRC: " << std::hex << item. crc () << std::dec << ' n ' ;
}
std::cout. flush ();
} catch ( const bit7z::BitException& ex ) { /* Do something with ex.what()... */ }Wiki部分提供了完整的API參考。
最新的BIT7Z V4引入了圖書館API的一些重大破壞變化。
std::string (而不是std::wstring ),因此用戶可以更輕鬆地在跨平台項目中使用庫(V4引入了Linux/MacOS支持)。std::string S將被視為UTF-8編碼。-DBIT7Z_USE_NATIVE_STRING cmake選項在Windows上實現舊行為。BitExtractor類稱為BitFileExtractor 。BitExtractor只是所有提取類的模板類的名稱。BitCompressor類稱為BitFileCompressor 。BitCompressor只是所有壓縮類的模板類的名稱。ProgressCallback必須返回一個bool值,以指示當前操作是否可以繼續( true )( false )。include/ include/bit7z/文件夾移動,因此#include指令現在需要將bit7z/預先登錄到隨附的標頭名稱(例如, #include <bit7z/bitfileextractor.hpp> )。lib/<architecture>/ ;如果CMAKE GENERATOR是多組合(例如,Visual Studio Generators),則默認輸出文件夾為lib/<architecture>/<build type>/ 。BIT7Z_VS_LIBNAME_OUTDIR_STYLE CMAKE選項來強制使用“ Visual Studio樣式”輸出路徑。每個發布的軟件包都包含:
X86和X64架構都可以使用包裝。
您也可以克隆/下載此存儲庫並自己構建庫(請閱讀Wiki)。
.dll庫,UNIX 3上的7 ZIP/P7ZIP .so庫。 用於構建圖書館:
cd < bit7z folder >
mkdir build && cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release
cmake --build . -j --config Release有關如何構建此庫的更詳細指南,請參見此處。
您還可以通過CMAKE將庫直接集成到您的項目中:
third_party ),或將其添加為存儲庫的git子模塊。CMakeLists.txt中的命令add_subdirectory()以包含bit7z。target_link_libraries()命令鏈接bit7z庫。例如:
add_subdirectory ( ${CMAKE_SOURCE_DIR} /third_party/bit7z )
target_link_libraries ( ${YOUR_TARGET} PRIVATE bit7z )該庫是高度自定義的:有關可用構建選項的詳細列表,請參閱Wiki。
在通過CMAKE配置BIT7Z時,它會自動下載庫支持的最新版本的7-ZIP。
可選地,您可以通過CMAKE選項BIT7Z_7ZIP_VERSION (例如, -DBIT7Z_7ZIP_VERSION="22.01" )指定不同版本的7 -zip。
另外,您可以通過選項BIT7Z_CUSTOM_7ZIP_PATH指定包含7個ZIP源代碼的自定義路徑。
請注意,通常,最好使用在運行時使用的共享庫的7個拉鍊。
默認情況下,BIT7Z與7-zip v23.01及更高版本的7z.so兼容。
如果您打算使用P7ZIP或7-ZIP V22.01及更早的7z.so ,則有兩種使Bit7Z兼容的方法:
-DBIT7Z_USE_LEGACY_IUNKNOWN=ON ;或者-DBIT7Z_7ZIP_VERSION="22.01"配置Bit7Z)。在Linux和MacOS上,7-ZIP v23.01引入了Iunknown接口的破壞更改。結果,如果您為7-zip版本(默認)構建了Bit7Z,則它將不支持使用7-ZIP(或P7ZIP)的共享庫。相反,為7 ZIP或P7ZIP的早期版本製作的BIT7Z與7-ZIP V23.01及以後的共享庫不兼容。
您可以通過定義宏Z7_USE_VIRTUAL_DESTRUCTOR_IN_IUNKNOWN來構建向後兼容模式的7 ZIP V23.01的共享庫。如果是您的情況,您需要啟用BIT7Z_USE_LEGACY_IUNKNOWN以使BIT7Z工作(在這種情況下,Bit7Z也將與以前的7-ZIP/P7ZIP的版本兼容)。
默認情況下,BIT7Z遵循UTF-8無處不在宣言,以簡化跨平台項目中庫的使用。簡而言之,這意味著:
std::string 。std::string s被認為是UTF-8編碼;輸出std::string S是UTF-8編碼。在POSIX Systems上, std::string S通常已經編碼UTF-8,並且不需要配置。
在Windows上,這種情況更為複雜,因為默認情況下,Windows將std::string S作為使用System Code Page編碼的std :: String S,這可能不一定是UTF-8,例如Windows-1252。
如果您的程序專門處理僅ASCII-lyly字符串,則應使用默認的BIT7Z設置(因為ASCII字符也是UTF-8)。
但是,如果您需要處理非ASCII/Unicode字符,則可能有以下選項:
使用Microsoft使用的整個應用程序使用UTF-8代碼頁面強制執行:
手動確保std::string s的編碼傳遞給BIT7Z:
ReadConsoleW ),然後將其轉換為UTF-8(BIT7Z為此提供了效用功能, bit7z::to_tstring )。SetConsoleOutputCP(CP_UTF8) ,將bit7z(例如,文件中的路徑/名稱元數據)從BIT7Z(例如,文件的路徑/名稱元數據)正確打印到控制台上。通過CMAKE啟用BIT7Z_USE_NATIVE_STRING選項,配置Bit7Z以使用UTF-16編碼的寬字符串(即, std::wstring )。
如果您的程序僅限Windows,或者您已經在Windows上使用了寬字符串,那麼這可能是最佳選擇,因為它可以避免任何內部字符串轉換(7-ZIP始終使用寬字符串)。
此選項使開發跨平台應用程序有些不便,因為您仍然必須在POSIX系統上使用std::string 。
該庫提供了類型的別名bit7z::tstring和一個宏功能BIT7Z_STRING ,用於在其他平台上定義寬字符串變量和文字範圍。
您必須以編程方式將標準輸入和輸出編碼設置為UTF-16,以正確讀取和打印Unicode字符:
# include < fcntl.h > // for _O_U16TEXT
# include < io.h > // for _setmode
_setmode (_fileno(stdout), _O_U16TEXT); // setting the stdout encoding to UTF16
_setmode (_fileno(stdin), _O_U16TEXT); // setting the stdin encoding to UTF16通過CMAKE啟用BIT7Z_USE_SYSTEM_CODEPAGE選項,配置Bit7Z以使用std::string的系統代碼頁面編碼。
如果您發現該項目有幫助,請考慮通過少量捐款來支持我,以便我可以繼續改善它!謝謝你!
該項目是根據Mozilla公共許可證v2.0的條款許可的。
有關更多詳細信息,請檢查:
BIT7Z的較舊版本(v3.x和更早)根據GNU通用公共許可證V2發布。
在Windows上,您還應將程序也鏈接到OLEAUT32 (例如-lbit7z -loleaut32 )。
在Linux和MACOS上,您也應該將程序也鏈接到DL (例如-lbit7z -ldl )。
如果您通過CMAKE使用庫,這些依賴項將自動鏈接到您的項目。 ↩
MSVC 2010得到支持,直到V2.X,MSVC 2012/2013年,直到V3.x。 ↩
Bit7Z不使用7個拉鍊共享庫。您可以從7-zip.org的可用源代碼構建它們。 ↩