annotate OrthancFramework/Sources/Compression/ZipWriter.cpp @ 5560:c80dbbae3f60

Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
author Alain Mazy <am@orthanc.team>
date Mon, 22 Apr 2024 10:50:33 +0200
parents 48b8dae6dc77
children f7adfb22e20e
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
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
7 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
8 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
11 * the License, or (at your option) any later version.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
12 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
13 * 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
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
16 * Lesser General Public License for more details.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
17 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
20 * <http://www.gnu.org/licenses/>.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
21 **/
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
22
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
23
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
24 #include "../PrecompiledHeaders.h"
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
25
658
e8e59e80868c note about glog-0.3.3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 644
diff changeset
26 #ifndef NOMINMAX
568
3f27814104f7 compile with msvc
jodogne
parents: 398
diff changeset
27 #define NOMINMAX
3f27814104f7 compile with msvc
jodogne
parents: 398
diff changeset
28 #endif
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
29
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include "ZipWriter.h"
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
1278
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
32 #include <limits>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
33 #include <boost/filesystem.hpp>
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include <boost/date_time/posix_time/posix_time.hpp>
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
1278
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1277
diff changeset
36 #include "../../Resources/ThirdParty/minizip/zip.h"
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
37 #include "../Logging.h"
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include "../OrthancException.h"
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
39 #include "../SystemToolbox.h"
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 static void PrepareFileInfo(zip_fileinfo& zfi)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 memset(&zfi, 0, sizeof(zfi));
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 using namespace boost::posix_time;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 ptime now = second_clock::local_time();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 boost::gregorian::date today = now.date();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 ptime midnight(today);
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 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
53 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
54 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
55 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
56
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 // 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
58 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
59
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 // 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
61 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
62
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 // 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
64 zfi.tmz_date.tm_year = today.year(); // years - [1980..2044]
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
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
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 namespace Orthanc
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
71 ZipWriter::MemoryStream::MemoryStream(std::string& target) :
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
72 target_(target),
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
73 archiveSize_(0)
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
74 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
75 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
76
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
77
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
78 void ZipWriter::MemoryStream::Write(const std::string& chunk)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
79 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
80 chunked_.AddChunk(chunk);
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
81 archiveSize_ += chunk.size();
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
82 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
83
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
84
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
85 uint64_t ZipWriter::MemoryStream::GetArchiveSize() const
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
86 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
87 return archiveSize_;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
88 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
89
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
90
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
91 void ZipWriter::MemoryStream::Close()
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
92 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
93 chunked_.Flatten(target_);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
94 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
95
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
96
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
97 void ZipWriter::BufferWithSeek::CheckInvariants() const
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
98 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
99 #if !defined(NDEBUG)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
100 assert(chunks_.GetNumBytes() == 0 ||
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
101 flattened_.empty());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
102
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
103 assert(currentPosition_ <= GetSize());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
104
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
105 if (currentPosition_ < GetSize())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
106 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
107 assert(chunks_.GetNumBytes() == 0);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
108 assert(!flattened_.empty());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
109 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
110 #endif
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
111 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
112
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
113
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
114 ZipWriter::BufferWithSeek::BufferWithSeek() :
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
115 currentPosition_(0)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
116 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
117 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
118 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
119
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
120
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
121 ZipWriter::BufferWithSeek::~BufferWithSeek()
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
122 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
123 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
124 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
125
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
126
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
127 size_t ZipWriter::BufferWithSeek::GetPosition() const
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
128 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
129 return currentPosition_;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
130 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
131
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
132
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
133 size_t ZipWriter::BufferWithSeek::GetSize() const
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
134 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
135 if (flattened_.empty())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
136 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
137 return chunks_.GetNumBytes();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
138 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
139 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
140 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
141 return flattened_.size();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
142 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
143 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
144
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
145
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
146 void ZipWriter::BufferWithSeek::Write(const void* data,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
147 size_t size)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
148 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
149 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
150
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
151 if (size != 0)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
152 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
153 if (currentPosition_ < GetSize())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
154 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
155 if (currentPosition_ + size > flattened_.size())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
156 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
157 throw OrthancException(ErrorCode_ParameterOutOfRange);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
158 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
159 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
160 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
161 memcpy(&flattened_[currentPosition_], data, size);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
162 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
163 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
164 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
165 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
166 if (!flattened_.empty())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
167 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
168 assert(chunks_.GetNumBytes() == 0);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
169 chunks_.AddChunk(flattened_);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
170 flattened_.clear();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
171 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
172
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
173 chunks_.AddChunk(data, size);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
174 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
175
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
176 currentPosition_ += size;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
177 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
178
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
179 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
180 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
181
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
182
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
183 void ZipWriter::BufferWithSeek::Write(const std::string& data)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
184 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
185 if (!data.empty())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
186 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
187 Write(data.c_str(), data.size());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
188 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
189 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
190
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
191
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
192 void ZipWriter::BufferWithSeek::Seek(size_t position)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
193 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
194 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
195
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
196 if (currentPosition_ != position)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
197 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
198 if (position < GetSize())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
199 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
200 if (chunks_.GetNumBytes() != 0)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
201 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
202 assert(flattened_.empty());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
203 chunks_.Flatten(flattened_);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
204 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
205
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
206 assert(chunks_.GetNumBytes() == 0);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
207 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
208 else if (position > GetSize())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
209 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
210 throw OrthancException(ErrorCode_ParameterOutOfRange);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
211 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
212
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
213 currentPosition_ = position;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
214 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
215
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
216 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
217 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
218
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
219
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
220 void ZipWriter::BufferWithSeek::Flush(std::string& target)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
221 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
222 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
223
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
224 if (flattened_.empty())
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
225 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
226 chunks_.Flatten(target);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
227 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
228 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
229 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
230 flattened_.swap(target);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
231 flattened_.clear();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
232 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
233
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
234 currentPosition_ = 0;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
235
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
236 CheckInvariants();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
237 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
238
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
239
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
240 /**
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
241 * Inside a ZIP archive, compressed files are concatenated, each
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
242 * file being prefixed by its "Local file header". The ZIP archive
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
243 * ends with the "central directory" structure.
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
244 * https://en.wikipedia.org/wiki/ZIP_(file_format)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
245 *
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
246 * When writing one file, the minizip implementation first TELLS to
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
247 * know the current size of the archive, then WRITES the header and
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
248 * data bytes, then SEEKS backward to update the "local file header"
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
249 * with info about the compressed data (at the 14 offset, containing
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
250 * CRC-32, compressed size and uncompressed size), and finally SEEKS
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
251 * to get back at the end of the stream in order to continue adding
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
252 * files.
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
253 *
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
254 * The minizip implementation will *never* SEEK *before* the "local
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
255 * file header" of the current file. However, the current file must
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
256 * *not* be immediately sent to the stream as new bytes are written,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
257 * because the "local file header" will be updated.
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
258 *
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
259 * Consequently, this buffer class only sends the pending bytes to
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
260 * the output stream once it receives a SEEK command that moves the
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
261 * cursor at the end of the archive. In the minizip implementation,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
262 * such a SEEK indicates that the current file has been properly
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
263 * added to the archive.
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
264 **/
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
265 class ZipWriter::StreamBuffer : public boost::noncopyable
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
266 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
267 private:
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
268 IOutputStream& stream_;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
269 bool success_;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
270 ZPOS64_T startCurrentFile_;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
271 BufferWithSeek buffer_;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
272
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
273 public:
4690
13efc0967cea cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4673
diff changeset
274 explicit StreamBuffer(IOutputStream& stream) :
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
275 stream_(stream),
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
276 success_(true),
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
277 startCurrentFile_(0)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
278 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
279 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
280
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
281 int Close()
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
282 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
283 try
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
284 {
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
285 if (success_)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
286 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
287 std::string s;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
288 buffer_.Flush(s);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
289 stream_.Write(s);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
290 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
291
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
292 return 0;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
293 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
294 catch (...)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
295 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
296 success_ = false;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
297 return 1;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
298 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
299 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
300
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
301 ZPOS64_T Tell() const
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
302 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
303 return startCurrentFile_ + static_cast<ZPOS64_T>(buffer_.GetPosition());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
304 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
305
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
306 uLong Write(const void* buf,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
307 uLong size)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
308 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
309 if (size == 0)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
310 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
311 return 0;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
312 }
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
313 else if (!success_)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
314 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
315 return 0; // Error
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
316 }
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
317 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
318 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
319 try
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
320 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
321 buffer_.Write(buf, size);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
322 return size;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
323 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
324 catch (...)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
325 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
326 return 0;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
327 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
328 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
329 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
330
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
331
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
332 long Seek(ZPOS64_T offset,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
333 int origin)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
334 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
335 try
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
336 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
337 if (origin == ZLIB_FILEFUNC_SEEK_SET &&
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
338 offset >= startCurrentFile_ &&
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
339 success_)
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
340 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
341 ZPOS64_T fullSize = startCurrentFile_ + static_cast<ZPOS64_T>(buffer_.GetSize());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
342 assert(offset <= fullSize);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
343
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
344 if (offset == fullSize)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
345 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
346 // We can flush to the output stream
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
347 std::string s;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
348 buffer_.Flush(s);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
349 stream_.Write(s);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
350 startCurrentFile_ = fullSize;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
351 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
352 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
353 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
354 buffer_.Seek(offset - startCurrentFile_);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
355 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
356
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
357 return 0; // OK
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
358 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
359 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
360 {
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
361 return 1;
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
362 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
363 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
364 catch (...)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
365 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
366 return 1;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
367 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
368 }
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
369
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
370
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
371 void Cancel()
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
372 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
373 success_ = false;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
374 }
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
375
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
376
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
377 static int CloseWrapper(voidpf opaque,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
378 voidpf stream)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
379 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
380 assert(opaque != NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
381 return reinterpret_cast<StreamBuffer*>(opaque)->Close();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
382 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
383
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
384 static voidpf OpenWrapper(voidpf opaque,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
385 const void* filename,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
386 int mode)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
387 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
388 assert(opaque != NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
389 return opaque;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
390 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
391
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
392 static long SeekWrapper(voidpf opaque,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
393 voidpf stream,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
394 ZPOS64_T offset,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
395 int origin)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
396 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
397 assert(opaque != NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
398 return reinterpret_cast<StreamBuffer*>(opaque)->Seek(offset, origin);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
399 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
400
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
401 static ZPOS64_T TellWrapper(voidpf opaque,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
402 voidpf stream)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
403 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
404 assert(opaque != NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
405 return reinterpret_cast<StreamBuffer*>(opaque)->Tell();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
406 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
407
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
408 static int TestErrorWrapper(voidpf opaque,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
409 voidpf stream)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
410 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
411 assert(opaque != NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
412 return reinterpret_cast<StreamBuffer*>(opaque)->success_ ? 0 : 1;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
413 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
414
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
415 static uLong WriteWrapper(voidpf opaque,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
416 voidpf stream,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
417 const void* buf,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
418 uLong size)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
419 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
420 assert(opaque != NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
421 return reinterpret_cast<StreamBuffer*>(opaque)->Write(buf, size);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
422 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
423 };
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
424
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
425
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
426 struct ZipWriter::PImpl : public boost::noncopyable
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 zipFile file_;
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
429 std::unique_ptr<StreamBuffer> streamBuffer_;
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
430 uint64_t archiveSize_;
1277
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
431
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
432 PImpl() :
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
433 file_(NULL),
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
434 archiveSize_(0)
1277
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
435 {
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
436 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 };
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
1277
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
439 ZipWriter::ZipWriter() :
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
440 pimpl_(new PImpl),
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
441 isZip64_(false),
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
442 hasFileInZip_(false),
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
443 append_(false),
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
444 compressionLevel_(6)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 ZipWriter::~ZipWriter()
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 {
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
450 try
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
451 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
452 Close();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
453 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
454 catch (OrthancException& e) // Don't throw exceptions in destructors
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
455 {
4751
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4690
diff changeset
456 LOG(ERROR) << "Caught exception in destructor: " << e.What();
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
457 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 void ZipWriter::Close()
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 if (IsOpen())
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 zipClose(pimpl_->file_, "Created by Orthanc");
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 pimpl_->file_ = NULL;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 hasFileInZip_ = false;
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
467
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
468 pimpl_->streamBuffer_.reset(NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
469
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
470 if (outputStream_.get() != NULL)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
471 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
472 outputStream_->Close();
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
473 pimpl_->archiveSize_ = outputStream_->GetArchiveSize();
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
474 outputStream_.reset(NULL);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
475 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 bool ZipWriter::IsOpen() const
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 return pimpl_->file_ != NULL;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 void ZipWriter::Open()
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 if (IsOpen())
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 return;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 }
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
490 else if (outputStream_.get() != NULL)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
491 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
492 // New in Orthanc 1.9.4
4671
42e1f5bde40b added sanity check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4670
diff changeset
493 if (IsAppendToExisting())
42e1f5bde40b added sanity check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4670
diff changeset
494 {
42e1f5bde40b added sanity check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4670
diff changeset
495 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Cannot append to output streams");
42e1f5bde40b added sanity check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4670
diff changeset
496 }
42e1f5bde40b added sanity check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4670
diff changeset
497
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
498 hasFileInZip_ = false;
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
500 zlib_filefunc64_def funcs;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
501 memset(&funcs, 0, sizeof(funcs));
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
502
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
503 pimpl_->streamBuffer_.reset(new StreamBuffer(*outputStream_));
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
504 funcs.opaque = pimpl_->streamBuffer_.get();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
505 funcs.zclose_file = StreamBuffer::CloseWrapper;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
506 funcs.zerror_file = StreamBuffer::TestErrorWrapper;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
507 funcs.zopen64_file = StreamBuffer::OpenWrapper;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
508 funcs.ztell64_file = StreamBuffer::TellWrapper;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
509 funcs.zwrite_file = StreamBuffer::WriteWrapper;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
510 funcs.zseek64_file = StreamBuffer::SeekWrapper;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
511
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
512 /**
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
513 * "funcs.zread_file" (ZREAD64) also appears in "minizip/zip.c",
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
514 * but is only needed by function "LoadCentralDirectoryRecord()"
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
515 * that is only used if appending new files to an already
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
516 * existing ZIP, which makes no sense for an output stream.
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
517 **/
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
518
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
519 pimpl_->file_ = zipOpen2_64(NULL /* no output path */, APPEND_STATUS_CREATE,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
520 NULL /* global comment */, &funcs);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
521
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
522 if (!pimpl_->file_)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
523 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
524 throw OrthancException(ErrorCode_CannotWriteFile,
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
525 "Cannot create new ZIP archive into an output stream");
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
526 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
527 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
528 else if (path_.empty())
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
530 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
531 "Please call SetOutputPath() before creating the file");
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 }
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
533 else
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
534 {
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
535 hasFileInZip_ = false;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
536
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
537 int mode = APPEND_STATUS_CREATE;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
538 if (append_ &&
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
539 boost::filesystem::exists(path_))
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
540 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
541 mode = APPEND_STATUS_ADDINZIP;
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
542 }
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
543
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
544 if (isZip64_)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
545 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
546 pimpl_->file_ = zipOpen64(path_.c_str(), mode);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
547 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
548 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
549 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
550 pimpl_->file_ = zipOpen(path_.c_str(), mode);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
551 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
552
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
553 if (!pimpl_->file_)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
554 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
555 throw OrthancException(ErrorCode_CannotWriteFile,
5560
c80dbbae3f60 Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
Alain Mazy <am@orthanc.team>
parents: 5485
diff changeset
556 "Cannot create new ZIP archive"); // we do not log the path anymore since it can contain PHI
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
557 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 void ZipWriter::SetOutputPath(const char* path)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563 Close();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564 path_ = path;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566
4296
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
567 const std::string &ZipWriter::GetOutputPath() const
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
568 {
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
569 return path_;
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
570 }
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
571
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
572 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
573 {
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
574 if (outputStream_.get() == NULL)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
575 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
576 Close();
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
577 isZip64_ = isZip64;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
578 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
579 else
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
580 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
581 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
582 "SetZip64() must be given to AcquireOutputStream()");
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
583 }
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
584 }
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
585
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 void ZipWriter::SetCompressionLevel(uint8_t level)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 if (level >= 10)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
590 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
591 "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
592 "and 9 (highest compression)");
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
593 }
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
594 else
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
595 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
596 compressionLevel_ = level;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
597 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
598 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
599
4296
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
600 uint8_t ZipWriter::GetCompressionLevel() const
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
601 {
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
602 return compressionLevel_;
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
603 }
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
604
249
5694365ecb96 fixes for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 136
diff changeset
605 void ZipWriter::OpenFile(const char* path)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607 Open();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 zip_fileinfo zfi;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 PrepareFileInfo(zfi);
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611
644
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
612 int result;
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
613
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
614 if (isZip64_)
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
615 {
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
616 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
617 &zfi,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
618 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
619 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
620 "", // Comment
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
621 Z_DEFLATED,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
622 compressionLevel_, 1);
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
623 }
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
624 else
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
625 {
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
626 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
627 &zfi,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
628 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
629 NULL, 0,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
630 "", // Comment
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
631 Z_DEFLATED,
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
632 compressionLevel_);
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
633 }
eb5a0b21d05e do not use ZIP64 as the default format anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 568
diff changeset
634
5560
c80dbbae3f60 Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
Alain Mazy <am@orthanc.team>
parents: 5485
diff changeset
635 if (result != ZIP_OK)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 {
3184
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
637 throw OrthancException(ErrorCode_CannotWriteFile,
5560
c80dbbae3f60 Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
Alain Mazy <am@orthanc.team>
parents: 5485
diff changeset
638 "Cannot add new file inside ZIP archive - error code = " + boost::lexical_cast<std::string>(result)); // we do not log the path anymore since it can contain PHI
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641 hasFileInZip_ = true;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
642 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645 void ZipWriter::Write(const std::string& data)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647 if (data.size())
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649 Write(&data[0], data.size());
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
653
3945
0b3256c3ee14 simplified IDicomTranscoder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
654 void ZipWriter::Write(const void* data, size_t length)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
656 if (!hasFileInZip_)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
658 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Call first OpenFile()");
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661 const size_t maxBytesInAStep = std::numeric_limits<int32_t>::max();
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662
3945
0b3256c3ee14 simplified IDicomTranscoder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
663 const char* p = reinterpret_cast<const char*>(data);
0b3256c3ee14 simplified IDicomTranscoder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
664
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
665 while (length > 0)
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
666 {
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
667 int bytes = static_cast<int32_t>(length <= maxBytesInAStep ? length : maxBytesInAStep);
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
668
5560
c80dbbae3f60 Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
Alain Mazy <am@orthanc.team>
parents: 5485
diff changeset
669 int result = zipWriteInFileInZip(pimpl_->file_, p, bytes);
c80dbbae3f60 Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
Alain Mazy <am@orthanc.team>
parents: 5485
diff changeset
670 if (result != ZIP_OK)
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
671 {
3184
5d1f5984dc41 improved log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3181
diff changeset
672 throw OrthancException(ErrorCode_CannotWriteFile,
5560
c80dbbae3f60 Removed potential PHI from the logs when Orthanc encounters an error while creating a zip file
Alain Mazy <am@orthanc.team>
parents: 5485
diff changeset
673 "Cannot write data to ZIP archive - error code =" + boost::lexical_cast<std::string>(result)); // we do not log the path anymore since it can contain PHI
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
674 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
675
3945
0b3256c3ee14 simplified IDicomTranscoder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
676 p += bytes;
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
677 length -= bytes;
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
678 }
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
679 }
1277
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
680
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
681
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
682 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
683 {
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
684 Close();
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
685 append_ = append;
46bca019587e primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
686 }
4296
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
687
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
688 bool ZipWriter::IsAppendToExisting() const
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
689 {
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
690 return append_;
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
691 }
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
692
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4296
diff changeset
693 bool ZipWriter::IsZip64() const
4296
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
694 {
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
695 return isZip64_;
3b70a2e6a06c moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
696 }
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
697
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
698
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
699 void ZipWriter::AcquireOutputStream(IOutputStream* stream,
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
700 bool isZip64)
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
701 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
702 std::unique_ptr<IOutputStream> protection(stream);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
703
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
704 if (stream == NULL)
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
705 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
706 throw OrthancException(ErrorCode_NullPointer);
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
707 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
708 else
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
709 {
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
710 Close();
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
711 path_.clear();
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
712 isZip64_ = isZip64;
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
713 outputStream_.reset(protection.release());
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
714 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
715 }
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
716
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
717
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
718 void ZipWriter::SetMemoryOutput(std::string& target,
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
719 bool isZip64)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
720 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
721 AcquireOutputStream(new MemoryStream(target), isZip64);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
722 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
723
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
724
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
725 void ZipWriter::CancelStream()
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
726 {
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
727 if (outputStream_.get() == NULL ||
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
728 pimpl_->streamBuffer_.get() == NULL)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
729 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
730 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Only applicable after AcquireOutputStream() and Open()");
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
731 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
732 else
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
733 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
734 pimpl_->streamBuffer_->Cancel();
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
735 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
736 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
737
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
738
4673
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4672
diff changeset
739 uint64_t ZipWriter::GetArchiveSize() const
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
740 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
741 if (outputStream_.get() != NULL)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
742 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
743 return outputStream_->GetArchiveSize();
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
744 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
745 else if (path_.empty())
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
746 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
747 // This is the case after a call to "Close()"
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
748 return pimpl_->archiveSize_;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
749 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
750 else
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
751 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
752 return SystemToolbox::GetFileSize(path_);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4671
diff changeset
753 }
4670
b12faca76a52 support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
754 }
81
0ec5e2e327b1 zip writer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
755 }