Mercurial > hg > orthanc
annotate Core/Compression/ZipWriter.cpp @ 1001:f3929718ea7e lua-scripting
autorouting primitives
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 Jul 2014 15:31:42 +0200 |
parents | 8811abd6aec9 |
children | 46bca019587e |
rev | line source |
---|---|
136 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
136 | 4 * Belgium |
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 | |
950
8811abd6aec9
move ThirdParty code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
41 #include "../../Resources/ThirdParty/minizip/zip.h" |
81 | 42 #include <boost/date_time/posix_time/posix_time.hpp> |
568 | 43 #include <limits> |
81 | 44 |
45 #include "../OrthancException.h" | |
46 | |
47 | |
48 static void PrepareFileInfo(zip_fileinfo& zfi) | |
49 { | |
50 memset(&zfi, 0, sizeof(zfi)); | |
51 | |
52 using namespace boost::posix_time; | |
53 ptime now = second_clock::local_time(); | |
54 | |
55 boost::gregorian::date today = now.date(); | |
56 ptime midnight(today); | |
57 | |
58 time_duration sinceMidnight = now - midnight; | |
59 zfi.tmz_date.tm_sec = sinceMidnight.seconds(); // seconds after the minute - [0,59] | |
60 zfi.tmz_date.tm_min = sinceMidnight.minutes(); // minutes after the hour - [0,59] | |
61 zfi.tmz_date.tm_hour = sinceMidnight.hours(); // hours since midnight - [0,23] | |
62 | |
63 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_day.html | |
64 zfi.tmz_date.tm_mday = today.day(); // day of the month - [1,31] | |
65 | |
66 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_month.html | |
67 zfi.tmz_date.tm_mon = today.month() - 1; // months since January - [0,11] | |
68 | |
69 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_year.html | |
70 zfi.tmz_date.tm_year = today.year(); // years - [1980..2044] | |
71 } | |
72 | |
73 | |
74 | |
75 namespace Orthanc | |
76 { | |
77 struct ZipWriter::PImpl | |
78 { | |
79 zipFile file_; | |
80 }; | |
81 | |
82 ZipWriter::ZipWriter() : pimpl_(new PImpl) | |
83 { | |
84 compressionLevel_ = 6; | |
85 hasFileInZip_ = false; | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
86 isZip64_ = false; |
81 | 87 |
88 pimpl_->file_ = NULL; | |
89 } | |
90 | |
91 ZipWriter::~ZipWriter() | |
92 { | |
93 Close(); | |
94 } | |
95 | |
96 void ZipWriter::Close() | |
97 { | |
98 if (IsOpen()) | |
99 { | |
100 zipClose(pimpl_->file_, "Created by Orthanc"); | |
101 pimpl_->file_ = NULL; | |
102 hasFileInZip_ = false; | |
103 } | |
104 } | |
105 | |
106 bool ZipWriter::IsOpen() const | |
107 { | |
108 return pimpl_->file_ != NULL; | |
109 } | |
110 | |
111 void ZipWriter::Open() | |
112 { | |
113 if (IsOpen()) | |
114 { | |
115 return; | |
116 } | |
117 | |
118 if (path_.size() == 0) | |
119 { | |
120 throw OrthancException("Please call SetOutputPath() before creating the file"); | |
121 } | |
122 | |
123 hasFileInZip_ = false; | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
124 |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
125 if (isZip64_) |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
126 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
127 pimpl_->file_ = zipOpen64(path_.c_str(), APPEND_STATUS_CREATE); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
128 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
129 else |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
130 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
131 pimpl_->file_ = zipOpen(path_.c_str(), APPEND_STATUS_CREATE); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
132 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
133 |
81 | 134 if (!pimpl_->file_) |
135 { | |
136 throw OrthancException(ErrorCode_CannotWriteFile); | |
137 } | |
138 } | |
139 | |
140 void ZipWriter::SetOutputPath(const char* path) | |
141 { | |
142 Close(); | |
143 path_ = path; | |
144 } | |
145 | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
146 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
|
147 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
148 Close(); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
149 isZip64_ = isZip64; |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
150 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
151 |
81 | 152 void ZipWriter::SetCompressionLevel(uint8_t level) |
153 { | |
154 if (level >= 10) | |
155 { | |
156 throw OrthancException("ZIP compression level must be between 0 (no compression) and 9 (highest compression"); | |
157 } | |
158 | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
159 Close(); |
81 | 160 compressionLevel_ = level; |
161 } | |
162 | |
249 | 163 void ZipWriter::OpenFile(const char* path) |
81 | 164 { |
165 Open(); | |
166 | |
167 zip_fileinfo zfi; | |
168 PrepareFileInfo(zfi); | |
169 | |
644
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
170 int result; |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
171 |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
172 if (isZip64_) |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
173 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
174 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
|
175 &zfi, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
176 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
177 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
178 "", // Comment |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
179 Z_DEFLATED, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
180 compressionLevel_, 1); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
181 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
182 else |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
183 { |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
184 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
|
185 &zfi, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
186 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
187 NULL, 0, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
188 "", // Comment |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
189 Z_DEFLATED, |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
190 compressionLevel_); |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
191 } |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
192 |
eb5a0b21d05e
do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
193 if (result != 0) |
81 | 194 { |
195 throw OrthancException(ErrorCode_CannotWriteFile); | |
196 } | |
197 | |
198 hasFileInZip_ = true; | |
199 } | |
200 | |
201 | |
202 void ZipWriter::Write(const std::string& data) | |
203 { | |
204 if (data.size()) | |
205 { | |
206 Write(&data[0], data.size()); | |
207 } | |
208 } | |
209 | |
210 | |
211 void ZipWriter::Write(const char* data, size_t length) | |
212 { | |
213 if (!hasFileInZip_) | |
214 { | |
249 | 215 throw OrthancException("Call first OpenFile()"); |
81 | 216 } |
217 | |
218 const size_t maxBytesInAStep = std::numeric_limits<int32_t>::max(); | |
219 | |
220 while (length > 0) | |
221 { | |
222 int bytes = static_cast<int32_t>(length <= maxBytesInAStep ? length : maxBytesInAStep); | |
223 | |
224 if (zipWriteInFileInZip(pimpl_->file_, data, bytes)) | |
225 { | |
226 throw OrthancException(ErrorCode_CannotWriteFile); | |
227 } | |
228 | |
229 data += bytes; | |
230 length -= bytes; | |
231 } | |
232 } | |
233 } |