bit7z 4.1.0
A C++ library for interfacing with the 7-zip shared libs.
Loading...
Searching...
No Matches
bitoutputarchive.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 BITOUTPUTARCHIVE_HPP
11#define BITOUTPUTARCHIVE_HPP
12
13#include "bitabstractarchivecreator.hpp"
14#include "bitabstractarchivehandler.hpp"
15#include "bitexception.hpp" //for FailedFiles
16#include "bitinputarchive.hpp"
17#include "bititemsvector.hpp"
18#include "bitpropvariant.hpp"
19#include "bittypes.hpp"
20
21#include <cstdint>
22#include <istream>
23#include <map>
24#include <memory>
25#include <ostream>
26#include <set>
27#include <utility>
28#include <vector>
29
31struct ISequentialInStream;
32
33template< typename T >
34class CMyComPtr;
36
37namespace bit7z {
38
39using DeletedItems = std::set< std::uint32_t >;
40
41/* We introduce a strong index type to differentiate between indices in the output
42 * archive (std::uint32_t, as used by the UpdateCallback), and the corresponding indexes
43 * in the input archive (InputIndex). In this way, we avoid implicit conversions
44 * between the two kinds of indices.
45 *
46 * UpdateCallback uses indices in the range [0, BitOutputArchive::itemsCount() - 1]
47 *
48 * Now, if the user doesn't delete any item in the input archive, itemsCount()
49 * is just equal to <n° of items in the input archive> + <n° of newly added items>.
50 * In this case, an InputIndex value is just equal to the index used by UpdateCallback.
51 *
52 * On the contrary, if the user wants to delete an item in the input archive, the value
53 * of an InputIndex may differ from the corresponding UpdateCallback's index.
54 *
55 * Note: given an InputIndex i:
56 * if i < mInputArchiveItemsCount, the item is old (old item in the input archive);
57 * if i >= mInputArchiveItemsCount, the item is new (added by the user); */
58enum class InputIndex : std::uint32_t {}; // NOLINT(*-enum-size)
59
60class UpdateCallback;
61
66 public:
74
89 const tstring& inFile,
91 );
92
107 const buffer_t& inBuffer,
109 );
110
119 buffer_t&& inBuffer,
121 ) = delete;
122
133 std::istream& inStream,
135 );
136
137 BitOutputArchive( const BitOutputArchive& ) = delete;
138
140
141 auto operator=( const BitOutputArchive& ) -> BitOutputArchive& = delete;
142
143 auto operator=( BitOutputArchive&& ) -> BitOutputArchive& = delete;
144
150 void addItems( const std::vector< tstring >& inPaths );
151
160
169
180 auto addFile( const tstring& inFile, const tstring& name = {} ) -> BitInputItem&;
181
190 auto addFile( const buffer_t& inBuffer, const tstring& name ) -> BitInputItem&;
191
198 auto addFile( buffer_t&& inBuffer, const tstring& name ) -> BitInputItem& = delete;
199
209 auto addFile( std::istream& inStream, const tstring& name ) -> BitInputItem&;
210
218 void addFiles( const std::vector< tstring >& inFiles );
219
227 void addFiles( const tstring& inDir, const tstring& filter, bool recursive );
228
239 const tstring& inDir,
240 const tstring& filter = BIT7Z_STRING( "*" ),
242 bool recursive = true
243 );
244
250 void addDirectory( const tstring& inDir );
251
263 void addDirectoryContents( const tstring& inDir, const tstring& filter, bool recursive );
264
279 const tstring& inDir,
280 const tstring& filter = BIT7Z_STRING( "*" ),
282 bool recursive = true
283 );
284
293 void compressTo( const tstring& outFile );
294
300 void compressTo( buffer_t& outBuffer );
301
307 void compressTo( std::ostream& outStream );
308
312 auto itemsCount() const -> std::uint32_t;
313
318 auto handler() const noexcept -> const BitAbstractArchiveHandler&;
319
324 auto creator() const noexcept -> const BitAbstractArchiveCreator&;
325
330
331 protected:
332 virtual auto itemProperty( InputIndex index, BitProperty property ) const -> BitPropVariant;
333
334 virtual auto itemStream( InputIndex index, ISequentialInStream** inStream ) const -> HRESULT;
335
336 virtual auto hasNewData( std::uint32_t index ) const noexcept -> bool;
337
338 virtual auto hasNewProperties( std::uint32_t index ) const noexcept -> bool;
339
340 auto itemInputIndex( std::uint32_t newIndex ) const noexcept -> InputIndex;
341
342 auto outputItemProperty( std::uint32_t index, BitProperty property ) const -> BitPropVariant;
343
344 auto outputItemStream( std::uint32_t index, ISequentialInStream** inStream ) const -> HRESULT;
345
346 auto indexInArchive( std::uint32_t index ) const noexcept -> std::uint32_t;
347
348 auto inputArchive() const -> BitInputArchive* {
349 return mInputArchive.get();
350 }
351
352 void setInputArchive( std::unique_ptr< BitInputArchive >&& inputArchive ) {
353 mInputArchive = std::move( inputArchive );
354 }
355
356 auto inputArchiveItemsCount() const -> std::uint32_t {
357 return mInputArchiveItemsCount;
358 }
359
360 void setDeletedIndex( std::uint32_t index ) {
361 mDeletedItems.insert( index );
362 }
363
364 auto isDeletedIndex( std::uint32_t index ) const -> bool {
365 return mDeletedItems.find( index ) != mDeletedItems.cend();
366 }
367
368 auto hasDeletedIndexes() const -> bool {
369 return !mDeletedItems.empty();
370 }
371
372 auto hasNewItems() const -> bool {
373 return !mNewItems.empty();
374 }
375
376 friend class UpdateCallback;
377
378 private:
379 const BitAbstractArchiveCreator& mArchiveCreator;
380
381 std::unique_ptr< BitInputArchive > mInputArchive;
382 std::uint32_t mInputArchiveItemsCount;
383
384 BitItemsVector mNewItems;
385 DeletedItems mDeletedItems;
386
387 mutable FailedFiles mFailedFiles;
388
389 /* mInputIndices:
390 * - Position i = index in range [0, itemsCount() - 1] used by UpdateCallback.
391 * - Value at position i = corresponding index in the input archive (type InputIndex).
392 *
393 * If there are some deleted items, then 'i' is not equal to mInputIndices[i]
394 * (at least for values of i greater than the index of the first deleted item).
395 *
396 * Otherwise, if there are no deleted items, the vector is empty, and itemInputIndex(i)
397 * will return InputIndex with value i.
398 *
399 * This vector is either empty, or it has size equal to itemsCount() (thanks to updateInputIndices()). */
400 std::vector< InputIndex > mInputIndices;
401
402 auto initOutArchive() const -> CMyComPtr< IOutArchive >;
403
404 auto initOutFileStream( const bit7zfs::path& outArchive ) const -> CMyComPtr< IOutStream >;
405
407 const BitAbstractArchiveCreator& creator,
408 const bit7zfs::path& inArc,
409 ArchiveStartOffset archiveStart
410 );
411
412 void compressToFile( const bit7zfs::path& outFile, UpdateCallback* updateCallback );
413
414 void compressOut( IOutArchive* outArc, IOutStream* outStream, UpdateCallback* updateCallback );
415
416 void setArchiveProperties( IOutArchive* outArchive ) const;
417
418 void updateInputIndices();
419};
420
421} // namespace bit7z
422
423#endif //BITOUTPUTARCHIVE_HPP
Abstract class representing a generic archive creator.
Definition bitabstractarchivecreator.hpp:57
Abstract class representing a generic archive handler.
Definition bitabstractarchivehandler.hpp:120
The BitInputArchive class, given a handler object, allows reading/extracting the content of archives.
Definition bitinputarchive.hpp:64
A generic input item for compression operations.
Definition bitinputitem.hpp:70
auto addFile(buffer_t &&inBuffer, const tstring &name) -> BitInputItem &=delete
Deleted overload preventing the addition of a temporary buffer.
auto addFile(const tstring &inFile, const tstring &name={}) -> BitInputItem &
Adds the given file path, with an optional user-defined path to be used in the output archive.
void compressTo(buffer_t &outBuffer)
Compresses all the items added to this object to the specified buffer.
void addFiles(const tstring &inDir, const tstring &filter="*", FilterPolicy policy=FilterPolicy::Include, bool recursive=true)
Adds all the files inside the given directory path that match the given wildcard filter.
void addItems(const std::map< tstring, tstring > &inPaths)
Adds all the items that can be found by indexing the keys of the given filesystem paths map; the corr...
BitOutputArchive(const BitAbstractArchiveCreator &creator, std::istream &inStream, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitOutputArchive object, reading an input file archive from the given std::istream.
void compressTo(std::ostream &outStream)
Compresses all the items added to this object to the specified buffer.
void addFiles(const tstring &inDir, const tstring &filter, bool recursive)
Adds all the files inside the given directory path that match the given wildcard filter.
auto addFile(std::istream &inStream, const tstring &name) -> BitInputItem &
Adds the given standard input stream, using the given name as a path when compressed in the output ar...
BitOutputArchive(const BitAbstractArchiveCreator &creator, const buffer_t &inBuffer, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitOutputArchive object, opening an input file archive from the given buffer.
void addFiles(const std::vector< tstring > &inFiles)
Adds all the files in the given vector of filesystem paths.
auto itemsCount() const -> std::uint32_t
void compressTo(const tstring &outFile)
Compresses all the items added to this object to the specified archive file path.
BitOutputArchive(const BitAbstractArchiveCreator &creator, const tstring &inFile, ArchiveStartOffset startOffset=ArchiveStartOffset::None)
Constructs a BitOutputArchive object, opening an (optional) input file archive.
void addDirectoryContents(const tstring &inDir, const tstring &filter, bool recursive)
Adds the contents of the given directory path.
void addItems(const std::vector< std::pair< tstring, tstring > > &inPaths)
Adds all the items that can be found by indexing the keys of the given filesystem paths map; the corr...
BitOutputArchive(const BitAbstractArchiveCreator &creator, buffer_t &&inBuffer, ArchiveStartOffset startOffset=ArchiveStartOffset::None)=delete
Deleted overload preventing the use of a temporary input buffer.
void addDirectoryContents(const tstring &inDir, const tstring &filter="*", FilterPolicy policy=FilterPolicy::Include, bool recursive=true)
Adds the contents of the given directory path.
auto addFile(const buffer_t &inBuffer, const tstring &name) -> BitInputItem &
Adds the given buffer file, using the given name as a path when compressed in the output archive.
void addItems(const std::vector< tstring > &inPaths)
Adds all the items that can be found by indexing the given vector of filesystem paths.
auto handler() const noexcept -> const BitAbstractArchiveHandler &
void addDirectory(const tstring &inDir)
Adds the given directory path and all its content.
auto creator() const noexcept -> const BitAbstractArchiveCreator &
BitOutputArchive(const BitAbstractArchiveCreator &creator)
Constructs a BitOutputArchive object for a completely new archive.
T move(T... args)
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:29
std::vector< BitInputItem > BitItemsVector
A vector of items to be compressed into an archive.
Definition bititemsvector.hpp:36
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
BitProperty
The BitProperty enum represents the archive/item properties that 7-zip can read or write.
Definition bitpropvariant.hpp:32
std::basic_string< tchar > tstring
Definition bittypes.hpp:104
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::vector< FailedFile > FailedFiles
An alias for a sequence of FailedFile entries.
Definition bitexception.hpp:33
The BitPropVariant struct is a light extension to the WinAPI PROPVARIANT struct providing useful gett...
Definition bitpropvariant.hpp:161