bit7z 4.0.9
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) 2014-2023 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 <array>
13#include <map>
14
15#include "bitabstractarchivehandler.hpp"
16#include "bitarchiveitemoffset.hpp"
17#include "bitformat.hpp"
18#include "bitfs.hpp"
19
20struct IInStream;
21struct IInArchive;
22struct IOutArchive;
23
24namespace bit7z {
25
26using std::vector;
27
32 None,
35};
36
41 public:
52 const tstring& inFile,
54
65 const fs::path& arcPath,
67
78 const buffer_t& inBuffer,
80
91 std::istream& inStream,
93
94 BitInputArchive( const BitInputArchive& ) = delete;
95
96 BitInputArchive( BitInputArchive&& ) = delete;
97
98 auto operator=( const BitInputArchive& ) -> BitInputArchive& = delete;
99
100 auto operator=( BitInputArchive&& ) -> BitInputArchive& = delete;
101
102 virtual ~BitInputArchive();
103
107 BIT7Z_NODISCARD auto detectedFormat() const noexcept -> const BitInFormat&;
108
116 BIT7Z_NODISCARD auto archiveProperty( BitProperty property ) const -> BitPropVariant;
117
127 BIT7Z_NODISCARD auto itemProperty( uint32_t index, BitProperty property ) const -> BitPropVariant;
128
132 BIT7Z_NODISCARD auto itemsCount() const -> uint32_t;
133
139 BIT7Z_NODISCARD auto isItemFolder( uint32_t index ) const -> bool;
140
146 BIT7Z_NODISCARD auto isItemEncrypted( uint32_t index ) const -> bool;
147
151 BIT7Z_NODISCARD auto archivePath() const noexcept -> const tstring&;
152
156 BIT7Z_NODISCARD auto handler() const noexcept -> const BitAbstractArchiveHandler&;
157
164 void useFormatProperty( const wchar_t* name, const BitPropVariant& property ) const;
165
173 template< typename T,
174 typename = typename std::enable_if< is_explicitly_convertible< T, BitPropVariant >::value >::type >
175 void useFormatProperty( const wchar_t* name, T&& value ) const { // NOLINT(*-avoid-c-arrays)
176 useFormatProperty( name, BitPropVariant{ std::forward< T >( value ) } );
177 }
178
179 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
180 inline void extract( const tstring& outDir, const std::vector< uint32_t >& indices = {} ) const {
181 extractTo( outDir, indices );
182 }
183
189 void extractTo( const tstring& outDir ) const;
190
197 void extractTo( const tstring& outDir, const std::vector< uint32_t >& indices ) const;
198
199 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
200 inline void extract( std::vector< byte_t >& outBuffer, uint32_t index = 0 ) const {
201 extractTo( outBuffer, index );
202 }
203
210 void extractTo( std::vector< byte_t >& outBuffer, uint32_t index = 0 ) const;
211
212 template< std::size_t N >
213 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
214 void extract( std::array< byte_t, N >& buffer, uint32_t index = 0 ) const {
215 extractTo( buffer.data(), buffer.size(), index );
216 }
217
226 template< std::size_t N >
227 void extractTo( std::array< byte_t, N >& buffer, uint32_t index = 0 ) const {
228 extractTo( buffer.data(), buffer.size(), index );
229 }
230
231 template< std::size_t N >
232 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
233 void extract( byte_t (& buffer)[N], uint32_t index = 0 ) const { // NOLINT(*-avoid-c-arrays)
234 extractTo( buffer, N, index );
235 }
236
245 template< std::size_t N >
246 void extractTo( byte_t (& buffer)[N], uint32_t index = 0 ) const { // NOLINT(*-avoid-c-arrays)
247 extractTo( buffer, N, index );
248 }
249
250 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
251 inline void extract( byte_t* buffer, std::size_t size, uint32_t index = 0 ) const {
252 extractTo( buffer, size, index );
253 }
254
263 void extractTo( byte_t* buffer, std::size_t size, uint32_t index = 0 ) const;
264
265 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
266 inline void extract( std::ostream& outStream, uint32_t index = 0 ) const {
267 extractTo( outStream, index );
268 }
269
276 void extractTo( std::ostream& outStream, uint32_t index = 0 ) const;
277
278 BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.")
279 inline void extract( std::map< tstring, std::vector< byte_t > >& outMap ) const {
280 extractTo( outMap );
281 }
282
290
296 void test() const;
297
305 void testItem( uint32_t index ) const;
306
307 protected:
308 auto initUpdatableArchive( IOutArchive** newArc ) const -> HRESULT;
309
310 BIT7Z_NODISCARD auto close() const noexcept -> HRESULT;
311
312 friend class BitAbstractArchiveOpener;
313
314 friend class BitAbstractArchiveCreator;
315
316 friend class BitOutputArchive;
317
318 private:
319 IInArchive* mInArchive;
320 const BitInFormat* mDetectedFormat;
321 const BitAbstractArchiveHandler& mArchiveHandler;
322 tstring mArchivePath;
323
324 BIT7Z_NODISCARD
325 auto openArchiveStream( const fs::path& name, IInStream* inStream, ArchiveStartOffset startOffset ) -> IInArchive*;
326
327 public:
332 public:
333 // iterator traits
334 using iterator_category BIT7Z_MAYBE_UNUSED = std::input_iterator_tag;
335 using value_type BIT7Z_MAYBE_UNUSED = BitArchiveItemOffset;
336 using reference = const BitArchiveItemOffset&;
337 using pointer = const BitArchiveItemOffset*;
338 using difference_type BIT7Z_MAYBE_UNUSED = uint32_t; //so that count_if returns an uint32_t
339
345 auto operator++() noexcept -> ConstIterator&;
346
352 auto operator++( int ) noexcept -> ConstIterator; // NOLINT(cert-dcl21-cpp)
353
361 auto operator==( const ConstIterator& other ) const noexcept -> bool;
362
370 auto operator!=( const ConstIterator& other ) const noexcept -> bool;
371
377 auto operator*() const noexcept -> reference;
378
384 auto operator->() const noexcept -> pointer;
385
386 private:
387 BitArchiveItemOffset mItemOffset;
388
389 ConstIterator( uint32_t itemIndex, const BitInputArchive& itemArchive ) noexcept;
390
391 friend class BitInputArchive;
392 };
393
394 BIT7Z_DEPRECATED_TYPEDEF( const_iterator, ConstIterator, "Use ConstIterator" );
395
400 BIT7Z_NODISCARD auto begin() const noexcept -> BitInputArchive::ConstIterator;
401
406 BIT7Z_NODISCARD auto end() const noexcept -> BitInputArchive::ConstIterator;
407
412 BIT7Z_NODISCARD auto cbegin() const noexcept -> BitInputArchive::ConstIterator;
413
418 BIT7Z_NODISCARD auto cend() const noexcept -> BitInputArchive::ConstIterator;
419
428 BIT7Z_NODISCARD auto find( const tstring& path ) const noexcept -> BitInputArchive::ConstIterator;
429
437 BIT7Z_NODISCARD auto contains( const tstring& path ) const noexcept -> bool;
438
446 BIT7Z_NODISCARD auto itemAt( uint32_t index ) const -> BitArchiveItemOffset;
447
448};
449
450} // namespace bit7z
451
452#endif //BITINPUTARCHIVE_HPP
Abstract class representing a generic archive creator.
Definition bitabstractarchivecreator.hpp:44
Abstract class representing a generic archive handler.
Definition bitabstractarchivehandler.hpp:74
The BitAbstractArchiveOpener abstract class represents a generic archive opener.
Definition bitabstractarchiveopener.hpp:26
The BitArchiveItemOffset class represents an archived item but doesn't store its properties.
Definition bitarchiveitemoffset.hpp:22
The BitInFormat class specifies an extractable archive format.
Definition bitformat.hpp:58
An iterator for the elements contained in an archive.
Definition bitinputarchive.hpp:331
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:40
void useFormatProperty(const wchar_t *name, const BitPropVariant &property) const
Use the given format property to read the archive.
BitInputArchive(const BitAbstractArchiveHandler &handler, const fs::path &arcPath, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the input file archive.
auto end() const noexcept -> BitInputArchive::ConstIterator
void test() const
Tests the archive without extracting its content.
auto archivePath() const noexcept -> const tstring &
auto itemProperty(uint32_t index, BitProperty property) const -> BitPropVariant
Gets the specified property of an item in the archive.
auto find(const tstring &path) const noexcept -> BitInputArchive::ConstIterator
Find an item in the archive that has the given path.
void extractTo(std::map< tstring, std::vector< byte_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(std::vector< byte_t > &outBuffer, uint32_t index=0) const
Extracts a file to the output buffer.
BitInputArchive(const BitAbstractArchiveHandler &handler, const buffer_t &inBuffer, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the archive given in the input buffer.
auto isItemEncrypted(uint32_t index) const -> bool
void testItem(uint32_t index) const
Tests the item at the given index inside the archive without extracting it.
auto begin() const noexcept -> BitInputArchive::ConstIterator
void extractTo(const tstring &outDir, const std::vector< uint32_t > &indices) const
Extracts the specified items to the chosen directory.
BitInputArchive(const BitAbstractArchiveHandler &handler, const tstring &inFile, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitInputArchive object, opening the input file archive.
auto cbegin() const noexcept -> BitInputArchive::ConstIterator
void extractTo(const tstring &outDir) const
Extracts the archive to the chosen directory.
auto contains(const tstring &path) const noexcept -> bool
Find if there is an item in the archive that has the given path.
auto itemAt(uint32_t index) const -> BitArchiveItemOffset
Retrieve the item at the given index.
void extractTo(byte_t(&buffer)[N], uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitinputarchive.hpp:246
void extractTo(std::ostream &outStream, uint32_t index=0) const
Extracts a file to the output stream.
void extractTo(byte_t *buffer, std::size_t size, uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
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 itemsCount() const -> uint32_t
void extractTo(std::array< byte_t, N > &buffer, uint32_t index=0) const
Extracts a file to the pre-allocated output buffer.
Definition bitinputarchive.hpp:227
auto cend() const noexcept -> BitInputArchive::ConstIterator
auto isItemFolder(uint32_t index) const -> bool
The BitOutputArchive class, given a creator object, allows creating new archives.
Definition bitoutputarchive.hpp:60
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:30
unsigned char byte_t
A type representing a byte.
Definition bittypes.hpp:36
ArchiveStartOffset
Offset from where the archive starts within the input file.
Definition bitinputarchive.hpp:31
@ None
Don't specify an archive start offset.
@ FileStart
Check only the file start for the archive's start.
BitProperty
The BitProperty enum represents the archive/item properties that 7-zip can read or write.
Definition bitpropvariant.hpp:30
@ None
The creator will throw an exception (unless the OverwriteMode is not None).
The BitPropVariant struct is a light extension to the WinAPI PROPVARIANT struct providing useful gett...
Definition bitpropvariant.hpp:150