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