10#ifndef BITEXTRACTOR_HPP
11#define BITEXTRACTOR_HPP
15#include "bitabstractarchiveopener.hpp"
16#include "biterror.hpp"
17#include "bitexception.hpp"
18#include "bitinputarchive.hpp"
24auto wildcard_match(
const tstring& pattern,
const tstring& str ) -> bool;
33template<
typename Input >
62 inputArchive.extractTo( outDir );
74 inputArchive.
extractTo( outBuffer, index );
86 inputArchive.
extractTo( outStream, index );
113 using namespace filesystem;
115 if ( itemFilter.empty() ) {
116 throw BitException(
"Cannot extract items", make_error_code( BitError::FilterNotSpecified ) );
119 extractMatchingFilter( inArchive, outDir, policy, [ &itemFilter ](
const tstring& itemPath ) ->
bool {
120 return fsutil::wildcard_match( itemFilter, itemPath );
136 using namespace filesystem;
138 if ( itemFilter.empty() ) {
139 throw BitException(
"Cannot extract items", make_error_code( BitError::FilterNotSpecified ) );
142 extractMatchingFilter( inArchive, outBuffer, policy,
143 [ &itemFilter ](
const tstring& itemPath ) ->
bool {
144 return fsutil::wildcard_match( itemFilter, itemPath );
157 const tstring& outDir = {} )
const {
158 if ( indices.empty() ) {
159 throw BitException(
"Cannot extract items", make_error_code( BitError::IndicesNotSpecified ) );
162 BitInputArchive inputArchive( *
this, inArchive );
163 uint32_t nItems = inputArchive.itemsCount();
165 const auto findRes = std::find_if( indices.cbegin(),
167 [ &nItems ]( uint32_t index ) ->
bool {
168 return index >= nItems;
170 if ( findRes != indices.cend() ) {
171 throw BitException(
"Cannot extract item at the index " + std::to_string( *findRes ),
172 make_error_code( BitError::InvalidIndex ) );
175 inputArchive.extractTo( outDir, indices );
178#ifdef BIT7Z_REGEX_MATCHING
194 if ( regex.empty() ) {
195 throw BitException(
"Cannot extract items", make_error_code( BitError::FilterNotSpecified ) );
198 const tregex regexFilter( regex, tregex::ECMAScript | tregex::optimize );
199 extractMatchingFilter( inArchive, outDir, policy, [ ®exFilter ](
const tstring& itemPath ) ->
bool {
200 return std::regex_match( itemPath, regexFilter );
218 if ( regex.empty() ) {
219 throw BitException(
"Cannot extract items", make_error_code( BitError::FilterNotSpecified ) );
222 const tregex regexFilter( regex, tregex::ECMAScript | tregex::optimize );
223 return extractMatchingFilter( inArchive, outBuffer, policy,
224 [ ®exFilter ](
const tstring& itemPath ) ->
bool {
225 return std::regex_match( itemPath, regexFilter );
238 void test( Input inArchive )
const {
244 void extractMatchingFilter( Input inArchive,
253 for (
const auto& item : inputArchive ) {
254 const bool itemMatches = filter( item.path() );
255 if ( itemMatches == shouldExtractMatchedItems ) {
261 matchedIndices.push_back( item.index() );
265 if ( matchedIndices.empty() ) {
266 throw BitException(
"Cannot extract items", make_error_code( BitError::NoMatchingItems ) );
269 inputArchive.extractTo( outDir, matchedIndices );
272 void extractMatchingFilter( Input inArchive,
273 vector< byte_t >& outBuffer,
276 BitInputArchive inputArchive( *
this, inArchive );
280 for (
const auto& item : inputArchive ) {
281 const bool itemMatches = filter( item.path() );
282 if ( itemMatches == shouldExtractMatchedItem ) {
285 inputArchive.extractTo( outBuffer, item.index() );
290 throw BitException(
"Failed to extract items", make_error_code( BitError::NoMatchingItems ) );
The Bit7zLibrary class allows accessing the basic functionalities provided by the 7z DLLs.
Definition bit7zlibrary.hpp:56
The BitAbstractArchiveOpener abstract class represents a generic archive opener.
Definition bitabstractarchiveopener.hpp:26
auto format() const noexcept -> const BitInFormat &override
The BitException class represents a generic exception thrown from the bit7z classes.
Definition bitexception.hpp:32
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:30
FilterPolicy
Enumeration representing the policy according to which the archive handler should treat the items tha...
Definition bitabstractarchivehandler.hpp:66
@ Include
Extract/compress the items that match the pattern.
std::basic_regex< tchar > tregex
Definition bittypes.hpp:97
std::basic_string< tchar > tstring
Definition bittypes.hpp:90