annotate Core/Compression/ZipWriter.cpp @ 3598:f2df51bff2fd

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 06 Jan 2020 19:57:11 +0100
parents 5d1f5984dc41
children 94f4a18a79cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
1 /**
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
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
3060
4e43e67f8ecf preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
6 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
7 * This program is free software: you can redistribute it and/or
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
8 * modify it under the terms of the GNU General Public License as
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
10 * License, or (at your option) any later version.
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
11 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
12 * In addition, as a special exception, the copyright holders of this
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
13 * program give permission to link the code of its release with the
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
16 * the linked executables. You must obey the GNU General Public License
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
20 * you do not wish to do so, delete this exception statement from your
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
21 * version. If you delete this exception statement from all source files
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
22 * in the program, then also delete it here.
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
23 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
24 * This program is distributed in the hope that it will be useful, but
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
27 * General Public License for more details.
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
28 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
29 * You should have received a copy of the GNU General Public License
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
31 **/
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
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
3f27814104f7 compile with msvc
jodogne
parents: 398
diff changeset
37 #define NOMINMAX
3f27814104f7 compile with msvc
jodogne
parents: 398
diff changeset
38 #endif
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
39
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include "ZipWriter.h"
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
1278
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
42 #include <limits>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
43 #include <boost/filesystem.hpp>
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 #include <boost/date_time/posix_time/posix_time.hpp>
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
1278
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
46 #include "../../Resources/ThirdParty/minizip/zip.h"
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 #include "../OrthancException.h"
1582
bd1889029cbb encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
48 #include "../Logging.h"
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 static void PrepareFileInfo(zip_fileinfo& zfi)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 memset(&zfi, 0, sizeof(zfi));
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 using namespace boost::posix_time;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 ptime now = second_clock::local_time();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 boost::gregorian::date today = now.date();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 ptime midnight(today);
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 time_duration sinceMidnight = now - midnight;
3128
972cc98959a3 fix build of civetweb for Visual Studio 2008 and LSB
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
62 zfi.tmz_date.tm_sec = static_cast<unsigned int>(sinceMidnight.seconds()); // seconds after the minute - [0,59]
972cc98959a3 fix build of civetweb for Visual Studio 2008 and LSB
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
63 zfi.tmz_date.tm_min = static_cast<unsigned int>(sinceMidnight.minutes()); // minutes after the hour - [0,59]
972cc98959a3 fix build of civetweb for Visual Studio 2008 and LSB
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
64 zfi.tmz_date.tm_hour = static_cast<unsigned int>(sinceMidnight.hours()); // hours since midnight - [0,23]
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_day.html
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 zfi.tmz_date.tm_mday = today.day(); // day of the month - [1,31]
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_month.html
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 zfi.tmz_date.tm_mon = today.month() - 1; // months since January - [0,11]
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_year.html
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 zfi.tmz_date.tm_year = today.year(); // years - [1980..2044]
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 namespace Orthanc
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 struct ZipWriter::PImpl
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 };
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 ZipWriter::~ZipWriter()
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 Close();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 void ZipWriter::Close()
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 if (IsOpen())
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 zipClose(pimpl_->file_, "Created by Orthanc");
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 pimpl_->file_ = NULL;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 hasFileInZip_ = false;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 bool ZipWriter::IsOpen() const
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 return pimpl_->file_ != NULL;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 void ZipWriter::Open()
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 if (IsOpen())
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 return;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if (path_.size() == 0)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
133 int mode = APPEND_STATUS_CREATE;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
134 if (append_ &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
135 boost::filesystem::exists(path_))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
136 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
137 mode = APPEND_STATUS_ADDINZIP;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
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
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 if (!pimpl_->file_)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
3184
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
151 throw OrthancException(ErrorCode_CannotWriteFile,
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
152 "Cannot create new ZIP archive: " + path_);
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 void ZipWriter::SetOutputPath(const char* path)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 Close();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 path_ = path;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
162 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
163 {
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
164 Close();
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
165 isZip64_ = isZip64;
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
166 }
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
167
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 void ZipWriter::SetCompressionLevel(uint8_t level)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 if (level >= 10)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
172 throw OrthancException(ErrorCode_ParameterOutOfRange,
3181
6fd38327e777 Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3128
diff changeset
173 "ZIP compression level must be between 0 (no compression) "
6fd38327e777 Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3128
diff changeset
174 "and 9 (highest compression)");
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
177 Close();
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 compressionLevel_ = level;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
249
5694365ecb96 fixes for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
181 void ZipWriter::OpenFile(const char* path)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 Open();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 zip_fileinfo zfi;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 PrepareFileInfo(zfi);
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
188 int result;
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 if (isZip64_)
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 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
193 &zfi,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
194 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
195 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
196 "", // Comment
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
197 Z_DEFLATED,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
198 compressionLevel_, 1);
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 else
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
201 {
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
202 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
203 &zfi,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
204 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
205 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
206 "", // Comment
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
207 Z_DEFLATED,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
208 compressionLevel_);
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
209 }
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
210
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
211 if (result != 0)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 {
3184
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
213 throw OrthancException(ErrorCode_CannotWriteFile,
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
214 "Cannot add new file inside ZIP archive: " + std::string(path));
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 hasFileInZip_ = true;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 void ZipWriter::Write(const std::string& data)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 if (data.size())
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 Write(&data[0], data.size());
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 void ZipWriter::Write(const char* data, size_t length)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 if (!hasFileInZip_)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
234 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Call first OpenFile()");
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 const size_t maxBytesInAStep = std::numeric_limits<int32_t>::max();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 while (length > 0)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 int bytes = static_cast<int32_t>(length <= maxBytesInAStep ? length : maxBytesInAStep);
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 if (zipWriteInFileInZip(pimpl_->file_, data, bytes))
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
3184
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
245 throw OrthancException(ErrorCode_CannotWriteFile,
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
246 "Cannot write data to ZIP archive: " + path_);
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 data += bytes;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 length -= bytes;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 }
1277
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
253
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
254
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
255 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
256 {
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
257 Close();
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
258 append_ = append;
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
259 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 }