bit7z 4.1.0
A C++ library for interfacing with the 7-zip shared libs.
Loading...
Searching...
No Matches
bitinputarchive.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#ifndef BITINPUTARCHIVE_HPP
10#define BITINPUTARCHIVE_HPP
11
12#include "bit7zlibrary.hpp"
13#include "bitabstractarchivehandler.hpp"
14#include "bitarchiveitemoffset.hpp"
15#include "bitdefines.hpp"
16#include "bitformat.hpp"
17#include "bitfs.hpp"
18#include "bitindicesview.hpp"
19#include "bitpropvariant.hpp"
20#include "bittypes.hpp"
21#include "bitwindows.hpp"
22
23#include <array>
24#include <cstddef>
25#include <cstdint>
26#include <iterator>
27#include <istream>
28#include <map>
29#include <ostream>
30#include <vector>
31
32struct IInStream;
33struct IInArchive;
34struct IOutArchive;
35struct ISequentialInStream;
36
37namespace bit7z {
38
39class BufferQueue;
40class ExtractCallback;
41class OpenCallback;
42
51
60
65 public:
77 const tstring& inFile,
79 );
80
92 const bit7zfs::path& arcPath,
94 );
95
107 const buffer_t& inBuffer,
109 );
110
122 std::istream& inStream,
124 );
125
126 BitInputArchive( const BitInputArchive& ) = delete;
127
128 BitInputArchive( BitInputArchive&& ) = delete;
129
130 auto operator=( const BitInputArchive& ) -> BitInputArchive& = delete;
131
132 auto operator=( BitInputArchive&& ) -> BitInputArchive& = delete;
133
134 virtual ~BitInputArchive();
135
139 BIT7Z_NODISCARD auto detectedFormat() const noexcept -> const BitInFormat&;
140
148 BIT7Z_NODISCARD auto archiveProperty( BitProperty property ) const -> BitPropVariant;
149
159 BIT7Z_NODISCARD auto itemProperty( std::uint32_t index, BitProperty property ) const -> BitPropVariant;
160
169 BIT7Z_NODISCARD auto itemHasProperty( std::uint32_t index, BitProperty property ) const -> bool;
170
174 BIT7Z_NODISCARD auto itemsCount() const -> std::uint32_t;
175
181 BIT7Z_NODISCARD auto isItemFolder( std::uint32_t index ) const -> bool;
182
188 BIT7Z_NODISCARD auto isItemEncrypted( std::uint32_t index ) const -> bool;
189
193 BIT7Z_NODISCARD auto archivePath() const noexcept -> const tstring&;
194
198 BIT7Z_NODISCARD auto archiveHasPath() const noexcept -> bool;
199
213 BIT7Z_NODISCARD auto rootFolder() const -> tstring;
214
218 BIT7Z_NODISCARD auto handler() const noexcept -> const BitAbstractArchiveHandler&;
219
226 void useFormatProperty( const wchar_t* name, const BitPropVariant& property ) const;
227
235 template<
236 typename T,
237 typename = typename std::enable_if< is_explicitly_convertible< T, BitPropVariant >::value >::type
238 >
239 void useFormatProperty( const wchar_t* name, T&& value ) const { // NOLINT(*-avoid-c-arrays)
241 }
242
243 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
244 void extract( const tstring& outDir, const IndicesVector& indices = {} ) const {
245 extractTo( outDir, indices );
246 }
247
254 void extractTo( const tstring& outDir, BitIndicesView indices = {} ) const;
255
264 const tstring& outDir,
265 const tstring& itemFilter,
267 ) const;
268
269#ifdef BIT7Z_REGEX_MATCHING
280 const tstring& outDir,
281 const tstring& regex,
283 ) const;
284
295 const tstring& outDir,
296 const tregex& regex,
298 ) const;
299#endif
300
307 void extractTo( const tstring& outDir, FilterCallback filterCallback ) const;
308
321 void extractTo( const tstring& outDir, RenameCallback renameCallback ) const;
322
323 BIT7Z_DEPRECATED_MSG(
324 "Since v4.1; the RenameCallback now receives the BitArchiveItem being extracted. "
325 "The (index, path) form will be removed in v4.2."
326 )
327 void extractTo( const tstring& outDir, LegacyRenameCallback renameCallback ) const;
328
337 const tstring& outDir,
338 const tstring& folderPath,
340 ) const;
341
353 const tstring& outDir
354 ) const;
355
356 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
357 void extract( buffer_t& outBuffer, std::uint32_t index = 0 ) const {
358 extractTo( outBuffer, index );
359 }
360
367 void extractTo( buffer_t& outBuffer, std::uint32_t index = 0 ) const;
368
377 buffer_t& outBuffer,
378 const tstring& itemFilter,
380 ) const;
381
382#ifdef BIT7Z_REGEX_MATCHING
393 buffer_t& outBuffer,
394 const tstring& regex,
396 ) const;
397
408 buffer_t& outBuffer,
409 const tregex& regex,
411 ) const;
412#endif
413
420 void extractTo( buffer_t& outBuffer, FilterCallback filterCallback ) const;
421
422 template< std::size_t N >
423 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
424 void extract( std::array< byte_t, N >& buffer, std::uint32_t index = 0 ) const {
425 extractTo( buffer.data(), buffer.size(), index );
426 }
427
436 template< std::size_t N >
437 void extractTo( std::array< byte_t, N >& outBuffer, std::uint32_t index = 0 ) const {
438 extractTo( outBuffer.data(), outBuffer.size(), index );
439 }
440
441 template< std::size_t N >
442 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
443 void extract( byte_t (&buffer)[ N ], std::uint32_t index = 0 ) const { // NOLINT(*-avoid-c-arrays)
444 extractTo( buffer, N, index );
445 }
446
455 template< std::size_t N >
456 void extractTo( byte_t (&outBuffer)[ N ], std::uint32_t index = 0 ) const { // NOLINT(*-avoid-c-arrays)
457 extractTo( outBuffer, N, index );
458 }
459
460 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
461 void extract( byte_t* buffer, std::size_t size, std::uint32_t index = 0 ) const {
462 extractTo( buffer, size, index );
463 }
464
473 void extractTo( byte_t* outBuffer, std::size_t size, std::uint32_t index = 0 ) const;
474
475 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
476 void extract( std::ostream& outStream, std::uint32_t index = 0 ) const {
477 extractTo( outStream, index );
478 }
479
486 void extractTo( std::ostream& outStream, std::uint32_t index = 0 ) const;
487
488 BIT7Z_DEPRECATED_MSG( "Since v4.0; please, use the extractTo method." )
489 void extract( std::map< tstring, buffer_t >& outMap ) const {
490 extractTo( outMap );
491 }
492
500
507 void extractTo( BufferCallback callback, BitIndicesView indices = {} ) const;
508
517 void extractTo( RawDataCallback callback, BitIndicesView indices = {} ) const;
518
526 void test( BitIndicesView indices = {} ) const;
527
536 void testItem( std::uint32_t index ) const;
537
541 class ConstIterator {
542 public:
543 // iterator traits
544 using iterator_category BIT7Z_MAYBE_UNUSED = std::input_iterator_tag;
545 using value_type BIT7Z_MAYBE_UNUSED = BitArchiveItemOffset;
546 using reference = const BitArchiveItemOffset&;
547 using pointer = const BitArchiveItemOffset*;
548 using difference_type BIT7Z_MAYBE_UNUSED = std::uint32_t; //so that count_if returns an std::uint32_t
549
555 auto operator++() noexcept -> ConstIterator&;
556
562 auto operator++( int ) noexcept -> ConstIterator; // NOLINT(cert-dcl21-cpp)
563
571 auto operator==( const ConstIterator& other ) const noexcept -> bool;
572
580 auto operator!=( const ConstIterator& other ) const noexcept -> bool;
581
587 auto operator*() const noexcept -> reference;
588
594 auto operator->() const noexcept -> pointer;
595
596 private:
597 BitArchiveItemOffset mItemOffset;
598
599 ConstIterator( std::uint32_t itemIndex, const BitInputArchive& itemArchive ) noexcept;
600
601 friend class BitInputArchive;
602 };
603
604 BIT7Z_DEPRECATED_TYPEDEF( const_iterator, ConstIterator, "Use ConstIterator" );
605
610 BIT7Z_NODISCARD auto begin() const noexcept -> BitInputArchive::ConstIterator;
611
616 BIT7Z_NODISCARD auto end() const noexcept -> BitInputArchive::ConstIterator;
617
622 BIT7Z_NODISCARD auto cbegin() const noexcept -> BitInputArchive::ConstIterator;
623
628 BIT7Z_NODISCARD auto cend() const noexcept -> BitInputArchive::ConstIterator;
629
638 BIT7Z_NODISCARD auto find( const tstring& path ) const noexcept -> BitInputArchive::ConstIterator;
639
648 BIT7Z_NODISCARD auto findByName( const tstring& name ) const noexcept -> BitInputArchive::ConstIterator;
649
657 BIT7Z_NODISCARD auto contains( const tstring& path ) const noexcept -> bool;
658
666 BIT7Z_NODISCARD auto itemAt( std::uint32_t index ) const -> BitArchiveItemOffset;
667
673 BIT7Z_NODISCARD auto mainSubfileIndex() const -> std::uint32_t;
674
675 protected:
676 explicit BitInputArchive(
678 const BitInputArchive& parentArchive,
679 std::uint32_t subfileIndex,
680 ArchiveStartOffset archiveStart
681 );
682
683 BIT7Z_NODISCARD
684 auto initUpdatableArchive( IOutArchive** newArc ) const -> HRESULT;
685
686 void openArchiveSeqStream( ISequentialInStream* inStream ) const;
687
688 BIT7Z_NODISCARD
689 auto getSubfileStream( std::uint32_t index ) const -> CMyComPtr< IInStream >;
690
691 void extractSequentially( BufferQueue& queue, std::uint32_t index ) const;
692
693 void extractArchive( ExtractCallback* callback, std::int32_t mode, BitIndicesView indices = {} ) const;
694
695 BIT7Z_NODISCARD
696 auto isInvalidIndex( std::uint32_t index ) const -> bool;
697
698 BIT7Z_NODISCARD
699 auto findInvalidIndex( BitIndicesView indices ) const -> BitIndicesView::const_iterator;
700
701 BIT7Z_NODISCARD
702 auto close() const noexcept -> HRESULT;
703
704 private:
705 IInArchive* mInArchive;
706 const BitInFormat* mDetectedFormat;
707 const BitAbstractArchiveHandler& mArchiveHandler;
708 tstring mArchivePath;
709
710 explicit BitInputArchive( const BitAbstractArchiveHandler& handler, const BitArchiveItemOffset& nestedItem );
711
712 BIT7Z_NODISCARD
713 auto openArchiveStream(
714 const bit7zfs::path& name,
715 IInStream* inStream,
716 ArchiveStartOffset startOffset
717 ) -> IInArchive*;
718
719#ifdef BIT7Z_AUTO_FORMAT
720 BIT7Z_NODISCARD
721 auto tryOpenSfxArchive(
722 IInStream* inStream,
723 OpenCallback* openCallback,
724 const bit7zfs::path& name
725 ) -> IInArchive*;
726#endif
727
728 void testArchive( BitIndicesView indices ) const;
729
741 BIT7Z_NODISCARD auto itemAtUnchecked( std::uint32_t index ) const -> BitArchiveItemOffset;
742
743 friend class ExtractCallback;
744
745 friend class BitAbstractArchiveOpener;
746
747 friend class BitAbstractArchiveCreator;
748
749 friend class BitNestedArchiveReader;
750
751 friend class BitOutputArchive;
752
753 friend class CSynchronizedInStream;
754};
755
756} // namespace bit7z
757
758#endif //BITINPUTARCHIVE_HPP
Abstract class representing a generic archive creator.
Definition bitabstractarchivecreator.hpp:57
Abstract class representing a generic archive handler.
Definition bitabstractarchivehandler.hpp:120
The BitAbstractArchiveOpener abstract class represents a generic archive opener.
Definition bitabstractarchiveopener.hpp:24
The BitArchiveItemOffset class represents an archived item but doesn't store its properties.
Definition bitarchiveitemoffset.hpp:27
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
An iterator for the elements contained in an archive.
Definition bitinputarchive.hpp:541
auto operator++() noexcept -> ConstIterator &
Advances the iterator to the next element in the archive.
The BitInputArchive class, given a handler object, allows reading/extracting the content of archives.
Definition bitinputarchive.hpp:64
void useFormatProperty(const wchar_t *name, const BitPropVariant &property) const
Use the given format property to read the archive.
auto itemProperty(std::uint32_t index, BitProperty property) const -> BitPropVariant
Gets the specified property of an item in the archive.
auto end() const noexcept -> BitInputArchive::ConstIterator
auto archivePath() const noexcept -> const tstring &
void extractRootFolderContentTo(const tstring &outDir) const
Extracts the content of the archive's root folder to the chosen directory.
void extractTo(const tstring &outDir, FilterCallback filterCallback) const
Extracts to the output directory all the items that satisfy the given filtering criteria.
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.
auto find(const tstring &path) const noexcept -> BitInputArchive::ConstIterator
Find an item in the archive that has the given path.
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.
auto isItemEncrypted(std::uint32_t index) const -> bool
void extractMatchingTo(buffer_t &outBuffer, const tregex &regex, FilterPolicy policy=FilterPolicy::Include) const
Extracts to the output buffer the first file in the archive that matches the given regex pattern.
void extractTo(std::map< tstring, buffer_t > &outMap) const
Extracts the content of the archive to a map of memory buffers, where the keys are the paths of the f...
void extractTo(BufferCallback callback, BitIndicesView indices={}) const
Extracts the content of the archive to the buffers provided by the given BufferCallback.
BitInputArchive(const BitAbstractArchiveHandler &handler, const buffer_t &inBuffer, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the archive given in the input buffer.
void extractMatchingTo(buffer_t &outBuffer, const tstring &itemFilter, FilterPolicy policy=FilterPolicy::Include) const
Extracts to the output buffer the first file whose path matches the given wildcard 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.
void extractTo(buffer_t &outBuffer, std::uint32_t index=0) const
Extracts a file to the output buffer.
auto begin() const noexcept -> BitInputArchive::ConstIterator
BitInputArchive(const BitAbstractArchiveHandler &handler, const tstring &inFile, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the input file archive.
auto mainSubfileIndex() const -> std::uint32_t
void extractTo(byte_t(&outBuffer)[N], std::uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitinputarchive.hpp:456
auto itemsCount() const -> std::uint32_t
auto cbegin() const noexcept -> BitInputArchive::ConstIterator
void extractTo(byte_t *outBuffer, std::size_t size, std::uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
void extractTo(buffer_t &outBuffer, FilterCallback filterCallback) const
Extracts to the output buffer the first item satisfying the given filtering criteria.
auto contains(const tstring &path) const noexcept -> bool
Find if there is an item in the archive that has the given path.
auto archiveHasPath() const noexcept -> bool
auto rootFolder() const -> tstring
Returns the single top-level folder that contains all of the archive's items.
void extractMatchingTo(const tstring &outDir, const tregex &regex, FilterPolicy policy=FilterPolicy::Include) const
Extracts to the output directory all the items whose paths match the given regex pattern.
auto findByName(const tstring &name) const noexcept -> BitInputArchive::ConstIterator
Find an item in the archive that has the given name (last component of its path within the archive).
void extractTo(RawDataCallback callback, BitIndicesView indices={}) const
Extracts the raw content of the archive to the given callback.
void extractTo(const tstring &outDir, RenameCallback renameCallback) const
Extracts the archive to the chosen directory, specifying the names of the extracted items via a Renam...
auto isItemFolder(std::uint32_t index) const -> bool
void extractTo(std::ostream &outStream, std::uint32_t index=0) const
Extracts a file to the output stream.
auto itemAt(std::uint32_t index) const -> BitArchiveItemOffset
Retrieve the item at the given index.
void testItem(std::uint32_t index) const
Tests the archive item at the given index without extracting it.
auto detectedFormat() const noexcept -> const BitInFormat &
BitInputArchive(const BitAbstractArchiveHandler &handler, std::istream &inStream, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the archive by reading the given input stream.
auto archiveProperty(BitProperty property) const -> BitPropVariant
Gets the specified archive property.
auto handler() const noexcept -> const BitAbstractArchiveHandler &
auto itemHasProperty(std::uint32_t index, BitProperty property) const -> bool
Checks whether the item at the given index has the specified property.
BitInputArchive(const BitAbstractArchiveHandler &handler, const bit7zfs::path &arcPath, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the input file archive.
auto cend() const noexcept -> BitInputArchive::ConstIterator
void extractTo(std::array< byte_t, N > &outBuffer, std::uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitinputarchive.hpp:437
void extractMatchingRegexTo(buffer_t &outBuffer, const tstring &regex, FilterPolicy policy=FilterPolicy::Include) const
Extracts to the output buffer the first file in the archive that matches the given regex pattern.
The BitNestedArchiveReader class allows reading and extracting nested archives (e....
Definition bitnestedarchivereader.hpp:23
The BitOutputArchive class, given a creator object, allows creating new archives.
Definition bitoutputarchive.hpp:65
T forward(T... args)
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:29
std::vector< std::uint32_t > IndicesVector
A dynamically-sized vector of archive item indices.
Definition bitindicesview.hpp:25
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
ArchiveStartOffset
Offset from where the archive starts within the input file.
Definition bitinputarchive.hpp:46
@ None
Don't specify an archive start offset.
Definition bitinputarchive.hpp:47
@ FileStart
Check only the file start for the archive's start.
Definition bitinputarchive.hpp:49
BitProperty
The BitProperty enum represents the archive/item properties that 7-zip can read or write.
Definition bitpropvariant.hpp:32
@ None
The creator will throw an exception (unless the OverwriteMode is not None).
Definition bitabstractarchivecreator.hpp:40
std::basic_regex< tchar > tregex
Definition bittypes.hpp:111
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
std::integral_constant< bool, std::is_constructible< To, From >::value && !std::is_convertible< From, To >::value > is_explicitly_convertible
Type trait that is true if and only if From is explicitly (but not implicitly) convertible to To.
Definition bittypes.hpp:239
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
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
@ KeepName
Preserve the folder name in the extracted path.
Definition bitinputarchive.hpp:57
@ KeepPath
Preserve the full folder path in the extracted path.
Definition bitinputarchive.hpp:58
The BitPropVariant struct is a light extension to the WinAPI PROPVARIANT struct providing useful gett...
Definition bitpropvariant.hpp:161