支持的功能•入门•下载•要求•构建和使用•捐赠•许可证
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的可用源代码构建它们。 ↩