bit7z 4.0.0
A C++ library for interfacing with the 7-zip shared libs.
|
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.
Alternatively, if you only need to work on a single archive:
Alternatively, if you only need to work on a single archive:
A complete API reference is available in the wiki section.
The newest bit7z v4 introduced some significant breaking changes to the library's API.
std::string
(instead of std::wstring
), so users can use the library in cross-platform projects more easily (v4 introduced Linux/macOS support too).std::string
s will be considered as UTF-8 encoded.-DBIT7Z_USE_NATIVE_STRING
CMake option.BitExtractor
class is now called BitFileExtractor
.BitExtractor
is just the name of a template class for all the extraction classes.BitCompressor
class is now called BitFileCompressor
.BitCompressor
is just the name of a template class for all the compression classes.ProgressCallback
now must return a bool
value indicating whether the current operation can continue (true
) or not (false
).include/
to the include/bit7z/
folder, so #include
directives now need to prepend bit7z/
to the included header name (e.g., #include <bit7z/bitfileextractor.hpp>
).lib/<architecture>/
; if the CMake generator is multi-config (e.g., Visual Studio generators), the default output folder is lib/<architecture>/<build type>/
.BIT7Z_VS_LIBNAME_OUTDIR_STYLE
CMake option.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:
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:
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 currently 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.
On Linux and macOS, 7-zip v23.01 introduced breaking changes to the IUnknown interface. If you build bit7z for such a version of 7-zip (the default), it will not support using the shared libraries from previous versions of 7-zip (or from p7zip). Conversely, bit7z made for earlier versions of 7-zip or for p7zip is incompatible with the shared libraries from 7-zip v23.01 and later.
You can build the shared libraries of 7-zip v23.01 in a backward-compatible mode by defining the macro Z7_USE_VIRTUAL_DESTRUCTOR_IN_IUNKNOWN
. If this is your case, you can build bit7z for v23.01 using the option BIT7Z_USE_LEGACY_IUNKNOWN
(in this case, bit7z will be compatible also with previous versions of 7-zip/p7zip).
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.On POSIX systems, std::string
s are usually already UTF-8 encoded, and no configuration is needed.
The situation is a bit more complex on Windows since, by default, Windows treats std::string
s as encoded using the system code page, which may not necessarily be UTF-8, like, for example, Windows-1252.
If your program deals exclusively with ASCII-only strings, you should be fine with the default bit7z settings (as ASCII characters are also UTF-8).
However, if you need to handle non-ASCII/Unicode characters, as it is likely, you have the following options:
std::string
s passed to bit7z:ReadConsoleW
), and convert it to UTF-8 (bit7z provides a utility function for this, bit7z::to_tstring
).SetConsoleOutputCP(CP_UTF8)
before.std::wstring
) by enabling the BIT7Z_USE_NATIVE_STRING
option via CMake.std::string
on POSIX systems.bit7z::tstring
and a macro function BIT7Z_STRING
for defining wide string variables and literals on Windows and narrow ones on other platforms.You must programmatically set the standard input and output encoding to UTF-16 to correctly read and print Unicode characters:
std::string
by enabling the BIT7Z_USE_SYSTEM_CODEPAGE
option via CMake.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.