Mercurial > hg > orthanc
annotate Core/Compression/ZipWriter.cpp @ 3059:caa03eaeb097 db-changes
fix to use from orthanc-databases
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 21 Dec 2018 17:25:12 +0100 |
parents | d924f9bb61cc |
children | 4e43e67f8ecf |
rev | line source |
---|---|
136 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1278
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
136 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 |
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
34 #include "../PrecompiledHeaders.h" |
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
35 |
658
e8e59e80868c
note about glog-0.3.3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
644
diff
changeset
|
36 #ifndef NOMINMAX |
568 | 37 #define NOMINMAX |
38 #endif | |
136 | 39 |
81 | 40 #include "ZipWriter.h" |
41 | |
1278 | 42 #include <limits> |
43 #include <boost/filesystem.hpp> | |
81 | 44 #include <boost/date_time/posix_time/posix_time.hpp> |
45 | |
1278 | 46 #include "../../Resources/ThirdParty/minizip/zip.h" |
81 | 47 #include "../OrthancException.h" |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
48 #include "../Logging.h" |
81 | 49 |
50 | |
51 static void PrepareFileInfo(zip_fileinfo& zfi) | |
52 { | |
53 memset(&zfi, 0, sizeof(zfi)); | |
54 | |
55 using namespace boost::posix_time; | |
56 ptime now = second_clock::local_time(); | |
57 | |
58 boost::gregorian::date today = now.date(); | |
59 ptime midnight(today); | |
60 | |
61 time_duration sinceMidnight = now - midnight; | |
62 zfi.tmz_date.tm_sec = sinceMidnight.seconds(); // seconds after the minute - [0,59] | |
63 zfi.tmz_date.tm_min = sinceMidnight.minutes(); // minutes after the hour - [0,59] | |
64 zfi.tmz_date.tm_hour = sinceMidnight.hours(); // hours since midnight - [0,23] | |
65 | |
66 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_day.html | |
67 zfi.tmz_date.tm_mday = today.day(); // day of the month - [1,31] | |
68 | |
69 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_month.html | |
70 zfi.tmz_date.tm_mon = today.month() - 1; // months since January - [0,11] | |
71 | |
72 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_year.html | |
73 zfi.tmz_date.tm_year = today.year(); // years - [1980..2044] | |
74 } | |
75 | |
76 | |
77 | |
78 namespace Orthanc | |
79 { | |
80 struct ZipWriter::PImpl | |
81 { | |
82 zipFile file_; | |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
83 |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
84 PImpl() : file_(NULL) |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
85 { |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
86 } |
81 | 87 }; |
88 | |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
89 ZipWriter::ZipWriter() : |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
90 pimpl_(new PImpl), |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
91 isZip64_(false), |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
92 hasFileInZip_(false), |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
93 append_(false), |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
94 compressionLevel_(6) |
81 | 95 { |
96 } | |
97 | |
98 ZipWriter::~ZipWriter() | |
99 { | |
100 Close(); | |
101 } | |
102 | |
103 void ZipWriter::Close() | |
104 { | |
105 if (IsOpen()) | |
106 { | |
107 zipClose(pimpl_->file_, "Created by Orthanc"); | |
108 pimpl_->file_ = NULL; | |
109 hasFileInZip_ = false; | |
110 } | |
111 } | |
112 | |
113 bool ZipWriter::IsOpen() const | |
114 { | |
115 return pimpl_->file_ != NULL; | |
116 } | |
117 | |
118 void ZipWriter::Open() | |
119 { | |
120 if (IsOpen()) | |
121 { | |
122 return; | |
123 } | |
124 | |
125 if (path_.size() == 0) | |
126 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
127 throw OrthancException(ErrorCode_BadSequenceOfCalls, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
128 "Please call SetOutputPath() before creating the file"); |
81 | 129 } |
130 | |
131 hasFileInZip_ = false; | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
132 |
1278 | 133 int mode = APPEND_STATUS_CREATE; |
134 if (append_ && | |
135 boost::filesystem::exists(path_)) | |
136 { | |
137 mode = APPEND_STATUS_ADDINZIP; | |
138 } | |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
139 |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
140 if (isZip64_) |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
141 { |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
142 pimpl_->file_ = zipOpen64(path_.c_str(), mode); |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
143 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
144 else |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
145 { |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
146 pimpl_->file_ = zipOpen(path_.c_str(), mode); |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
147 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
148 |
81 | 149 if (!pimpl_->file_) |
150 { | |
151 throw OrthancException(ErrorCode_CannotWriteFile); | |
152 } | |
153 } | |
154 | |
155 void ZipWriter::SetOutputPath(const char* path) | |
156 { | |
157 Close(); | |
158 path_ = path; | |
159 } | |
160 | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
161 void ZipWriter::SetZip64(bool isZip64) |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
162 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
163 Close(); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
164 isZip64_ = isZip64; |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
165 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
166 |
81 | 167 void ZipWriter::SetCompressionLevel(uint8_t level) |
168 { | |
169 if (level >= 10) | |
170 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
171 throw OrthancException(ErrorCode_ParameterOutOfRange, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
172 "ZIP compression level must be between 0 (no compression) and 9 (highest compression)"); |
81 | 173 } |
174 | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
175 Close(); |
81 | 176 compressionLevel_ = level; |
177 } | |
178 | |
249 | 179 void ZipWriter::OpenFile(const char* path) |
81 | 180 { |
181 Open(); | |
182 | |
183 zip_fileinfo zfi; | |
184 PrepareFileInfo(zfi); | |
185 | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
186 int result; |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
187 |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
188 if (isZip64_) |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
189 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
190 result = zipOpenNewFileInZip64(pimpl_->file_, path, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
191 &zfi, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
192 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
193 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
194 "", // Comment |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
195 Z_DEFLATED, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
196 compressionLevel_, 1); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
197 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
198 else |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
199 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
200 result = zipOpenNewFileInZip(pimpl_->file_, path, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
201 &zfi, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
202 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
203 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
204 "", // Comment |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
205 Z_DEFLATED, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
206 compressionLevel_); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
207 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
208 |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
209 if (result != 0) |
81 | 210 { |
211 throw OrthancException(ErrorCode_CannotWriteFile); | |
212 } | |
213 | |
214 hasFileInZip_ = true; | |
215 } | |
216 | |
217 | |
218 void ZipWriter::Write(const std::string& data) | |
219 { | |
220 if (data.size()) | |
221 { | |
222 Write(&data[0], data.size()); | |
223 } | |
224 } | |
225 | |
226 | |
227 void ZipWriter::Write(const char* data, size_t length) | |
228 { | |
229 if (!hasFileInZip_) | |
230 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
231 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Call first OpenFile()"); |
81 | 232 } |
233 | |
234 const size_t maxBytesInAStep = std::numeric_limits<int32_t>::max(); | |
235 | |
236 while (length > 0) | |
237 { | |
238 int bytes = static_cast<int32_t>(length <= maxBytesInAStep ? length : maxBytesInAStep); | |
239 | |
240 if (zipWriteInFileInZip(pimpl_->file_, data, bytes)) | |
241 { | |
242 throw OrthancException(ErrorCode_CannotWriteFile); | |
243 } | |
244 | |
245 data += bytes; | |
246 length -= bytes; | |
247 } | |
248 } | |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
249 |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
250 |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
251 void ZipWriter::SetAppendToExisting(bool append) |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
252 { |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
253 Close(); |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
254 append_ = append; |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
255 } |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
256 |
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
257 |
81 | 258 } |