1 | // |
2 | // Decompress.cpp |
3 | // |
4 | // Library: Zip |
5 | // Package: Zip |
6 | // Module: Decompress |
7 | // |
8 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Zip/Decompress.h" |
16 | #include "Poco/Zip/ZipLocalFileHeader.h" |
17 | #include "Poco/Zip/ZipArchive.h" |
18 | #include "Poco/Zip/ZipStream.h" |
19 | #include "Poco/Zip/ZipException.h" |
20 | #include "Poco/File.h" |
21 | #include "Poco/Exception.h" |
22 | #include "Poco/StreamCopier.h" |
23 | #include "Poco/Delegate.h" |
24 | #include "Poco/FileStream.h" |
25 | |
26 | |
27 | namespace Poco { |
28 | namespace Zip { |
29 | |
30 | |
31 | Decompress::Decompress(std::istream& in, const Poco::Path& outputDir, bool flattenDirs, bool keepIncompleteFiles): |
32 | _in(in), |
33 | _outDir(outputDir), |
34 | _flattenDirs(flattenDirs), |
35 | _keepIncompleteFiles(keepIncompleteFiles), |
36 | _mapping() |
37 | { |
38 | _outDir.makeAbsolute(); |
39 | _outDir.makeDirectory(); |
40 | if (!_in.good()) throw Poco::IOException("Bad input stream" ); |
41 | Poco::File tmp(_outDir); |
42 | if (!tmp.exists()) |
43 | { |
44 | tmp.createDirectories(); |
45 | } |
46 | if (!tmp.isDirectory()) |
47 | throw Poco::IOException("Failed to create/open directory: " + _outDir.toString()); |
48 | EOk += Poco::Delegate<Decompress, std::pair<const ZipLocalFileHeader, const Poco::Path> >(this, &Decompress::onOk); |
49 | |
50 | } |
51 | |
52 | |
53 | Decompress::~Decompress() |
54 | { |
55 | try |
56 | { |
57 | EOk -= Poco::Delegate<Decompress, std::pair<const ZipLocalFileHeader, const Poco::Path> >(this, &Decompress::onOk); |
58 | } |
59 | catch (...) |
60 | { |
61 | poco_unexpected(); |
62 | } |
63 | } |
64 | |
65 | |
66 | ZipArchive Decompress::decompressAllFiles() |
67 | { |
68 | poco_assert (_mapping.empty()); |
69 | ZipArchive arch(_in, *this); |
70 | return arch; |
71 | } |
72 | |
73 | |
74 | bool Decompress::handleZipEntry(std::istream& zipStream, const ZipLocalFileHeader& hdr) |
75 | { |
76 | if (hdr.isDirectory()) |
77 | { |
78 | // directory have 0 size, nth to read |
79 | if (!_flattenDirs) |
80 | { |
81 | std::string dirName = hdr.getFileName(); |
82 | if (!ZipCommon::isValidPath(dirName)) |
83 | throw ZipException("Illegal entry name" , dirName); |
84 | Poco::Path dir(_outDir, dirName); |
85 | dir.makeDirectory(); |
86 | Poco::File aFile(dir); |
87 | aFile.createDirectories(); |
88 | } |
89 | if (hdr.getCompressionMethod() == ZipCommon::CM_DEFLATE) |
90 | { |
91 | // If directory is stored with deflate method, two extra bytes |
92 | // (the result of deflating a zero-length sequence) must be read. |
93 | char buffer[2]; |
94 | zipStream.read(buffer, 2); |
95 | } |
96 | return true; |
97 | } |
98 | try |
99 | { |
100 | std::string fileName = hdr.getFileName(); |
101 | if (_flattenDirs) |
102 | { |
103 | // remove path info |
104 | Poco::Path p(fileName); |
105 | p.makeFile(); |
106 | fileName = p.getFileName(); |
107 | } |
108 | |
109 | if (!ZipCommon::isValidPath(fileName)) |
110 | throw ZipException("Illegal entry name" , fileName); |
111 | |
112 | Poco::Path file(fileName); |
113 | file.makeFile(); |
114 | Poco::Path dest(_outDir, file); |
115 | dest.makeFile(); |
116 | if (dest.depth() > 0) |
117 | { |
118 | Poco::File aFile(dest.parent()); |
119 | aFile.createDirectories(); |
120 | } |
121 | Poco::FileOutputStream out(dest.toString()); |
122 | ZipInputStream inp(zipStream, hdr, false); |
123 | Poco::StreamCopier::copyStream(inp, out); |
124 | out.close(); |
125 | Poco::File aFile(dest.toString()); |
126 | if (!aFile.exists() || !aFile.isFile()) |
127 | { |
128 | std::pair<const ZipLocalFileHeader, const std::string> tmp = std::make_pair(hdr, "Failed to create output stream " + dest.toString()); |
129 | EError.notify(this, tmp); |
130 | return false; |
131 | } |
132 | |
133 | if (!inp.crcValid()) |
134 | { |
135 | if (!_keepIncompleteFiles) |
136 | aFile.remove(); |
137 | std::pair<const ZipLocalFileHeader, const std::string> tmp = std::make_pair(hdr, "CRC mismatch. Corrupt file: " + dest.toString()); |
138 | EError.notify(this, tmp); |
139 | return false; |
140 | } |
141 | |
142 | // cannot check against hdr.getUnCompressedSize if CRC and size are not set in hdr but in a ZipDataInfo |
143 | // crc is typically enough to detect errors |
144 | if (aFile.getSize() != hdr.getUncompressedSize() && !hdr.searchCRCAndSizesAfterData()) |
145 | { |
146 | if (!_keepIncompleteFiles) |
147 | aFile.remove(); |
148 | std::pair<const ZipLocalFileHeader, const std::string> tmp = std::make_pair(hdr, "Filesizes do not match. Corrupt file: " + dest.toString()); |
149 | EError.notify(this, tmp); |
150 | return false; |
151 | } |
152 | |
153 | std::pair<const ZipLocalFileHeader, const Poco::Path> tmp = std::make_pair(hdr, file); |
154 | EOk.notify(this, tmp); |
155 | } |
156 | catch (Poco::Exception& e) |
157 | { |
158 | std::pair<const ZipLocalFileHeader, const std::string> tmp = std::make_pair(hdr, std::string("Exception: " + e.displayText())); |
159 | EError.notify(this, tmp); |
160 | return false; |
161 | } |
162 | catch (...) |
163 | { |
164 | std::pair<const ZipLocalFileHeader, const std::string> tmp = std::make_pair(hdr, std::string("Unknown Exception" )); |
165 | EError.notify(this, tmp); |
166 | return false; |
167 | } |
168 | |
169 | return true; |
170 | } |
171 | |
172 | |
173 | void Decompress::(const void*, std::pair<const ZipLocalFileHeader, const Poco::Path>& val) |
174 | { |
175 | _mapping.insert(std::make_pair(val.first.getFileName(), val.second)); |
176 | } |
177 | |
178 | |
179 | } } // namespace Poco::Zip |
180 | |