bit7z 4.1.0
A C++ library for interfacing with the 7-zip shared libs.
Loading...
Searching...
No Matches
bitextractor.hpp
1/*
2 * bit7z - A C++ static library to interface with the 7-zip shared libraries.
3 * Copyright (c) Riccardo Ostani - All Rights Reserved.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
8 */
9
10#ifndef BITEXTRACTOR_HPP
11#define BITEXTRACTOR_HPP
12
13#include "bit7zlibrary.hpp"
14#include "bitabstractarchivehandler.hpp"
15#include "bitabstractarchiveopener.hpp"
16#include "biterror.hpp"
17#include "bitexception.hpp"
18#include "bitformat.hpp"
19#include "bitinputarchive.hpp"
20#include "bittypes.hpp"
21
22#include <algorithm>
23#include <cstdint>
24#include <functional>
25#include <map>
26#include <ostream>
27#include <vector>
28
29#ifdef BIT7Z_REGEX_MATCHING
30#include <regex>
31#endif
32
33namespace bit7z {
34
35namespace filesystem { // NOLINT(modernize-concat-nested-namespaces)
36 namespace fsutil {
37 auto wildcardMatch( const tstring& pattern, const tstring& str ) -> bool;
38 } // namespace fsutil
39} // namespace filesystem
40
46template< typename Input >
47class BitExtractor final : public BitAbstractArchiveOpener {
48 public:
64 explicit BitExtractor( const Bit7zLibrary& lib, const BitInFormat& format BIT7Z_DEFAULT_FORMAT )
65 : BitAbstractArchiveOpener( lib, format ) {}
66
73 void extract( Input inArchive, const tstring& outDir = {} ) const {
74 const BitInputArchive inputArchive( *this, inArchive );
75 inputArchive.extractTo( outDir );
76 }
77
91 void extract( Input inArchive, const tstring& outDir, RenameCallback callback ) const {
92 const BitInputArchive inputArchive( *this, inArchive );
93 inputArchive.extractTo( outDir, std::move( callback ) );
94 }
95
96 BIT7Z_DEPRECATED_MSG(
97 "Since v4.1; the RenameCallback now receives the BitArchiveItem being extracted. "
98 "The (index, path) form will be removed in v4.2."
99 )
100 void extract( Input inArchive, const tstring& outDir, LegacyRenameCallback callback ) const {
101 const BitInputArchive inputArchive( *this, inArchive );
102 inputArchive.extractTo(
103 outDir,
104 // Init-capture (to move the callback into the lambda) requires C++14;
105 // under C++11 we fall back to capturing the (copyable) std::function by copy.
106#if BIT7Z_CPP_STANDARD >= 14
107 [ callback = std::move( callback ) ]( const BitArchiveItem& item ) -> tstring {
108#else
109 [ callback ]( const BitArchiveItem& item ) -> tstring {
110#endif
111 return callback( item.index(), item.path() );
112 }
113 );
114 }
115
125 Input inArchive,
126 const tstring& outDir,
127 const tstring& folderPath,
129 ) const {
130 const BitInputArchive inputArchive( *this, inArchive );
131 inputArchive.extractFolderTo( outDir, folderPath, policy );
132 }
133
146 Input inArchive,
147 const tstring& outDir
148 ) const {
149 const BitInputArchive inputArchive( *this, inArchive );
150 inputArchive.extractRootFolderContentTo( outDir );
151 }
152
160 void extract( Input inArchive, buffer_t& outBuffer, std::uint32_t index = 0 ) const {
161 const BitInputArchive inputArchive( *this, inArchive );
162 inputArchive.extractTo( outBuffer, index );
163 }
164
174 template< std::size_t N >
175 void extract( Input inArchive, std::array< byte_t, N >& outBuffer, std::uint32_t index = 0 ) const {
176 const BitInputArchive inputArchive( *this, inArchive );
177 inputArchive.extractTo( outBuffer, index );
178 }
179
189 template< std::size_t N > // NOLINTNEXTLINE(*-avoid-c-arrays)
190 void extract( Input inArchive, byte_t (&outBuffer)[ N ], std::uint32_t index = 0 ) const {
191 const BitInputArchive inputArchive( *this, inArchive );
192 inputArchive.extractTo( outBuffer, index );
193 }
194
204 void extract( Input inArchive, byte_t* outBuffer, std::size_t size, std::uint32_t index = 0 ) const {
205 const BitInputArchive inputArchive( *this, inArchive );
206 inputArchive.extractTo( outBuffer, size, index );
207 }
208
216 void extract( Input inArchive, std::ostream& outStream, std::uint32_t index = 0 ) const {
217 const BitInputArchive inputArchive( *this, inArchive );
218 inputArchive.extractTo( outStream, index );
219 }
220
228 void extract( Input inArchive, std::map< tstring, buffer_t >& outMap ) const {
229 const BitInputArchive inputArchive( *this, inArchive );
230 inputArchive.extractTo( outMap );
231 }
232
240 void extract( Input inArchive, BufferCallback callback, BitIndicesView indices = {} ) const {
241 const BitInputArchive inputArchive( *this, inArchive );
242 inputArchive.extractTo( std::move( callback ), indices );
243 }
244
254 void extractTo( Input inArchive, RawDataCallback callback, BitIndicesView indices = {} ) const {
255 const BitInputArchive inputArchive( *this, inArchive );
256 inputArchive.extractTo( std::move( callback ), indices );
257 }
258
269 Input inArchive,
270 const tstring& itemFilter,
271 const tstring& outDir = {},
273 ) const {
274 const BitInputArchive inputArchive{ *this, inArchive };
275 inputArchive.extractMatchingTo( outDir, itemFilter, policy );
276 }
277
288 Input inArchive,
289 const tstring& itemFilter,
290 buffer_t& outBuffer,
292 ) const {
293 const BitInputArchive inputArchive{ *this, inArchive };
294 inputArchive.extractMatchingTo( outBuffer, itemFilter, policy );
295 }
296
305 Input inArchive,
306 BitIndicesView indices,
307 const tstring& outDir = {}
308 ) const {
309 if ( indices.empty() ) {
310 throw BitException( "Cannot extract items", make_error_code( BitError::IndicesNotSpecified ) );
311 }
312
313 const BitInputArchive inputArchive( *this, inArchive );
314 inputArchive.extractTo( outDir, indices );
315 }
316
317#ifdef BIT7Z_REGEX_MATCHING
318
331 Input inArchive,
332 const tstring& regex,
333 const tstring& outDir = {},
335 ) const {
336 const BitInputArchive inputArchive{ *this, inArchive };
337 inputArchive.extractMatchingRegexTo( outDir, regex, policy );
338 }
339
352 Input inArchive,
353 const tstring& regex,
354 buffer_t& outBuffer,
356 ) const {
357 const BitInputArchive inputArchive{ *this, inArchive };
358 inputArchive.extractMatchingRegexTo( outBuffer, regex, policy );
359 }
360
361#endif
362
370 void extractIf( Input inArchive, const tstring& outDir, FilterCallback callback ) const {
371 const BitInputArchive inputArchive{ *this, inArchive };
372 inputArchive.extractTo( outDir, std::move( callback ) );
373 }
374
382 void extractIf( Input inArchive, buffer_t& outBuffer, FilterCallback callback ) const {
383 const BitInputArchive inputArchive{ *this, inArchive };
384 inputArchive.extractTo( outBuffer, std::move( callback ) );
385 }
386
395 void test( Input inArchive, BitIndicesView indices = {} ) const {
396 const BitInputArchive inputArchive( *this, inArchive );
397 inputArchive.test( indices );
398 }
399};
400
401} // namespace bit7z
402
403#endif //BITEXTRACTOR_HPP
The Bit7zLibrary class allows accessing the basic functionalities provided by the 7z DLLs.
Definition bit7zlibrary.hpp:55
auto format() const noexcept -> const BitInFormat &override
The BitArchiveItem class represents a generic item inside an archive.
Definition bitarchiveitem.hpp:25
auto path() const -> tstring final
auto index() const noexcept -> std::uint32_t
The BitException class represents a generic exception thrown from the bit7z classes.
Definition bitexception.hpp:54
void extractTo(Input inArchive, RawDataCallback callback, BitIndicesView indices={}) const
Extracts the raw content of the archive to the given callback.
Definition bitextractor.hpp:254
BitExtractor(const Bit7zLibrary &lib, const BitInFormat &format=BitFormat::Auto)
Constructs a BitExtractor object.
Definition bitextractor.hpp:64
void extract(Input inArchive, std::map< tstring, buffer_t > &outMap) const
Extracts the content of the given archive into a map of memory buffers, where the keys are the paths ...
Definition bitextractor.hpp:228
void extractIf(Input inArchive, const tstring &outDir, FilterCallback callback) const
Extracts to the output directory all the items that satisfy the given filtering criteria.
Definition bitextractor.hpp:370
void extract(Input inArchive, BufferCallback callback, BitIndicesView indices={}) const
Extracts the content of the given archive to the buffers provided by the given BufferCallback.
Definition bitextractor.hpp:240
void extractMatching(Input inArchive, const tstring &itemFilter, const tstring &outDir={}, FilterPolicy policy=FilterPolicy::Include) const
Extracts from the archive to the output directory all the items whose paths match the given wildcard ...
Definition bitextractor.hpp:268
void extractMatchingRegex(Input inArchive, const tstring &regex, buffer_t &outBuffer, FilterPolicy policy=FilterPolicy::Include) const
Extracts from the archive to the output buffer the first file whose path matches the given regex patt...
Definition bitextractor.hpp:351
void extractItems(Input inArchive, BitIndicesView indices, const tstring &outDir={}) const
Extracts the specified items from the given archive to the chosen directory.
Definition bitextractor.hpp:304
void extract(Input inArchive, byte_t *outBuffer, std::size_t size, std::uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitextractor.hpp:204
void extract(Input inArchive, buffer_t &outBuffer, std::uint32_t index=0) const
Extracts a file from the given archive to the output buffer.
Definition bitextractor.hpp:160
void extractFolder(Input inArchive, const tstring &outDir, const tstring &folderPath, FolderPathPolicy policy=FolderPathPolicy::Strip) const
Extracts a folder from the archive to the chosen directory.
Definition bitextractor.hpp:124
void extract(Input inArchive, const tstring &outDir, RenameCallback callback) const
Extracts the given archive to the chosen directory, renaming the extracted items using the provided R...
Definition bitextractor.hpp:91
void extract(Input inArchive, std::ostream &outStream, std::uint32_t index=0) const
Extracts a file from the given archive to the output stream.
Definition bitextractor.hpp:216
void extractIf(Input inArchive, buffer_t &outBuffer, FilterCallback callback) const
Extracts to the output buffer the first item satisfying the given filtering criteria.
Definition bitextractor.hpp:382
void test(Input inArchive, BitIndicesView indices={}) const
Tests the given archive without extracting its content.
Definition bitextractor.hpp:395
void extract(Input inArchive, byte_t(&outBuffer)[N], std::uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitextractor.hpp:190
void extractMatching(Input inArchive, const tstring &itemFilter, buffer_t &outBuffer, FilterPolicy policy=FilterPolicy::Include) const
Extracts from the archive to the output buffer the first file whose path matches the given wildcard p...
Definition bitextractor.hpp:287
void extract(Input inArchive, const tstring &outDir={}) const
Extracts the given archive to the chosen directory.
Definition bitextractor.hpp:73
void extract(Input inArchive, std::array< byte_t, N > &outBuffer, std::uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitextractor.hpp:175
void extractMatchingRegex(Input inArchive, const tstring &regex, const tstring &outDir={}, FilterPolicy policy=FilterPolicy::Include) const
Extracts from the archive to the output directory all the items whose paths match the given regex pat...
Definition bitextractor.hpp:330
void extractRootFolderContent(Input inArchive, const tstring &outDir) const
Extracts the content of the archive's root folder to the chosen directory.
Definition bitextractor.hpp:145
The BitInFormat class specifies an extractable archive format.
Definition bitformat.hpp:68
A non-owning, read-only view over a contiguous sequence of archive item indices.
Definition bitindicesview.hpp:44
constexpr auto empty() const noexcept -> bool
Definition bitindicesview.hpp:169
The BitInputArchive class, given a handler object, allows reading/extracting the content of archives.
Definition bitinputarchive.hpp:64
void extractRootFolderContentTo(const tstring &outDir) const
Extracts the content of the archive's root folder to the chosen directory.
void extractMatchingTo(const tstring &outDir, const tstring &itemFilter, FilterPolicy policy=FilterPolicy::Include) const
Extracts to the output directory all the items whose paths match the given wildcard pattern.
void test(BitIndicesView indices={}) const
Tests the archive without extracting its content.
void extractMatchingRegexTo(const tstring &outDir, const tstring &regex, FilterPolicy policy=FilterPolicy::Include) const
Extracts to the output directory all the items whose paths match the given regex pattern.
void extractTo(const tstring &outDir, BitIndicesView indices={}) const
Extracts the specified items to the chosen directory.
void extractFolderTo(const tstring &outDir, const tstring &folderPath, FolderPathPolicy policy=FolderPathPolicy::Strip) const
Extracts a folder from the archive to the chosen directory.
T move(T... args)
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:29
std::function< tstring(std::uint32_t, const tstring &) > LegacyRenameCallback
The (index, path) form of RenameCallback.
Definition bitabstractarchivehandler.hpp:73
unsigned char byte_t
A type representing a byte.
Definition bittypes.hpp:38
auto make_error_code(BitError error) noexcept -> std::error_code
Creates a std::error_code from the given BitError value.
std::function< tstring(const BitArchiveItem &) > RenameCallback
A function returning a new name for the item currently being extracted.
Definition bitabstractarchivehandler.hpp:65
std::function< bool(const byte_t *, std::size_t) > RawDataCallback
A function providing the raw extracted data and its size to the user.
Definition bitabstractarchivehandler.hpp:56
std::basic_string< tchar > tstring
Definition bittypes.hpp:104
std::function< buffer_t &(std::uint32_t, const tstring &) > BufferCallback
A function returning a reference to the buffer where to extract the item at the given index/path.
Definition bitabstractarchivehandler.hpp:78
FilterPolicy
Enumeration representing the policy according to which the archive handler should treat the items tha...
Definition bitabstractarchivehandler.hpp:112
@ Include
Extract/compress the items that match the pattern.
Definition bitabstractarchivehandler.hpp:113
@ IndicesNotSpecified
No item indices were specified.
Definition biterror.hpp:28
std::function< FilterResult(const BitArchiveItem &) > FilterCallback
A function returning what to do with the given archive item.
Definition bitabstractarchivehandler.hpp:95
FolderPathPolicy
Policy controlling how a folder's path is reflected in the extracted items' paths.
Definition bitinputarchive.hpp:55
@ Strip
Remove the folder path from the extracted path.
Definition bitinputarchive.hpp:56