Supported Features • Getting Started • Download • Requirements • Building & Using • Donate • License
bit7z is a cross-platform C++ static library that allows the compression/extraction of archive files through a clean and simple wrapper interface to the dynamic libraries from the 7-zip project.
It supports compression and extraction to and from the filesystem or the memory, reading archives metadata, updating existing ones, creating multi-volume archives, operation progress callbacks, and many other functionalities.
The presence or not of some of the above features depends on the particular shared library used along with bit7z.
For example, 7z.dll should support all these features, 7za.dll should work only with the 7z file format, and 7zxa.dll can only extract 7z files. For more information about the 7-zip DLLs, please check this wiki page.
In the end, some other features (e.g., automatic format detection and selective extraction using regular expressions) are disabled by default, and macro definitions must be used during compilation to have them available (wiki).
Below are a few examples that show how to use some of the main features of 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()...*/ }
Alternatively, if you only need to work on a single archive:
#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()...*/ }
Alternatively, if you only need to work on a single archive:
#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() << "\n\n";
// 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()...*/ }
A complete API reference is available in the wiki section.
The newest bit7z v4 introduced some significant breaking changes to the library's API.
Each released package contains:
Packages are available for both x86 and x64 architectures.
You can also clone/download this repository and build the library yourself (please, read the wiki).
.dll
library on Windows, a 7-zip/p7zip .so
library on Unix[^3].[^1]: On Windows, you should link your program also with oleaut32 (e.g., -lbit7z -loleaut32
).
On Linux and macOS, you should link your program also with dl (e.g., -lbit7z -ldl
).
If you are using the library via CMake, these dependencies will be linked automatically to your project.
[^2]: MSVC 2010 was supported until v2.x, MSVC 2012/2013 until v3.x.
[^3]: bit7z doesn't ship with the 7-zip shared libraries. You can build them from the source code available at 7-zip.org.
For building the library:
cd <bit7z folder>
mkdir build && cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release
cmake --build . -j --config Release
A more detailed guide on how to build this library is available here.
You can also directly integrate the library into your project via CMake:
third_party
), or add it as a git submodule of your repository.add_subdirectory()
in your CMakeLists.txt
to include bit7z.bit7z
library using the target_link_libraries()
command.For example:
add_subdirectory( ${CMAKE_SOURCE_DIR}/third_party/bit7z )
target_link_libraries( ${YOUR_TARGET} PRIVATE bit7z )
The library is highly customizable: for a detailed list of the available build options, please refer to the wiki.
While configuring bit7z via CMake, it automatically downloads the latest version of 7-zip supported by the library.
Optionally, you can specify a different version of 7-zip via the CMake option BIT7Z_7ZIP_VERSION
(e.g., -DBIT7Z_7ZIP_VERSION="22.01"
).
Alternatively, you can specify a custom path containing the 7-zip source code via the option BIT7Z_CUSTOM_7ZIP_PATH
.
Please note that, in general, it is best to use the same version of 7-zip of the shared libraries that you will use at runtime.
By default, bit7z is compatible with the 7z.so
from 7-zip v23.01 and later.
If you plan to use the 7z.so
from p7zip or 7-zip v22.01 and earlier instead, you have two ways to make bit7z compatible:
-DBIT7Z_USE_LEGACY_IUNKNOWN=ON
; or-DBIT7Z_7ZIP_VERSION="22.01"
).By default, bit7z follows the UTF-8 Everywhere Manifesto to simplify the use of the library within cross-platform projects. In short, this means that:
std::string
.std::string
s are considered as UTF-8 encoded; output std::string
s are UTF-8 encoded.If you have found this project helpful, please consider supporting me with a small donation so that I can keep improving it! Thank you! :pray:
This project is licensed under the terms of the Mozilla Public License v2.0.
For more details, please check:
Older versions (v3.x and earlier) of bit7z were released under the GNU General Public License v2.