Mercurial > hg > orthanc
annotate Core/Compression/ZipWriter.cpp @ 633:e207b33216db
merge
author | jodogne |
---|---|
date | Tue, 29 Oct 2013 09:04:25 +0100 |
parents | 3f27814104f7 |
children | eb5a0b21d05e |
rev | line source |
---|---|
136 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
398 | 3 * Copyright (C) 2012-2013 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 | |
568 | 32 #ifdef _WIN32 |
33 #define NOMINMAX | |
34 #endif | |
136 | 35 |
81 | 36 #include "ZipWriter.h" |
37 | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
81
diff
changeset
|
38 #include "../../Resources/minizip/zip.h" |
81 | 39 #include <boost/date_time/posix_time/posix_time.hpp> |
568 | 40 #include <limits> |
81 | 41 |
42 #include "../OrthancException.h" | |
43 | |
44 | |
45 static void PrepareFileInfo(zip_fileinfo& zfi) | |
46 { | |
47 memset(&zfi, 0, sizeof(zfi)); | |
48 | |
49 using namespace boost::posix_time; | |
50 ptime now = second_clock::local_time(); | |
51 | |
52 boost::gregorian::date today = now.date(); | |
53 ptime midnight(today); | |
54 | |
55 time_duration sinceMidnight = now - midnight; | |
56 zfi.tmz_date.tm_sec = sinceMidnight.seconds(); // seconds after the minute - [0,59] | |
57 zfi.tmz_date.tm_min = sinceMidnight.minutes(); // minutes after the hour - [0,59] | |
58 zfi.tmz_date.tm_hour = sinceMidnight.hours(); // hours since midnight - [0,23] | |
59 | |
60 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_day.html | |
61 zfi.tmz_date.tm_mday = today.day(); // day of the month - [1,31] | |
62 | |
63 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_month.html | |
64 zfi.tmz_date.tm_mon = today.month() - 1; // months since January - [0,11] | |
65 | |
66 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_year.html | |
67 zfi.tmz_date.tm_year = today.year(); // years - [1980..2044] | |
68 } | |
69 | |
70 | |
71 | |
72 namespace Orthanc | |
73 { | |
74 struct ZipWriter::PImpl | |
75 { | |
76 zipFile file_; | |
77 }; | |
78 | |
79 ZipWriter::ZipWriter() : pimpl_(new PImpl) | |
80 { | |
81 compressionLevel_ = 6; | |
82 hasFileInZip_ = false; | |
83 | |
84 pimpl_->file_ = NULL; | |
85 } | |
86 | |
87 ZipWriter::~ZipWriter() | |
88 { | |
89 Close(); | |
90 } | |
91 | |
92 void ZipWriter::Close() | |
93 { | |
94 if (IsOpen()) | |
95 { | |
96 zipClose(pimpl_->file_, "Created by Orthanc"); | |
97 pimpl_->file_ = NULL; | |
98 hasFileInZip_ = false; | |
99 } | |
100 } | |
101 | |
102 bool ZipWriter::IsOpen() const | |
103 { | |
104 return pimpl_->file_ != NULL; | |
105 } | |
106 | |
107 void ZipWriter::Open() | |
108 { | |
109 if (IsOpen()) | |
110 { | |
111 return; | |
112 } | |
113 | |
114 if (path_.size() == 0) | |
115 { | |
116 throw OrthancException("Please call SetOutputPath() before creating the file"); | |
117 } | |
118 | |
119 hasFileInZip_ = false; | |
120 pimpl_->file_ = zipOpen64(path_.c_str(), APPEND_STATUS_CREATE); | |
121 if (!pimpl_->file_) | |
122 { | |
123 throw OrthancException(ErrorCode_CannotWriteFile); | |
124 } | |
125 } | |
126 | |
127 void ZipWriter::SetOutputPath(const char* path) | |
128 { | |
129 Close(); | |
130 path_ = path; | |
131 } | |
132 | |
133 void ZipWriter::SetCompressionLevel(uint8_t level) | |
134 { | |
135 if (level >= 10) | |
136 { | |
137 throw OrthancException("ZIP compression level must be between 0 (no compression) and 9 (highest compression"); | |
138 } | |
139 | |
140 compressionLevel_ = level; | |
141 } | |
142 | |
249 | 143 void ZipWriter::OpenFile(const char* path) |
81 | 144 { |
145 Open(); | |
146 | |
147 zip_fileinfo zfi; | |
148 PrepareFileInfo(zfi); | |
149 | |
150 if (zipOpenNewFileInZip64(pimpl_->file_, path, | |
151 &zfi, | |
152 NULL, 0, | |
153 NULL, 0, | |
154 "", // Comment | |
155 Z_DEFLATED, | |
156 compressionLevel_, 1) != 0) | |
157 { | |
158 throw OrthancException(ErrorCode_CannotWriteFile); | |
159 } | |
160 | |
161 hasFileInZip_ = true; | |
162 } | |
163 | |
164 | |
165 void ZipWriter::Write(const std::string& data) | |
166 { | |
167 if (data.size()) | |
168 { | |
169 Write(&data[0], data.size()); | |
170 } | |
171 } | |
172 | |
173 | |
174 void ZipWriter::Write(const char* data, size_t length) | |
175 { | |
176 if (!hasFileInZip_) | |
177 { | |
249 | 178 throw OrthancException("Call first OpenFile()"); |
81 | 179 } |
180 | |
181 const size_t maxBytesInAStep = std::numeric_limits<int32_t>::max(); | |
182 | |
183 while (length > 0) | |
184 { | |
185 int bytes = static_cast<int32_t>(length <= maxBytesInAStep ? length : maxBytesInAStep); | |
186 | |
187 if (zipWriteInFileInZip(pimpl_->file_, data, bytes)) | |
188 { | |
189 throw OrthancException(ErrorCode_CannotWriteFile); | |
190 } | |
191 | |
192 data += bytes; | |
193 length -= bytes; | |
194 } | |
195 } | |
196 } |