Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Compression/HierarchicalZipWriter.h @ 4733:1db3b79d97bd
Error code and description of jobs are now saved into the Orthanc database
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 28 Jun 2021 14:25:37 +0200 |
parents | ac66afbdda58 |
children | 7053502fbf97 |
rev | line source |
---|---|
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1277
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4300
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * 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
|
8 * 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
|
9 * 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
|
10 * the License, or (at your option) any later version. |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * 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
|
15 * Lesser General Public License for more details. |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 **/ |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 #pragma once |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 #include "ZipWriter.h" |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 #include <map> |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 #include <list> |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 #include <boost/lexical_cast.hpp> |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 |
2388
50cde8246542
CMake to build the Orthanc framework without the Orthanc server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
31 #if ORTHANC_BUILD_UNIT_TESTS == 1 |
50cde8246542
CMake to build the Orthanc framework without the Orthanc server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
32 # include <gtest/gtest_prod.h> |
50cde8246542
CMake to build the Orthanc framework without the Orthanc server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
33 #endif |
50cde8246542
CMake to build the Orthanc framework without the Orthanc server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
34 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 namespace Orthanc |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 { |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3945
diff
changeset
|
37 class ORTHANC_PUBLIC HierarchicalZipWriter : public boost::noncopyable |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 { |
265 | 39 #if ORTHANC_BUILD_UNIT_TESTS == 1 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 FRIEND_TEST(HierarchicalZipWriter, Index); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 FRIEND_TEST(HierarchicalZipWriter, Filenames); |
265 | 42 #endif |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 private: |
4026
05a363186da6
ORTHANC_BUILDING_FRAMEWORK_LIBRARY, Orthanc::InitializeFramework()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3992
diff
changeset
|
45 class ORTHANC_PUBLIC Index |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 private: |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 struct Directory |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 typedef std::map<std::string, unsigned int> Content; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 std::string name_; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 Content content_; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 }; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 typedef std::list<Directory*> Stack; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 Stack stack_; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 std::string EnsureUniqueFilename(const char* filename); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 public: |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 Index(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 ~Index(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 |
4300 | 67 bool IsRoot() const; |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 |
249 | 69 std::string OpenFile(const char* name); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 |
249 | 71 void OpenDirectory(const char* name); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 void CloseDirectory(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 |
1121
82567bac5e25
Creation of ZIP archives for media storage, with DICOMDIR
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
75 std::string GetCurrentDirectoryPath() const; |
82567bac5e25
Creation of ZIP archives for media storage, with DICOMDIR
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
76 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 static std::string KeepAlphanumeric(const std::string& source); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 }; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 Index indexer_; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 ZipWriter writer_; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 public: |
4199 | 84 explicit HierarchicalZipWriter(const char* path); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 |
4672
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
86 HierarchicalZipWriter(ZipWriter::IOutputStream* stream, // transfers ownership |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
87 bool isZip64); |
4670
b12faca76a52
support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
88 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 ~HierarchicalZipWriter(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
91 void SetZip64(bool isZip64); |
646 | 92 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
93 bool IsZip64() const; |
646 | 94 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
95 void SetCompressionLevel(uint8_t level); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
97 uint8_t GetCompressionLevel() const; |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
99 void SetAppendToExisting(bool append); |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1121
diff
changeset
|
100 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
101 bool IsAppendToExisting() const; |
1277
46bca019587e
primitives to add new content to existing ZIP files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1121
diff
changeset
|
102 |
249 | 103 void OpenFile(const char* name); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 |
249 | 105 void OpenDirectory(const char* name); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
107 void CloseDirectory(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
109 std::string GetCurrentDirectoryPath() const; |
1121
82567bac5e25
Creation of ZIP archives for media storage, with DICOMDIR
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
110 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
111 void Write(const void* data, size_t length); |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 |
4296
3b70a2e6a06c
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
113 void Write(const std::string& data); |
4670
b12faca76a52
support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
114 |
b12faca76a52
support of output streams in ZipWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
115 // The lifetime of the "target" buffer must be larger than that of HierarchicalZipWriter |
4672
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
116 static HierarchicalZipWriter* CreateToMemory(std::string& target, |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
117 bool isZip64); |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
118 |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
119 void CancelStream(); |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
120 |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
121 void Close(); |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4670
diff
changeset
|
122 |
4673 | 123 uint64_t GetArchiveSize() const; |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 }; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 } |