Skip to content

Commit

Permalink
Merge branch 'hotfix/v4.0.8' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Aug 16, 2024
2 parents 04d7105 + 07d7ba4 commit 7681075
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cmake_minimum_required( VERSION 3.14 )

project( bit7z
VERSION 4.0.7
VERSION 4.0.8
DESCRIPTION "A C++ static library offering a clean and simple interface to the 7-zip/p7zip shared libraries"
HOMEPAGE_URL "https://github.com/rikyoz/bit7z/" )
set( CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON" )
Expand Down
15 changes: 15 additions & 0 deletions include/bit7z/bitfilecompressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ class BitFileCompressor final : public BitCompressor< const tstring& > {
*/
void compressDirectory( const tstring& inDir, const tstring& outFile ) const;

/**
* @brief Compresses the contents of a directory.
*
* @note Unlike compressFiles, this method includes also the metadata of the sub-folders.
*
* @param inDir the path (relative or absolute) to the input directory.
* @param outFile the path (relative or absolute) to the output archive file.
* @param recursive (optional) if true, it searches the contents inside the sub-folders of inDir.
* @param filter (optional) the filter to use when searching the contents inside inDir.
*/
void compressDirectoryContents( const tstring& inDir,
const tstring& outFile,
bool recursive = true,
const tstring& filter = BIT7Z_STRING( "*" ) ) const;

/* Compression from the file system to standard streams. */

/**
Expand Down
35 changes: 33 additions & 2 deletions include/bit7z/bitoutputarchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,43 @@ class BitOutputArchive {
bool recursive = true );

/**
* @brief Adds all the items inside the given directory path.
* @brief Adds the given directory path and all its content.
*
* @param inDir the directory where to search for items to be added to the output archive.
* @param inDir the path of the directory to be added to the archive.
*/
void addDirectory( const tstring& inDir );

/**
* @brief Adds the contents of the given directory path.
*
* This function iterates through the specified directory and adds its contents
* based on the provided wildcard filter. Optionally, the operation can be
* recursive, meaning it will include subdirectories and their contents.
*
* @param inDir the directory where to search for files to be added to the output archive.
* @param filter the wildcard filter to be used for searching the files.
* @param recursive recursively search the files in the given directory and all of its subdirectories.
*/
void addDirectoryContents( const tstring& inDir, const tstring& filter, bool recursive );

/**
* @brief Adds the contents of the given directory path.
*
* This function iterates through the specified directory and adds its contents
* based on the provided wildcard filter and policy. Optionally, the operation can be
* recursive, meaning it will include subdirectories and their contents.
*
* @param inDir the directory where to search for files to be added to the output archive.
* @param filter (optional) the wildcard filter to be used for searching the files.
* @param recursive (optional) recursively search the files in the given directory
* and all of its subdirectories.
* @param policy (optional) the filtering policy to be applied to the matched items.
*/
void addDirectoryContents( const tstring& inDir,
const tstring& filter = BIT7Z_STRING( "*" ),
FilterPolicy policy = FilterPolicy::Include,
bool recursive = true );

/**
* @brief Compresses all the items added to this object to the specified archive file path.
*
Expand Down
12 changes: 12 additions & 0 deletions src/bitfilecompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ void BitFileCompressor::compressDirectory( const tstring& inDir, const tstring&
outputArchive.compressTo( outFile );
}

void BitFileCompressor::compressDirectoryContents( const tstring& inDir,
const tstring& outFile,
bool recursive,
const tstring& filter ) const {
if ( !compressionFormat().hasFeature( FormatFeatures::MultipleFiles ) ) {
throw BitException( "Cannot compress multiple files", make_error_code( BitError::UnsupportedOperation ) );
}
BitOutputArchive outputArchive{ *this, outFile };
outputArchive.addDirectoryContents( inDir, filter, recursive );
outputArchive.compressTo( outFile );
}

/* from filesystem to stream */

void BitFileCompressor::compress( const std::vector< tstring >& inPaths, std::ostream& outStream ) const {
Expand Down
17 changes: 17 additions & 0 deletions src/bitoutputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ void BitOutputArchive::addDirectory( const tstring& inDir ) {
mNewItemsVector.indexDirectory( tstring_to_path( inDir ), BIT7Z_STRING( "" ), FilterPolicy::Include, options );
}

void BitOutputArchive::addDirectoryContents( const tstring& inDir, const tstring& filter, bool recursive ) {
addDirectoryContents( inDir, filter, FilterPolicy::Include, recursive );
}

void BitOutputArchive::addDirectoryContents( const tstring& inDir,
const tstring& filter,
FilterPolicy policy,
bool recursive ) {
IndexingOptions options{};
options.recursive = recursive;
options.onlyFiles = !recursive;
options.retainFolderStructure = mArchiveCreator.retainDirectories();
options.followSymlinks = !mArchiveCreator.storeSymbolicLinks();
std::error_code error;
mNewItemsVector.indexDirectory( fs::absolute( tstring_to_path( inDir ), error ), filter, policy, options );
}

auto BitOutputArchive::initOutArchive() const -> CMyComPtr< IOutArchive > {
CMyComPtr< IOutArchive > newArc;
if ( mInputArchive == nullptr ) {
Expand Down
11 changes: 10 additions & 1 deletion src/internal/fsindexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ FilesystemIndexer::FilesystemIndexer( FilesystemItem directory,
}
}

namespace {
inline auto countItemsInPath( const fs::path& path ) -> std::size_t {
std::error_code error;
auto begin = fs::recursive_directory_iterator{ path, fs::directory_options::skip_permission_denied, error };
return error ? 0 : static_cast< std::size_t >( std::distance( begin, fs::recursive_directory_iterator{} ) );
}
} // namespace

// NOTE: It indexes all the items whose metadata are needed in the archive to be created!
void FilesystemIndexer::listDirectoryItems( std::vector< std::unique_ptr< GenericInputItem > >& result,
bool recursive ) {
Expand All @@ -51,8 +59,9 @@ void FilesystemIndexer::listDirectoryItems( std::vector< std::unique_ptr< Generi
mDirItem.inArchivePath().filename() != mDirItem.filesystemName();
const bool shouldIncludeMatchedItems = mPolicy == FilterPolicy::Include;

fs::path basePath = mDirItem.filesystemPath();
const fs::path basePath = mDirItem.filesystemPath();
std::error_code error;
result.reserve( result.size() + countItemsInPath( basePath ) );
for ( auto iterator = fs::recursive_directory_iterator{ basePath, fs::directory_options::skip_permission_denied, error };
iterator != fs::recursive_directory_iterator{};
++iterator ) {
Expand Down

0 comments on commit 7681075

Please sign in to comment.