/src/poco/Zip/include/Poco/Zip/Decompress.h
Line | Count | Source |
1 | | // |
2 | | // Decompress.h |
3 | | // |
4 | | // Library: Zip |
5 | | // Package: Zip |
6 | | // Module: Decompress |
7 | | // |
8 | | // Definition of the Decompress class. |
9 | | // |
10 | | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Zip_Decompress_INCLUDED |
18 | | #define Zip_Decompress_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Zip/Zip.h" |
22 | | #include "Poco/Zip/ParseCallback.h" |
23 | | #include "Poco/Zip/ZipArchive.h" |
24 | | #include "Poco/Path.h" |
25 | | #include "Poco/BasicEvent.h" |
26 | | |
27 | | |
28 | | namespace Poco::Zip { |
29 | | |
30 | | |
31 | | class ZipArchive; |
32 | | |
33 | | |
34 | | class Zip_API Decompress: public ParseCallback |
35 | | /// Decompress extracts files from zip files, can be used to extract single files or all files |
36 | | { |
37 | | public: |
38 | | using ZipMapping = std::map<std::string, Poco::Path>; |
39 | | /// Maps key of FileInfo entries to their local decompressed representation |
40 | | |
41 | | Poco::BasicEvent<std::pair<const ZipLocalFileHeader, const std::string>> EError; |
42 | | /// Thrown whenever an error is detected when handling a ZipLocalFileHeader entry. The string contains an error message |
43 | | |
44 | | Poco::BasicEvent<std::pair<const ZipLocalFileHeader, const Poco::Path>> EOk; |
45 | | /// Thrown whenever a file was successfully decompressed |
46 | | |
47 | | Decompress(std::istream& in, const Poco::Path& outputDir, bool flattenDirs = false, bool keepIncompleteFiles = false); |
48 | | /// Creates the Decompress. Note that istream must be good and at the very beginning of the file! |
49 | | /// Calling decompressAllFiles will cause the stream to be in state failed once the zip file is processed. |
50 | | /// outputDir must be a directory. If it doesn't exist yet, it will be automatically created. |
51 | | /// If flattenDirs is set to true, the directory structure of the zip file is not recreated. |
52 | | /// Instead, all files are extracted into one single directory. |
53 | | |
54 | | ~Decompress() override; |
55 | | /// Destroys the Decompress. |
56 | | |
57 | | ZipArchive decompressAllFiles(const bool checkConsistency = true); |
58 | | /// Decompresses all files stored in the zip File. Can only be called once per Decompress object. |
59 | | /// Use mapping to retrieve the location of the decompressed files |
60 | | /// if checkConsistency is set to false, archive won't be checked for consistency before decompression |
61 | | |
62 | | bool handleZipEntry(std::istream& zipStream, const ZipLocalFileHeader& hdr) override; |
63 | | |
64 | | [[nodiscard]] const ZipMapping& mapping() const; |
65 | | /// A ZipMapping stores as key the full name of the ZipFileInfo/ZipLocalFileHeader and as value the decompressed file |
66 | | /// If for a ZipFileInfo no mapping exists, there was an error during decompression and the entry is considered to be corrupt |
67 | | |
68 | | private: |
69 | | Decompress(const Decompress&); |
70 | | Decompress& operator=(const Decompress&); |
71 | | |
72 | | void onOk(const void*, std::pair<const ZipLocalFileHeader, const Poco::Path>& val); |
73 | | |
74 | | private: |
75 | | std::istream& _in; |
76 | | Poco::Path _outDir; |
77 | | bool _flattenDirs; |
78 | | bool _keepIncompleteFiles; |
79 | | ZipMapping _mapping; |
80 | | }; |
81 | | |
82 | | |
83 | | inline const Decompress::ZipMapping& Decompress::mapping() const |
84 | 0 | { |
85 | 0 | return _mapping; |
86 | 0 | } |
87 | | |
88 | | |
89 | | } // namespace Poco::Zip |
90 | | |
91 | | |
92 | | #endif // Zip_Decompress_INCLUDED |