Loading...
Searching...
No Matches
zipFile.h
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_USD_USD_ZIP_FILE_H
25#define PXR_USD_USD_ZIP_FILE_H
26
27#include "pxr/pxr.h"
28#include "pxr/usd/usd/api.h"
29
30#include <memory>
31#include <string>
32#include <utility>
33#include <vector>
34
35PXR_NAMESPACE_OPEN_SCOPE
36
37class ArAsset;
38
57{
58private:
59 class _Impl;
60
61public:
64 USD_API
65 static UsdZipFile Open(const std::string& filePath);
66
69 USD_API
70 static UsdZipFile Open(const std::shared_ptr<ArAsset>& asset);
71
73 USD_API
75
76 USD_API
78
80 USD_API
81 explicit operator bool() const { return static_cast<bool>(_impl); }
82
86 {
87 public:
90 size_t dataOffset = 0;
91
95 size_t size = 0;
96
99 size_t uncompressedSize = 0;
100
102 size_t crc = 0;
103
107 uint16_t compressionMethod = 0;
108
110 bool encrypted = false;
111 };
112
115 class Iterator
116 {
117 // Proxy type for operator->(), needed since this iterator's value
118 // is generated on the fly.
119 class _ArrowProxy
120 {
121 public:
122 explicit _ArrowProxy(const std::string& s) : _s(s) { }
123 const std::string* operator->() const { return &_s; }
124 private:
125 std::string _s;
126 };
127
128 public:
129 USD_API
130 Iterator();
131
132 USD_API
133 ~Iterator();
134
135 USD_API
136 Iterator(const Iterator& rhs);
137
138 USD_API
139 Iterator(Iterator&& rhs);
140
141 USD_API
142 Iterator& operator=(const Iterator& rhs);
143
144 USD_API
145 Iterator& operator=(Iterator&& rhs);
146
147 using difference_type = std::ptrdiff_t;
148 using value_type = std::string;
149 using pointer = _ArrowProxy;
150 using reference = std::string;
151 using iterator_category = std::forward_iterator_tag;
152
153 USD_API
154 Iterator& operator++();
155 USD_API
156 Iterator operator++(int);
157
158 USD_API
159 bool operator==(const Iterator& rhs) const;
160 USD_API
161 bool operator!=(const Iterator& rhs) const;
162
164 USD_API
165 reference operator*() const;
166
168 USD_API
169 pointer operator->() const;
170
177 USD_API
178 const char* GetFile() const;
179
182 USD_API
184
185 private:
186 friend class UsdZipFile;
187 Iterator(const _Impl* impl, size_t offset = 0);
188
189 class _IteratorData;
190 std::unique_ptr<_IteratorData> _data;
191 };
192
194 USD_API
196
198 Iterator cbegin() const { return begin(); }
199
201 USD_API
202 Iterator end() const;
203
205 Iterator cend() const { return end(); }
206
209 USD_API
210 Iterator Find(const std::string& path) const;
211
214 USD_API
215 void DumpContents() const;
216
217private:
218 UsdZipFile(std::shared_ptr<_Impl>&& impl);
219
220 std::shared_ptr<_Impl> _impl;
221};
222
232{
233public:
239 USD_API
240 static UsdZipFileWriter CreateNew(const std::string& filePath);
241
243 USD_API
245
247 USD_API
249
250 UsdZipFileWriter(const UsdZipFileWriter&) = delete;
251 UsdZipFileWriter& operator=(const UsdZipFileWriter&) = delete;
252
253 USD_API
255 USD_API
256 UsdZipFileWriter& operator=(UsdZipFileWriter&& rhs);
257
259 USD_API
260 explicit operator bool() const { return static_cast<bool>(_impl); }
261
271 USD_API
272 std::string AddFile(const std::string& filePath,
273 const std::string& filePathInArchive = std::string());
274
278 USD_API
279 bool Save();
280
284 USD_API
285 void Discard();
286
287private:
288 class _Impl;
289 UsdZipFileWriter(std::unique_ptr<_Impl>&& impl);
290
291 std::unique_ptr<_Impl> _impl;
292};
293
294PXR_NAMESPACE_CLOSE_SCOPE
295
296#endif // PXR_USD_USD_ZIP_FILE_H
Interface for accessing the contents of an asset.
Definition: asset.h:44
Iterator for traversing and inspecting the contents of the zip archive.
Definition: zipFile.h:116
USD_API pointer operator->() const
Returns filename of the current file in the zip archive.
USD_API FileInfo GetFileInfo() const
Returns FileInfo object containing information about the current file.
USD_API const char * GetFile() const
Returns pointer to the beginning of the current file in the zip archive.
USD_API reference operator*() const
Returns filename of the current file in the zip archive.
Class for reading a zip file.
Definition: zipFile.h:57
size_t uncompressedSize
Uncompressed size of this file.
Definition: zipFile.h:99
Iterator cend() const
Returns end iterator for this zip archive.
Definition: zipFile.h:205
uint16_t compressionMethod
Compression method for this file.
Definition: zipFile.h:107
USD_API Iterator Find(const std::string &path) const
Returns iterator to the file with the given path in this zip archive, or end() if no such file exists...
Iterator cbegin() const
Returns iterator pointing to the first file in the zip archive.
Definition: zipFile.h:198
static USD_API UsdZipFile Open(const std::string &filePath)
Opens the zip archive at filePath.
size_t size
Size of this file as stored in the zip archive.
Definition: zipFile.h:95
bool encrypted
Whether or not this file is encrypted.
Definition: zipFile.h:110
USD_API UsdZipFile()
Create an invalid UsdZipFile object.
size_t dataOffset
Offset of the beginning of this file's data from the start of the zip archive.
Definition: zipFile.h:90
USD_API Iterator begin() const
Returns iterator pointing to the first file in the zip archive.
static USD_API UsdZipFile Open(const std::shared_ptr< ArAsset > &asset)
Opens the zip archive asset.
USD_API void DumpContents() const
Print out listing of contents of this zip archive to stdout.
USD_API Iterator end() const
Returns end iterator for this zip archive.
size_t crc
CRC-32 value of the uncompressed file.
Definition: zipFile.h:102
Information for a file in the zip archive.
Definition: zipFile.h:86
Class for writing a zip file.
Definition: zipFile.h:232
USD_API ~UsdZipFileWriter()
Calls Save()
USD_API std::string AddFile(const std::string &filePath, const std::string &filePathInArchive=std::string())
Adds the file at filePath to the zip archive with no compression applied.
USD_API UsdZipFileWriter()
Create an invalid UsdZipFileWriter object.
USD_API bool Save()
Finalizes the zip archive and saves it to the destination file path.
USD_API void Discard()
Discards the zip archive so that it is not saved to the destination file path.
static USD_API UsdZipFileWriter CreateNew(const std::string &filePath)
Create a new file writer with filePath as the destination file path where the zip archive will be wri...