bit7z 4.0.9
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) 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
10#ifndef BITOUTPUTARCHIVE_HPP
11#define BITOUTPUTARCHIVE_HPP
12
13#include <istream>
14#include <set>
15
16#include "bitabstractarchivecreator.hpp"
17#include "bititemsvector.hpp"
18#include "bitexception.hpp" //for FailedFiles
19#include "bitpropvariant.hpp"
20
22struct ISequentialInStream;
23
24template< typename T >
25class CMyComPtr;
27
28namespace bit7z {
29
30using std::istream;
31
32using DeletedItems = std::set< uint32_t >;
33
34/* General note: I tried my best to explain how indices work here, but it is a bit complex. */
35
36/* We introduce a strong index type to differentiate between indices in the output
37 * archive (uint32_t, as used by the UpdateCallback), and the corresponding indexes
38 * in the input archive (InputIndex). In this way, we avoid implicit conversions
39 * between the two kinds of indices.
40 *
41 * UpdateCallback uses indices in the range [0, BitOutputArchive::itemsCount() - 1]
42 *
43 * Now, if the user doesn't delete any item in the input archive, itemsCount()
44 * is just equal to <n° of items in the input archive> + <n° of newly added items>.
45 * In this case, an InputIndex value is just equal to the index used by UpdateCallback.
46 *
47 * On the contrary, if the user wants to delete an item in the input archive, the value
48 * of an InputIndex may differ from the corresponding UpdateCallback's index.
49 *
50 * Note: given an InputIndex i:
51 * if i < mInputArchiveItemsCount, the item is old (old item in the input archive);
52 * if i >= mInputArchiveItemsCount, the item is new (added by the user); */
53enum class InputIndex : std::uint32_t {};
54
55class UpdateCallback;
56
61 public:
69
82 const tstring& inFile,
84
97 const buffer_t& inBuffer,
99
108 std::istream& inStream,
110
111 BitOutputArchive( const BitOutputArchive& ) = delete;
112
114
115 auto operator=( const BitOutputArchive& ) -> BitOutputArchive& = delete;
116
117 auto operator=( BitOutputArchive&& ) -> BitOutputArchive& = delete;
118
124 void addItems( const std::vector< tstring >& inPaths );
125
134
143 void addFile( const tstring& inFile, const tstring& name = {} );
144
151 void addFile( const std::vector< byte_t >& inBuffer, const tstring& name );
152
160 void addFile( std::istream& inStream, const tstring& name );
161
169 void addFiles( const std::vector< tstring >& inFiles );
170
178 void addFiles( const tstring& inDir, const tstring& filter, bool recursive );
179
189 void addFiles( const tstring& inDir,
190 const tstring& filter = BIT7Z_STRING( "*" ),
192 bool recursive = true );
193
199 void addDirectory( const tstring& inDir );
200
212 void addDirectoryContents( const tstring& inDir, const tstring& filter, bool recursive );
213
227 void addDirectoryContents( const tstring& inDir,
228 const tstring& filter = BIT7Z_STRING( "*" ),
230 bool recursive = true );
231
240 void compressTo( const tstring& outFile );
241
248
254 void compressTo( std::ostream& outStream );
255
259 auto itemsCount() const -> uint32_t;
260
265 auto handler() const noexcept -> const BitAbstractArchiveHandler&;
266
271 auto creator() const noexcept -> const BitAbstractArchiveCreator&;
272
276 virtual ~BitOutputArchive() = default;
277
278 protected:
279 virtual auto itemProperty( InputIndex index, BitProperty property ) const -> BitPropVariant;
280
281 virtual auto itemStream( InputIndex index, ISequentialInStream** inStream ) const -> HRESULT;
282
283 virtual auto hasNewData( uint32_t index ) const noexcept -> bool;
284
285 virtual auto hasNewProperties( uint32_t index ) const noexcept -> bool;
286
287 auto itemInputIndex( uint32_t newIndex ) const noexcept -> InputIndex;
288
289 auto outputItemProperty( uint32_t index, BitProperty property ) const -> BitPropVariant;
290
291 auto outputItemStream( uint32_t index, ISequentialInStream** inStream ) const -> HRESULT;
292
293 auto indexInArchive( uint32_t index ) const noexcept -> uint32_t;
294
295 inline auto inputArchive() const -> BitInputArchive* {
296 return mInputArchive.get();
297 }
298
299 inline void setInputArchive( std::unique_ptr< BitInputArchive >&& inputArchive ) {
300 mInputArchive = std::move( inputArchive );
301 }
302
303 inline auto inputArchiveItemsCount() const -> uint32_t {
304 return mInputArchiveItemsCount;
305 }
306
307 inline void setDeletedIndex( uint32_t index ) {
308 mDeletedItems.insert( index );
309 }
310
311 inline auto isDeletedIndex( uint32_t index ) const -> bool {
312 return mDeletedItems.find( index ) != mDeletedItems.cend();
313 }
314
315 inline auto hasDeletedIndexes() const -> bool {
316 return !mDeletedItems.empty();
317 }
318
319 inline auto hasNewItems() const -> bool {
320 return mNewItemsVector.size() > 0;
321 }
322
323 friend class UpdateCallback;
324
325 private:
326 const BitAbstractArchiveCreator& mArchiveCreator;
327
328 unique_ptr< BitInputArchive > mInputArchive;
329 uint32_t mInputArchiveItemsCount;
330
331 BitItemsVector mNewItemsVector;
332 DeletedItems mDeletedItems;
333
334 mutable FailedFiles mFailedFiles;
335
336 /* mInputIndices:
337 * - Position i = index in range [0, itemsCount() - 1] used by UpdateCallback.
338 * - Value at position i = corresponding index in the input archive (type InputIndex).
339 *
340 * If there are some deleted items, then i != mInputIndices[i]
341 * (at least for values of i greater than the index of the first deleted item).
342 *
343 * Otherwise, if there are no deleted items, the vector is empty, and itemInputIndex(i)
344 * will return InputIndex with value i.
345 *
346 * This vector is either empty, or it has size equal to itemsCount() (thanks to updateInputIndices()). */
347 std::vector< InputIndex > mInputIndices;
348
349 auto initOutArchive() const -> CMyComPtr< IOutArchive >;
350
351 auto initOutFileStream( const fs::path& outArchive, bool updatingArchive ) const -> CMyComPtr< IOutStream >;
352
353 BitOutputArchive( const BitAbstractArchiveCreator& creator,
354 const fs::path& inArc,
355 ArchiveStartOffset archiveStart );
356
357 void compressToFile( const fs::path& outFile, UpdateCallback* updateCallback );
358
359 void compressOut( IOutArchive* outArc, IOutStream* outStream, UpdateCallback* updateCallback );
360
361 void setArchiveProperties( IOutArchive* outArchive ) const;
362
363 void updateInputIndices();
364};
365
366} // namespace bit7z
367
368#endif //BITOUTPUTARCHIVE_HPP
Abstract class representing a generic archive creator.
Definition bitabstractarchivecreator.hpp:44
Abstract class representing a generic archive handler.
Definition bitabstractarchivehandler.hpp:74
The BitInputArchive class, given a handler object, allows reading/extracting the content of archives.
Definition bitinputarchive.hpp:40
auto size() const -> std::size_t
The BitOutputArchive class, given a creator object, allows creating new archives.
Definition bitoutputarchive.hpp:60
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 map of filesystem paths; the c...
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 addFile(const std::vector< byte_t > &inBuffer, const tstring &name)
Adds the given buffer file, using the given name as a path when compressed in the output archive.
void compressTo(std::vector< byte_t > &outBuffer)
Compresses all the items added to this object to the specified buffer.
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.
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.
void compressTo(const tstring &outFile)
Compresses all the items added to this object to the specified archive file path.
void addFile(const tstring &inFile, const tstring &name={})
Adds the given file path, with an optional user-defined path to be used in the output archive.
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 addFile(std::istream &inStream, const tstring &name)
Adds the given standard input stream, using the given name as a path when compressed in the output ar...
void addDirectoryContents(const tstring &inDir, const tstring &filter="*", FilterPolicy policy=FilterPolicy::Include, bool recursive=true)
Adds the contents of the given directory path.
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 &
auto itemsCount() const -> uint32_t
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.
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:30
ArchiveStartOffset
Offset from where the archive starts within the input file.
Definition bitinputarchive.hpp:31
@ None
Don't specify an archive start offset.
BitProperty
The BitProperty enum represents the archive/item properties that 7-zip can read or write.
Definition bitpropvariant.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.
The BitPropVariant struct is a light extension to the WinAPI PROPVARIANT struct providing useful gett...
Definition bitpropvariant.hpp:150