bit7z
| Language | C++ (C++11 to use, C++14 to build) |
|---|---|
| License | MPL-2.0 (versions 3.x and older: GPL v2) |
| Repository | github.com/rikyoz/bit7z |
| Documentation | bit7z wiki and API reference |
| Depends on | the 7-Zip shared library at runtime (7z.dll, 7za.dll or 7z.so), not included |
| Read / write | yes / yes, all 7-Zip methods, AES-256, header encryption, solid mode, volumes |
bit7z is a C++ library that loads the 7-Zip shared library and exposes it through classes like BitFileExtractor, BitArchiveReader and BitArchiveWriter. Because the real 7-Zip code does the work, it supports every 7z feature: LZMA2, PPMd, BZip2, BCJ2, AES-256 encryption of data and headers, solid blocks, multi-volume archives, and also ZIP, TAR, RAR (read) and the other formats 7-Zip handles.
You ship a 7-Zip library with your program: 7z.dll from a 7-Zip installation (all formats) or 7za.dll from 7-Zip Extra (7z only, smaller) on Windows; on Linux and macOS a 7z.so built from 7-Zip or p7zip. Errors are reported as bit7z::BitException. Strings are std::string (UTF-8) by default; on Windows you can switch to std::wstring with the BIT7Z_USE_NATIVE_STRING CMake option.
Installation#
# vcpkg
vcpkg install bit7z
find_package(bit7z CONFIG REQUIRED)
target_link_libraries(myapp PRIVATE bit7z::bit7z)
Or add the repository as a subdirectory (add_subdirectory(bit7z), then link bit7z), or use CPM: CPMAddPackage("gh:rikyoz/bit7z@4.1.0") with the release you want. Then copy 7z.dll next to your executable.
Basic usage: extract and create a 7z archive#
#include <bit7z/bitfileextractor.hpp>
#include <bit7z/bitarchivewriter.hpp>
#include <iostream>
int main() {
using namespace bit7z;
try {
Bit7zLibrary lib{ "7z.dll" }; // "7z.so" on Linux
// extract everything
BitFileExtractor extractor{ lib, BitFormat::SevenZip };
extractor.extract( "archive.7z", "out/dir/" );
// create a new archive from a file and a folder
BitArchiveWriter writer{ lib, BitFormat::SevenZip };
writer.addFile( "report.pdf" );
writer.addDirectory( "photos/" );
writer.compressTo( "backup.7z" );
} catch ( const BitException& ex ) {
std::cerr << "error: " << ex.what() << '\n';
return 1;
}
return 0;
}
How to list the files in a 7z archive#
#include <bit7z/bitarchivereader.hpp>
#include <iostream>
void list(const bit7z::Bit7zLibrary& lib) {
bit7z::BitArchiveReader arc{ lib, "archive.7z", bit7z::BitFormat::SevenZip };
std::cout << arc.itemsCount() << " items, " << arc.size() << " bytes, packed "
<< arc.packSize() << "\n";
for ( const auto& item : arc ) {
std::cout << item.path() << " " << item.size()
<< (item.isDir() ? " <dir>" : "") << '\n';
}
}
BitArchiveReader opens the archive once; you can call arc.test() to verify CRCs and arc.extractTo("out/") to extract.
How to extract one file to memory#
#include <bit7z/bitfileextractor.hpp>
#include <map>
#include <vector>
bit7z::BitFileExtractor extractor{ lib, bit7z::BitFormat::SevenZip };
// all files into a map: path -> bytes
std::map< std::string, std::vector< bit7z::byte_t > > files;
extractor.extract( "archive.7z", files );
// only the files matching a wildcard, to a folder
extractor.extractMatching( "archive.7z", "*.pdf", "out/dir/" );
How to create an encrypted 7z archive#
#include <bit7z/bitfilecompressor.hpp>
#include <vector>
bit7z::BitFileCompressor compressor{ lib, bit7z::BitFormat::SevenZip };
compressor.setPassword( "secret", true ); // true: also encrypt file names (header)
std::vector< std::string > files = { "a.txt", "b.docx" };
compressor.compress( files, "secret.7z" );
// extracting needs the same password
bit7z::BitFileExtractor extractor{ lib, bit7z::BitFormat::SevenZip };
extractor.setPassword( "secret" );
extractor.extract( "secret.7z", "out/" );
How to set compression level, method and solid mode#
bit7z::BitArchiveWriter writer{ lib, bit7z::BitFormat::SevenZip };
writer.setCompressionLevel( bit7z::BitCompressionLevel::Ultra ); // None, Fastest, Fast, Normal, Max, Ultra
writer.setCompressionMethod( bit7z::BitCompressionMethod::Lzma2 ); // Lzma, Ppmd, BZip2, Copy...
writer.setSolidMode( false ); // one block per file: faster single-file extraction
writer.setVolumeSize( 100'000'000 ); // split into 100 MB volumes (.7z.001, .002...)
writer.addDirectory( "data/" );
writer.compressTo( "data.7z" );
Tips#
BitFormat::Autodetects the format of an archive you open (needs theBIT7Z_AUTO_FORMATCMake option).- Progress and cancellation:
setProgressCallbackreceives the number of bytes processed; returnfalseto stop. - Use
7za.dll(7-Zip Extra) if you only need 7z: it’s smaller than7z.dllbut has no RAR, ISO or other read-only formats.