bit7z 4.1.0
A C++ library for interfacing with the 7-zip shared libs.
Loading...
Searching...
No Matches
bitcompressor.hpp
1// This is an open source non-commercial project. Dear PVS-Studio, please check it.
2// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
4/*
5 * bit7z - A C++ static library to interface with the 7-zip shared libraries.
6 * Copyright (c) Riccardo Ostani - All Rights Reserved.
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
11 */
12
13#ifndef BITCOMPRESSOR_HPP
14#define BITCOMPRESSOR_HPP
15
16#include "bit7zlibrary.hpp"
17#include "bitabstractarchivecreator.hpp"
18#include "bitformat.hpp"
19#include "bitoutputarchive.hpp"
20#include "bittypes.hpp"
21
22#include <ostream>
23#include <type_traits>
24
25namespace bit7z {
26
27namespace filesystem { // NOLINT(modernize-concat-nested-namespaces)
28namespace fsutil {
29auto stem( const tstring& path ) -> tstring;
30} // namespace fsutil
31} // namespace filesystem
32
33#ifdef __cpp_if_constexpr
34#define BIT7Z_IF_CONSTEXPR if constexpr
35#else
36#define BIT7Z_IF_CONSTEXPR if
37#endif
38
45template< typename Input >
46class BitCompressor : public BitAbstractArchiveCreator {
47 public:
59 : BitAbstractArchiveCreator( lib, format ) {}
60
69 Input inFile,
70 const tstring& outFile,
71 const tstring& inputName = {}
72 ) const {
73 /* Note: if inFile is a filesystem path (i.e., its type is const tstring&), we can deduce the archived
74 * item filename using the original filename. Otherwise, if the user didn't specify the input file name,
75 * we use the filename (without extension) of the output file path. */
76 tstring name;
77 BIT7Z_IF_CONSTEXPR ( !std::is_same< Input, const tstring& >::value ) {
78 name = inputName.empty() ? filesystem::fsutil::stem( outFile ) : inputName;
79 }
80
81 BitOutputArchive outputArchive{ *this, outFile };
82 outputArchive.addFile( inFile, name );
83 outputArchive.compressTo( outFile );
84 }
85
94 Input inFile,
95 buffer_t& outBuffer,
96 const tstring& inputName = {}
97 ) const {
98 BitOutputArchive outputArchive{ *this, outBuffer };
99 outputArchive.addFile( inFile, inputName );
100 outputArchive.compressTo( outBuffer );
101 }
102
111 Input inFile,
112 std::ostream& outStream,
113 const tstring& inputName = {}
114 ) const {
115 BitOutputArchive outputArchive{ *this };
116 outputArchive.addFile( inFile, inputName );
117 outputArchive.compressTo( outStream );
118 }
119};
120
121} // namespace bit7z
122
123#endif //BITCOMPRESSOR_HPP
The Bit7zLibrary class allows accessing the basic functionalities provided by the 7z DLLs.
Definition bit7zlibrary.hpp:55
auto format() const noexcept -> const BitInFormat &override
void compressFile(Input inFile, const tstring &outFile, const tstring &inputName={}) const
Compresses a single file.
Definition bitcompressor.hpp:68
void compressFile(Input inFile, buffer_t &outBuffer, const tstring &inputName={}) const
Compresses the input file to the output buffer.
Definition bitcompressor.hpp:93
void compressFile(Input inFile, std::ostream &outStream, const tstring &inputName={}) const
Compresses the input file to the output stream.
Definition bitcompressor.hpp:110
BitCompressor(Bit7zLibrary const &lib, BitInOutFormat const &format)
Constructs a BitCompressor object.
Definition bitcompressor.hpp:58
The BitInOutFormat class specifies a format available for creating new archives and extract old ones.
Definition bitformat.hpp:115
The BitOutputArchive class, given a creator object, allows creating new archives.
Definition bitoutputarchive.hpp:65
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(const tstring &outFile)
Compresses all the items added to this object to the specified archive file path.
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:29
std::basic_string< tchar > tstring
Definition bittypes.hpp:104