bit7z 4.1.0
A C++ library for interfacing with the 7-zip shared libs.
Loading...
Searching...
No Matches
BitFileCompressor Class Referencefinal

The BitFileCompressor class allows compressing files and directories. More...

#include <bit7z/bitfilecompressor.hpp>

Inheritance diagram for BitFileCompressor:
Collaboration diagram for BitFileCompressor:

Public Member Functions

 BitFileCompressor (const Bit7zLibrary &lib, const BitInOutFormat &format)
 Constructs a BitFileCompressor object.
void clearPassword () noexcept
 Clear the current password used by the handler.
void compress (const std::map< tstring, tstring > &inPaths, const tstring &outFile) const
 Compresses the given files or directories using the specified aliases.
void compress (const std::map< tstring, tstring > &inPaths, std::ostream &outStream) const
 Compresses the given files or directories using the specified aliases.
void compress (const std::vector< std::pair< tstring, tstring > > &inPaths, const tstring &outFile) const
 Compresses the given files or directories using the specified aliases.
void compress (const std::vector< tstring > &inPaths, const tstring &outFile) const
 Compresses the given files or directories.
void compress (const std::vector< tstring > &inPaths, std::ostream &outStream) const
 Compresses the given files or directories.
void compressDirectory (const tstring &inDir, const tstring &outFile) const
 Compresses an entire directory.
void compressDirectoryContents (const tstring &inDir, const tstring &outFile, bool recursive=true, const tstring &filter="*") const
 Compresses the contents of a directory.
void compressFile (const tstring &inFile, const tstring &outFile, const tstring &inputName={}) const
 Compresses a single file.
void compressFiles (const std::vector< tstring > &inFiles, const tstring &outFile) const
 Compresses a group of files.
void compressFiles (const tstring &inDir, const tstring &outFile, bool recursive=true, const tstring &filter="*") const
 Compresses the files contained in a directory.
auto compressionFormat () const noexcept -> const BitInOutFormat &
auto compressionLevel () const noexcept -> BitCompressionLevel
auto compressionMethod () const noexcept -> BitCompressionMethod
auto cryptHeaders () const noexcept -> bool
auto dictionarySize () const noexcept -> std::uint32_t
auto fileCallback () const -> const FileCallback &
auto format () const noexcept -> const BitInFormat &override
auto isPasswordDefined () const noexcept -> bool
auto library () const noexcept -> const Bit7zLibrary &
auto overwriteMode () const noexcept -> OverwriteMode
auto password () const -> const tstring &
auto passwordCallback () const -> const PasswordCallback &
auto progressCallback () const -> const ProgressCallback &
auto ratioCallback () const -> const RatioCallback &
auto retainDirectories () const noexcept -> bool
void setCompressionLevel (BitCompressionLevel level) noexcept
 Sets the compression level to be used when creating/updating an archive.
void setCompressionMethod (BitCompressionMethod method)
 Sets the compression method to be used when creating/updating an archive.
void setDictionarySize (std::uint32_t dictionarySize)
 Sets the dictionary size to be used when creating/updating an archive.
void setFileCallback (const FileCallback &callback)
 Sets the function to be called when the current file being processed changes.
void setFormatProperty (const wchar_t(&name)[N], const T &value) noexcept
 Sets a property for the output archive format as described by the 7-zip documentation (e.g., https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm).
void setFormatProperty (const wchar_t(&name)[N], T value) noexcept
 Sets a property for the output archive format as described by the 7-zip documentation (e.g., https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm).
void setOverwriteMode (OverwriteMode mode)
 Sets how the handler should behave when it tries to output to an existing file or buffer.
void setPassword (const tstring &password) override
 Sets up a password for the output archives.
void setPassword (const tstring &password, bool cryptHeaders)
 Sets up a password for the output archive.
void setPassword (const tstring &password, EncryptionScope scope)
 Sets up a password for the output archive, specifying the encryption scope.
void setPasswordCallback (const PasswordCallback &callback)
 Sets the function to be called when a password is needed to complete the ongoing operation.
void setProgressCallback (const ProgressCallback &callback)
 Sets the function to be called when the processed size of the ongoing operation is updated.
void setRatioCallback (const RatioCallback &callback)
 Sets the function to be called when the input processed size and current output size of the ongoing operation are known.
void setRetainDirectories (bool retain) noexcept
 Sets whether the operations' output will preserve the input's directory structure or not.
void setSolidMode (bool solidMode) noexcept
 Sets whether to use solid compression or not.
void setStoreCreationTime (bool storeCreationTime) noexcept
 Sets whether the creator will store creation timestamps of items.
void setStoreLastAccessTime (bool storeLastAccessTime) noexcept
 Sets whether the creator will store last access timestamps of items.
void setStoreLastWriteTime (bool storeLastWriteTime) noexcept
 Sets whether the creator will store last write timestamps of items.
void setStoreOpenFiles (bool storeOpenFiles) noexcept
 Sets whether the creator will attempt to compress files that are locked by other processes.
void setStoreSymbolicLinks (bool storeSymlinks) noexcept
 Sets whether the creator will store symbolic links as links in the output archive.
void setThreadsCount (std::uint32_t threadsCount) noexcept
 Sets the number of threads to be used when creating/updating an archive.
void setTotalCallback (const TotalCallback &callback)
 Sets the function to be called when the total size of an operation is available.
void setUpdateMode (bool canUpdate)
 Sets whether the creator can update existing archives or not.
virtual void setUpdateMode (UpdateMode mode)
 Sets whether and how the creator can update existing archives or not.
void setVolumeSize (std::uint64_t volumeSize) noexcept
 Sets the volumeSize (in bytes) of the output archive volumes.
void setWordSize (std::uint32_t wordSize)
 Sets the word size to be used when creating/updating an archive.
auto solidMode () const noexcept -> bool
auto storeCreationTime () const noexcept -> bool
auto storeLastAccessTime () const noexcept -> bool
auto storeLastWriteTime () const noexcept -> bool
auto storeOpenFiles () const noexcept -> bool
auto storeSymbolicLinks () const noexcept -> bool
auto threadsCount () const noexcept -> std::uint32_t
auto totalCallback () const -> const TotalCallback &
auto updateMode () const noexcept -> UpdateMode
auto volumeSize () const noexcept -> std::uint64_t
auto wordSize () const noexcept -> std::uint32_t

Detailed Description

The BitFileCompressor class allows compressing files and directories.

The compressed archives can be saved to the filesystem, standard streams, or memory buffers.

It let decide various properties of the produced archive, such as the password protection and the compression level desired.

Constructor & Destructor Documentation

◆ BitFileCompressor()

BitFileCompressor ( const Bit7zLibrary & lib,
const BitInOutFormat & format )

Constructs a BitFileCompressor object.

The Bit7zLibrary parameter is needed to have access to the functionalities of the 7z DLLs. On the contrary, the BitInOutFormat is required to know the format of the output archive.

Parameters
libthe 7z library used.
formatthe output archive format.

Member Function Documentation

◆ clearPassword()

void clearPassword ( )
noexceptinherited

Clear the current password used by the handler.

Calling clearPassword() will disable the encryption/decryption of archives.

Note
This is equivalent to calling setPassword(L"").

◆ compress() [1/5]

void compress ( const std::map< tstring, tstring > & inPaths,
const tstring & outFile ) const

Compresses the given files or directories using the specified aliases.

The items in the first argument must be the relative or absolute paths to files or directories existing on the filesystem. Each pair in the map must follow the following format: {"path to file in the filesystem", "alias path in the archive"}.

Parameters
inPathsa map of paths and corresponding aliases.
outFilethe path (relative or absolute) to the output archive file.

◆ compress() [2/5]

void compress ( const std::map< tstring, tstring > & inPaths,
std::ostream & outStream ) const

Compresses the given files or directories using the specified aliases.

The items in the first argument must be the relative or absolute paths to files or directories existing on the filesystem. Each pair in the map must follow the following format: {"path to file in the filesystem", "alias path in the archive"}.

Parameters
inPathsa map of paths and corresponding aliases.
outStreamthe standard ostream where to output the archive file.

◆ compress() [3/5]

void compress ( const std::vector< std::pair< tstring, tstring > > & inPaths,
const tstring & outFile ) const

Compresses the given files or directories using the specified aliases.

The items in the first argument must be the relative or absolute paths to files or directories existing on the filesystem. Each pair in the vector must follow the following format: {"path to file in the filesystem", "alias path in the archive"}.

Parameters
inPathsa vector of <filesystem path, in-archive path> pairs.
outFilethe path (relative or absolute) to the output archive file.

◆ compress() [4/5]

void compress ( const std::vector< tstring > & inPaths,
const tstring & outFile ) const

Compresses the given files or directories.

The items in the first argument must be the relative or absolute paths to files or directories existing on the filesystem.

Parameters
inPathsa vector of paths.
outFilethe path (relative or absolute) to the output archive file.

◆ compress() [5/5]

void compress ( const std::vector< tstring > & inPaths,
std::ostream & outStream ) const

Compresses the given files or directories.

The items in the first argument must be the relative or absolute paths to files or directories existing on the filesystem.

Parameters
inPathsa vector of paths.
outStreamthe standard ostream where the archive will be output.

◆ compressDirectory()

void compressDirectory ( const tstring & inDir,
const tstring & outFile ) const

Compresses an entire directory.

Note
This method is equivalent to compressFiles with filter set to L"".
Parameters
inDirthe path (relative or absolute) to the input directory.
outFilethe path (relative or absolute) to the output archive file.

◆ compressDirectoryContents()

void compressDirectoryContents ( const tstring & inDir,
const tstring & outFile,
bool recursive = true,
const tstring & filter = "*" ) const

Compresses the contents of a directory.

Note
Unlike compressFiles, this method includes also the metadata of the sub-folders.
Parameters
inDirthe path (relative or absolute) to the input directory.
outFilethe path (relative or absolute) to the output archive file.
recursive(optional) if true, it searches the contents inside the sub-folders of inDir.
filter(optional) the filter to use when searching the contents inside inDir.

◆ compressFile()

void compressFile ( const tstring & inFile,
const tstring & outFile,
const tstring & inputName = {} ) const
inlineinherited

Compresses a single file.

Parameters
inFilethe file to be compressed.
outFilethe path (relative or absolute) to the output archive file.
inputName(optional) the name to give to the compressed file inside the output archive.

◆ compressFiles() [1/2]

void compressFiles ( const std::vector< tstring > & inFiles,
const tstring & outFile ) const

Compresses a group of files.

Note
Any path to a directory or to a not-existing file will be ignored.
Parameters
inFilesthe path (relative or absolute) to the input files.
outFilethe path (relative or absolute) to the output archive file.

◆ compressFiles() [2/2]

void compressFiles ( const tstring & inDir,
const tstring & outFile,
bool recursive = true,
const tstring & filter = "*" ) const

Compresses the files contained in a directory.

Parameters
inDirthe path (relative or absolute) to the input directory.
outFilethe path (relative or absolute) to the output archive file.
recursive(optional) if true, it searches files inside the subfolders of inDir.
filter(optional) the filter to use when searching files inside inDir.

◆ compressionFormat()

auto compressionFormat ( ) const -> const BitInOutFormat &
noexceptinherited
Returns
the format used for creating/updating an archive.

◆ compressionLevel()

auto compressionLevel ( ) const -> BitCompressionLevel
noexceptinherited
Returns
the compression level used for creating/updating an archive.

◆ compressionMethod()

auto compressionMethod ( ) const -> BitCompressionMethod
noexceptinherited
Returns
the compression method used for creating/updating an archive.

◆ cryptHeaders()

auto cryptHeaders ( ) const -> bool
noexceptinherited
Returns
whether the creator crypts also the headers of archives or not.

◆ dictionarySize()

auto dictionarySize ( ) const -> std::uint32_t
noexceptinherited
Returns
the dictionary size used for creating/updating an archive.

◆ fileCallback()

auto fileCallback ( ) const -> const FileCallback &
inherited
Returns
the current file callback.

◆ format()

auto format ( ) const -> const BitInFormat &
overridenoexceptinherited
Returns
the format used for creating/updating an archive.

◆ isPasswordDefined()

auto isPasswordDefined ( ) const -> bool
noexceptinherited
Returns
a boolean value indicating whether a password is defined or not.

◆ library()

auto library ( ) const -> const Bit7zLibrary &
noexceptinherited
Returns
the Bit7zLibrary object used by the handler.

◆ overwriteMode()

auto overwriteMode ( ) const -> OverwriteMode
noexceptinherited
Returns
the current OverwriteMode.

◆ password()

auto password ( ) const -> const tstring &
inherited
Returns
the password used to open, extract, or encrypt the archive.

◆ passwordCallback()

auto passwordCallback ( ) const -> const PasswordCallback &
inherited
Returns
the current password callback.

◆ progressCallback()

auto progressCallback ( ) const -> const ProgressCallback &
inherited
Returns
the current progress callback.

◆ ratioCallback()

auto ratioCallback ( ) const -> const RatioCallback &
inherited
Returns
the current ratio callback.

◆ retainDirectories()

auto retainDirectories ( ) const -> bool
noexceptinherited
Returns
a boolean value indicating whether the directory structure must be preserved while extracting or compressing the archive.

◆ setCompressionLevel()

void setCompressionLevel ( BitCompressionLevel level)
noexceptinherited

Sets the compression level to be used when creating/updating an archive.

Parameters
levelthe compression level desired.

◆ setCompressionMethod()

void setCompressionMethod ( BitCompressionMethod method)
inherited

Sets the compression method to be used when creating/updating an archive.

Parameters
methodthe compression method desired.

◆ setDictionarySize()

void setDictionarySize ( std::uint32_t dictionarySize)
inherited

Sets the dictionary size to be used when creating/updating an archive.

Parameters
dictionarySizethe dictionary size desired.

◆ setFileCallback()

void setFileCallback ( const FileCallback & callback)
inherited

Sets the function to be called when the current file being processed changes.

Parameters
callbackthe file callback to be used.

◆ setFormatProperty() [1/2]

void setFormatProperty ( const wchar_t(&) name[N],
const T & value )
inlinenoexceptinherited

Sets a property for the output archive format as described by the 7-zip documentation (e.g., https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm).

For example, passing the string L"tm" with a false value while creating a .7z archive will disable storing the last modified timestamps of the compressed files.

Template Parameters
TA non-integral type (i.e., a string).
Parameters
nameThe string name of the property to be set.
valueThe value to be used for the property.

◆ setFormatProperty() [2/2]

void setFormatProperty ( const wchar_t(&) name[N],
T value )
inlinenoexceptinherited

Sets a property for the output archive format as described by the 7-zip documentation (e.g., https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm).

Template Parameters
TAn integral type (i.e., a bool or an integer type).
Parameters
nameThe string name of the property to be set.
valueThe value to be used for the property.

◆ setOverwriteMode()

void setOverwriteMode ( OverwriteMode mode)
inherited

Sets how the handler should behave when it tries to output to an existing file or buffer.

Parameters
modethe OverwriteMode to be used by the handler.

◆ setPassword() [1/3]

void setPassword ( const tstring & password)
overrideinherited

Sets up a password for the output archives.

When setting a password, the produced archives will be encrypted using the default cryptographic method of the output format. The option "crypt headers" remains unchanged, in contrast with what happens when calling the setPassword(tstring, bool) method.

Note
Calling setPassword when the output format doesn't support archive encryption (e.g., GZip, BZip2, etc...) does not have any effects (in other words, it doesn't throw exceptions, and it has no effects on compression operations).
After a password has been set, it will be used for every subsequent operation. To disable the use of the password, you need to call the clearPassword method (inherited from BitAbstractArchiveHandler), which is equivalent to setPassword(L"").
Parameters
passwordthe password to be used when creating/updating archives.

◆ setPassword() [2/3]

void setPassword ( const tstring & password,
bool cryptHeaders )
inherited

Sets up a password for the output archive.

When setting a password, the produced archive will be encrypted using the default cryptographic method of the output format. If the format is 7z, and the option "cryptHeaders" is set to true, the headers of the archive will be encrypted, resulting in a password request every time the output file will be opened.

Note
Calling setPassword when the output format doesn't support archive encryption (e.g., GZip, BZip2, etc...) does not have any effects (in other words, it doesn't throw exceptions, and it has no effects on compression operations).
Calling setPassword with "cryptHeaders" set to true does not have effects on formats different from 7z.
After a password has been set, it will be used for every subsequent operation. To disable the use of the password, you need to call the clearPassword method (inherited from BitAbstractArchiveHandler), which is equivalent to setPassword(L"").
Parameters
passwordthe password to be used when creating/updating archives.
cryptHeadersif true, the headers of the output archives will be encrypted (valid only when using the 7z format).
Deprecated
Since v4.1. Please use the overloaded function that takes an EncryptionScope enumerator.

◆ setPassword() [3/3]

void setPassword ( const tstring & password,
EncryptionScope scope )
inherited

Sets up a password for the output archive, specifying the encryption scope.

When setting a password, the produced archive will be encrypted using the default cryptographic method of the output format. If the format is 7z and the scope is EncryptionScope::DataAndHeaders, the archive headers will also be encrypted, resulting in a password request every time the output file will be opened.

Note
Calling setPassword when the output format doesn't support archive encryption (e.g., GZip, BZip2, etc...) does not have any effects (in other words, it doesn't throw exceptions, and it has no effects on compression operations).
Using EncryptionScope::DataAndHeaders does not have effects on formats other than 7z.
After a password has been set, it will be used for every subsequent operation. To disable the use of the password, you need to call the clearPassword method (inherited from BitAbstractArchiveHandler), which is equivalent to setPassword(L"").
Parameters
passwordthe password to be used when creating/updating archives.
scopethe scope of encryption; use EncryptionScope::DataAndHeaders to also encrypt the archive headers (valid only for the 7z format).

◆ setPasswordCallback()

void setPasswordCallback ( const PasswordCallback & callback)
inherited

Sets the function to be called when a password is needed to complete the ongoing operation.

Parameters
callbackthe password callback to be used.

◆ setProgressCallback()

void setProgressCallback ( const ProgressCallback & callback)
inherited

Sets the function to be called when the processed size of the ongoing operation is updated.

Note
The completion percentage of the current operation can be obtained by calculating static_cast<int>((100.0 * processed_size) / total_size).
Parameters
callbackthe progress callback to be used.

◆ setRatioCallback()

void setRatioCallback ( const RatioCallback & callback)
inherited

Sets the function to be called when the input processed size and current output size of the ongoing operation are known.

Note
The ratio percentage of a compression operation can be obtained by calculating static_cast<int>((100.0 * output_size) / input_size).
Parameters
callbackthe ratio callback to be used.

◆ setRetainDirectories()

void setRetainDirectories ( bool retain)
noexceptinherited

Sets whether the operations' output will preserve the input's directory structure or not.

Parameters
retainthe setting for preserving or not the input directory structure

◆ setSolidMode()

void setSolidMode ( bool solidMode)
noexceptinherited

Sets whether to use solid compression or not.

Note
Setting the solid compression mode to true has effect only when using the 7z format with multiple input files.
Parameters
solidModeif true, it will be used the "solid compression" method.

◆ setStoreCreationTime()

void setStoreCreationTime ( bool storeCreationTime)
noexceptinherited

Sets whether the creator will store creation timestamps of items.

Parameters
storeCreationTimeif true, creation timestamps of items will be stored in the output archive.

◆ setStoreLastAccessTime()

void setStoreLastAccessTime ( bool storeLastAccessTime)
noexceptinherited

Sets whether the creator will store last access timestamps of items.

Parameters
storeLastAccessTimeif true, last access timestamps of items will be stored in the output archive.

◆ setStoreLastWriteTime()

void setStoreLastWriteTime ( bool storeLastWriteTime)
noexceptinherited

Sets whether the creator will store last write timestamps of items.

Note
By default, all archive formats store last write timestamps; pass false to suppress them.
Parameters
storeLastWriteTimeif false, last write timestamps will be omitted from the output archive.

◆ setStoreOpenFiles()

void setStoreOpenFiles ( bool storeOpenFiles)
noexceptinherited

Sets whether the creator will attempt to compress files that are locked by other processes.

When enabled, the creator opens files with shared read/write access on Windows, which is equivalent to 7-zip's -ssw switch. This allows compressing files that another process has open for writing.

Warning
Compressing a file that is actively being written by another process may produce an incomplete or inconsistent archive entry.
Note
On non-Windows platforms this setting has no effect.
Parameters
storeOpenFilesif true, the creator will attempt to compress files open by other processes.

◆ setStoreSymbolicLinks()

void setStoreSymbolicLinks ( bool storeSymlinks)
noexceptinherited

Sets whether the creator will store symbolic links as links in the output archive.

Parameters
storeSymlinksif true, symbolic links will be stored as links.

◆ setThreadsCount()

void setThreadsCount ( std::uint32_t threadsCount)
noexceptinherited

Sets the number of threads to be used when creating/updating an archive.

Parameters
threadsCountthe number of threads desired.

◆ setTotalCallback()

void setTotalCallback ( const TotalCallback & callback)
inherited

Sets the function to be called when the total size of an operation is available.

Parameters
callbackthe total callback to be used.

◆ setUpdateMode() [1/2]

void setUpdateMode ( bool canUpdate)
inherited

Sets whether the creator can update existing archives or not.

Deprecated
since v4.0. It is provided just for an easier transition from the old v3 API.
Note
If set to false, a subsequent compression operation may throw an exception if it targets an existing archive.
Parameters
canUpdateif true, compressing operations will update existing archives.

◆ setUpdateMode() [2/2]

virtual void setUpdateMode ( UpdateMode mode)
virtualinherited

Sets whether and how the creator can update existing archives or not.

Note
If set to UpdateMode::None, a subsequent compression operation may throw an exception if it targets an existing archive.
Parameters
modethe desired update mode.

◆ setVolumeSize()

void setVolumeSize ( std::uint64_t volumeSize)
noexceptinherited

Sets the volumeSize (in bytes) of the output archive volumes.

Note
This setting has effects only when the destination archive is on the filesystem.
Parameters
volumeSizeThe dimension of a volume.

◆ setWordSize()

void setWordSize ( std::uint32_t wordSize)
inherited

Sets the word size to be used when creating/updating an archive.

Parameters
wordSizethe word size desired.

◆ solidMode()

auto solidMode ( ) const -> bool
noexceptinherited
Returns
whether the archive creator uses solid compression or not.

◆ storeCreationTime()

auto storeCreationTime ( ) const -> bool
noexceptinherited
Returns
true if the creator has been explicitly configured to store creation timestamps of items.

◆ storeLastAccessTime()

auto storeLastAccessTime ( ) const -> bool
noexceptinherited
Returns
true if the creator has been explicitly configured to store last access timestamps of items.

◆ storeLastWriteTime()

auto storeLastWriteTime ( ) const -> bool
noexceptinherited
Returns
true if the creator has been explicitly configured to store last write timestamps of items.

◆ storeOpenFiles()

auto storeOpenFiles ( ) const -> bool
noexceptinherited
Returns
whether the creator will attempt to compress files that are locked by other processes.

◆ storeSymbolicLinks()

auto storeSymbolicLinks ( ) const -> bool
noexceptinherited
Returns
whether the archive creator stores symbolic links as links in the output archive.

◆ threadsCount()

auto threadsCount ( ) const -> std::uint32_t
noexceptinherited
Returns
the number of threads used when creating/updating an archive (a zero value means that it will use the 7-zip default value).

◆ totalCallback()

auto totalCallback ( ) const -> const TotalCallback &
inherited
Returns
the current total callback.

◆ updateMode()

auto updateMode ( ) const -> UpdateMode
noexceptinherited
Returns
the update mode used when updating existing archives.

◆ volumeSize()

auto volumeSize ( ) const -> std::uint64_t
noexceptinherited
Returns
the volume size (in bytes) used when creating multi-volume archives (a zero value means that all files are going in a single archive).

◆ wordSize()

auto wordSize ( ) const -> std::uint32_t
noexceptinherited
Returns
the word size used for creating/updating an archive.

The documentation for this class was generated from the following file: