comparison Core/Compression/HierarchicalZipWriter.cpp @ 247:c9b3ba0fd140

path management in zip files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Dec 2012 17:27:23 +0100
parents
children 2442033911d6
comparison
equal deleted inserted replaced
246:fe6ba20d00a8 247:c9b3ba0fd140
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "HierarchicalZipWriter.h"
34
35 #include "../Toolbox.h"
36 #include "../OrthancException.h"
37
38 #include <boost/lexical_cast.hpp>
39
40 namespace Orthanc
41 {
42 std::string HierarchicalZipWriter::Index::KeepAlphanumeric(const std::string& source)
43 {
44 std::string result;
45
46 bool lastSpace = false;
47
48 result.reserve(source.size());
49 for (size_t i = 0; i < source.size(); i++)
50 {
51 if (source[i] < 128 &&
52 source[i] >= 0)
53 {
54 if (isspace(source[i]))
55 {
56 if (!lastSpace)
57 {
58 lastSpace = true;
59 result.push_back(' ');
60 }
61 }
62 else if (isalnum(source[i]))
63 {
64 result.push_back(source[i]);
65 lastSpace = false;
66 }
67 }
68 }
69
70 return Toolbox::StripSpaces(result);
71 }
72
73 std::string HierarchicalZipWriter::Index::GetCurrentDirectoryPath() const
74 {
75 std::string result;
76
77 Stack::const_iterator it = stack_.begin();
78 it++; // Skip the root node (to avoid absolute paths)
79
80 while (it != stack_.end())
81 {
82 result += (*it)->name_ + "/";
83 it++;
84 }
85
86 return result;
87 }
88
89 std::string HierarchicalZipWriter::Index::EnsureUniqueFilename(const char* filename)
90 {
91 std::string standardized = KeepAlphanumeric(filename);
92
93 Directory& d = *stack_.back();
94 Directory::Content::iterator it = d.content_.find(standardized);
95
96 if (it == d.content_.end())
97 {
98 d.content_[standardized] = 1;
99 return standardized;
100 }
101 else
102 {
103 it->second++;
104 return standardized + "-" + boost::lexical_cast<std::string>(it->second);
105 }
106 }
107
108 HierarchicalZipWriter::Index::Index()
109 {
110 stack_.push_back(new Directory);
111 }
112
113 HierarchicalZipWriter::Index::~Index()
114 {
115 for (Stack::iterator it = stack_.begin(); it != stack_.end(); it++)
116 {
117 delete *it;
118 }
119 }
120
121 std::string HierarchicalZipWriter::Index::CreateFile(const char* name)
122 {
123 return GetCurrentDirectoryPath() + EnsureUniqueFilename(name);
124 }
125
126 void HierarchicalZipWriter::Index::CreateDirectory(const char* name)
127 {
128 std::string d = EnsureUniqueFilename(name);
129
130 // Push the new directory onto the stack
131 stack_.push_back(new Directory);
132 stack_.back()->name_ = d;
133 }
134
135 void HierarchicalZipWriter::Index::CloseDirectory()
136 {
137 if (IsRoot())
138 {
139 // Cannot close the root node
140 throw OrthancException(ErrorCode_BadSequenceOfCalls);
141 }
142
143 delete stack_.back();
144 stack_.pop_back();
145 }
146
147
148 HierarchicalZipWriter::HierarchicalZipWriter(const char* path)
149 {
150 writer_.SetOutputPath(path);
151 writer_.Open();
152 }
153
154 HierarchicalZipWriter::~HierarchicalZipWriter()
155 {
156 writer_.Close();
157 }
158
159 void HierarchicalZipWriter::CreateFile(const char* name)
160 {
161 std::string p = indexer_.CreateFile(name);
162 writer_.CreateFileInZip(p.c_str());
163 }
164
165 void HierarchicalZipWriter::CreateDirectory(const char* name)
166 {
167 indexer_.CreateDirectory(name);
168 }
169
170 void HierarchicalZipWriter::CloseDirectory()
171 {
172 indexer_.CloseDirectory();
173 }
174 }