bit7z 4.0.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) 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
81 explicit BitOutputArchive( const BitAbstractArchiveCreator& creator, const tstring& inFile );
82
95
104
105 BitOutputArchive( const BitOutputArchive& ) = delete;
106
108
109 auto operator=( const BitOutputArchive& ) -> BitOutputArchive& = delete;
110
111 auto operator=( BitOutputArchive&& ) -> BitOutputArchive& = delete;
112
118 void addItems( const std::vector< tstring >& inPaths );
119
128
137 void addFile( const tstring& inFile, const tstring& name = {} );
138
145 void addFile( const std::vector< byte_t >& inBuffer, const tstring& name );
146
154 void addFile( std::istream& inStream, const tstring& name );
155
163 void addFiles( const std::vector< tstring >& inFiles );
164
173 void addFiles( const tstring& inDir,
174 const tstring& filter = BIT7Z_STRING( "*" ),
175 bool recursive = true );
176
186 void addFiles( const tstring& inDir,
187 const tstring& filter = BIT7Z_STRING( "*" ),
189 bool recursive = true );
190
196 void addDirectory( const tstring& inDir );
197
206 void compressTo( const tstring& outFile );
207
214
220 void compressTo( std::ostream& outStream );
221
225 auto itemsCount() const -> uint32_t;
226
231 auto handler() const noexcept -> const BitAbstractArchiveHandler&;
232
237 auto creator() const noexcept -> const BitAbstractArchiveCreator&;
238
242 virtual ~BitOutputArchive() = default;
243
244 protected:
245 virtual auto itemProperty( InputIndex index, BitProperty property ) const -> BitPropVariant;
246
247 virtual auto itemStream( InputIndex index, ISequentialInStream** inStream ) const -> HRESULT;
248
249 virtual auto hasNewData( uint32_t index ) const noexcept -> bool;
250
251 virtual auto hasNewProperties( uint32_t index ) const noexcept -> bool;
252
253 auto itemInputIndex( uint32_t newIndex ) const noexcept -> InputIndex;
254
255 auto outputItemProperty( uint32_t index, BitProperty property ) const -> BitPropVariant;
256
257 auto outputItemStream( uint32_t index, ISequentialInStream** inStream ) const -> HRESULT;
258
259 auto indexInArchive( uint32_t index ) const noexcept -> uint32_t;
260
261 inline auto inputArchive() const -> BitInputArchive* {
262 return mInputArchive.get();
263 }
264
265 inline void setInputArchive( std::unique_ptr< BitInputArchive >&& inputArchive ) {
266 mInputArchive = std::move( inputArchive );
267 }
268
269 inline auto inputArchiveItemsCount() const -> uint32_t {
270 return mInputArchiveItemsCount;
271 }
272
273 inline void setDeletedIndex( uint32_t index ) {
274 mDeletedItems.insert( index );
275 }
276
277 inline auto isDeletedIndex( uint32_t index ) const -> bool {
278 return mDeletedItems.find( index ) != mDeletedItems.cend();
279 }
280
281 inline auto hasDeletedIndexes() const -> bool {
282 return !mDeletedItems.empty();
283 }
284
285 inline auto hasNewItems() const -> bool {
286 return mNewItemsVector.size() > 0;
287 }
288
289 friend class UpdateCallback;
290
291 private:
292 const BitAbstractArchiveCreator& mArchiveCreator;
293
294 unique_ptr< BitInputArchive > mInputArchive;
295 uint32_t mInputArchiveItemsCount;
296
297 BitItemsVector mNewItemsVector;
298 DeletedItems mDeletedItems;
299
300 mutable FailedFiles mFailedFiles;
301
302 /* mInputIndices:
303 * - Position i = index in range [0, itemsCount() - 1] used by UpdateCallback.
304 * - Value at position i = corresponding index in the input archive (type InputIndex).
305 *
306 * If there are some deleted items, then i != mInputIndices[i]
307 * (at least for values of i greater than the index of the first deleted item).
308 *
309 * Otherwise, if there are no deleted items, the vector is empty, and itemInputIndex(i)
310 * will return InputIndex with value i.
311 *
312 * This vector is either empty, or it has size equal to itemsCount() (thanks to updateInputIndices()). */
313 std::vector< InputIndex > mInputIndices;
314
315 auto initOutArchive() const -> CMyComPtr< IOutArchive >;
316
317 auto initOutFileStream( const fs::path& outArchive, bool updatingArchive ) const -> CMyComPtr< IOutStream >;
318
319 BitOutputArchive( const BitAbstractArchiveCreator& creator, const fs::path& inArc );
320
321 void compressToFile( const fs::path& outFile, UpdateCallback* updateCallback );
322
323 void compressOut( IOutArchive* outArc, IOutStream* outStream, UpdateCallback* updateCallback );
324
325 void setArchiveProperties( IOutArchive* outArchive ) const;
326
327 void updateInputIndices();
328};
329
330} // namespace bit7z
331
332#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:31
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...
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 addFiles(const tstring &inDir, const tstring &filter="*", bool recursive=true)
Adds all the files inside the given directory path that match the given wildcard filter.
void compressTo(std::ostream &outStream)
Compresses all the items added to this object to the specified buffer.
BitOutputArchive(const BitAbstractArchiveCreator &creator, const tstring &inFile)
Constructs a BitOutputArchive object, opening an (optional) input file archive.
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.
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 addItems(const std::vector< tstring > &inPaths)
Adds all the items that can be found by indexing the given vector of filesystem paths.
BitOutputArchive(const BitAbstractArchiveCreator &creator, std::istream &inStream)
Constructs a BitOutputArchive object, reading an input file archive from the given std::istream.
auto handler() const noexcept -> const BitAbstractArchiveHandler &
auto itemsCount() const -> uint32_t
void addDirectory(const tstring &inDir)
Adds all the items inside the given directory path.
auto creator() const noexcept -> const BitAbstractArchiveCreator &
BitOutputArchive(const BitAbstractArchiveCreator &creator)
Constructs a BitOutputArchive object for a completely new archive.
BitOutputArchive(const BitAbstractArchiveCreator &creator, const std::vector< byte_t > &inBuffer)
Constructs a BitOutputArchive object, opening an input file archive from the given buffer.
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:30
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