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