bit7z 4.0.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) 2014-2023 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 <vector>
17
18#include "bitoutputarchive.hpp"
19
20namespace bit7z {
21
22using std::vector;
23
24namespace filesystem { // NOLINT(modernize-concat-nested-namespaces)
25namespace fsutil {
26auto stem( const tstring& path ) -> tstring;
27} // namespace fsutil
28} // namespace filesystem
29
30using namespace filesystem;
31
32#ifdef __cpp_if_constexpr
33#define BIT7Z_IF_CONSTEXPR if constexpr
34#else
35#define BIT7Z_IF_CONSTEXPR if
36#endif
37
44template< typename Input >
46 public:
59
67 void compressFile( Input inFile,
68 const tstring& outFile,
69 const tstring& inputName = {} ) const {
70 /* Note: if inFile is a filesystem path (i.e., its type is const tstring&), we can deduce the archived
71 * item filename using the original filename. Otherwise, if the user didn't specify the input file name,
72 * we use the filename (without extension) of the output file path. */
73 tstring name;
75 name = inputName.empty() ? fsutil::stem( outFile ) : inputName;
76 }
77
78 BitOutputArchive outputArchive{ *this, outFile };
79 outputArchive.addFile( inFile, name );
80 outputArchive.compressTo( outFile );
81 }
82
90 void compressFile( Input inFile,
91 vector< byte_t >& outBuffer,
92 const tstring& inputName = {} ) const {
93 BitOutputArchive outputArchive{ *this, outBuffer };
94 outputArchive.addFile( inFile, inputName );
95 outputArchive.compressTo( outBuffer );
96 }
97
105 void compressFile( Input inFile,
106 ostream& outStream,
107 const tstring& inputName = {} ) const {
108 BitOutputArchive outputArchive{ *this };
109 outputArchive.addFile( inFile, inputName );
110 outputArchive.compressTo( outStream );
111 }
112};
113
114} // namespace bit7z
115
116#endif //BITCOMPRESSOR_HPP
The Bit7zLibrary class allows accessing the basic functionalities provided by the 7z DLLs.
Definition bit7zlibrary.hpp:56
Abstract class representing a generic archive creator.
Definition bitabstractarchivecreator.hpp:44
auto format() const noexcept -> const BitInFormat &override
The BitCompressor template class allows compressing files into archives.
Definition bitcompressor.hpp:45
void compressFile(Input inFile, const tstring &outFile, const tstring &inputName={}) const
Compresses a single file.
Definition bitcompressor.hpp:67
void compressFile(Input inFile, ostream &outStream, const tstring &inputName={}) const
Compresses the input file to the output stream.
Definition bitcompressor.hpp:105
BitCompressor(Bit7zLibrary const &lib, BitInOutFormat const &format)
Constructs a BitCompressor object.
Definition bitcompressor.hpp:57
void compressFile(Input inFile, vector< byte_t > &outBuffer, const tstring &inputName={}) const
Compresses the input file to the output buffer.
Definition bitcompressor.hpp:90
The BitInOutFormat class specifies a format available for creating new archives and extract old ones.
Definition bitformat.hpp:105
The BitOutputArchive class, given a creator object, allows creating new archives.
Definition bitoutputarchive.hpp:60
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.
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:30
std::basic_string< tchar > tstring
Definition bittypes.hpp:90