HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
zipFile.h
Go to the documentation of this file.
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 
36 
37 class ArAsset;
38 
39 /// \class UsdZipFile
40 ///
41 /// Class for reading a zip file. This class is primarily intended to support
42 /// the .usdz file format. It is not a general-purpose zip reader, as it does
43 /// not implement the full zip file specification. In particular:
44 ///
45 /// - This class does not natively support decompressing data from a zip
46 /// archive. Clients may access the data exactly as stored in the file and
47 /// perform their own decompression if desired.
48 ///
49 /// - This class does not rely on the central directory in order to read the
50 /// contents of the file. This allows it to operate on partial zip archives.
51 /// However, this also means it may handle certain zip files incorrectly.
52 /// For example, if a file was deleted from a zip archive by just removing
53 /// its central directory header, that file will still be found by this
54 /// class.
55 ///
57 {
58 private:
59  class _Impl;
60 
61 public:
62  /// Opens the zip archive at \p filePath.
63  /// Returns invalid object on error.
64  USD_API
65  static UsdZipFile Open(const std::string& filePath);
66 
67  /// Opens the zip archive \p asset.
68  /// Returns invalid object on error.
69  USD_API
70  static UsdZipFile Open(const std::shared_ptr<ArAsset>& asset);
71 
72  /// Create an invalid UsdZipFile object.
73  USD_API
74  UsdZipFile();
75 
76  USD_API
77  ~UsdZipFile();
78 
79  /// Return true if this object is valid, false otherwise.
80  USD_API
81  explicit operator bool() const { return static_cast<bool>(_impl); }
82 
83  /// \class FileInfo
84  /// Information for a file in the zip archive.
85  class FileInfo
86  {
87  public:
88  /// Offset of the beginning of this file's data from the start of
89  /// the zip archive.
90  size_t dataOffset = 0;
91 
92  /// Size of this file as stored in the zip archive. If this file is
93  /// compressed, this is its compressed size. Otherwise, this is the
94  /// same as the uncompressed size.
95  size_t size = 0;
96 
97  /// Uncompressed size of this file. This may not be the same as the
98  /// size of the file as stored in the zip archive.
99  size_t uncompressedSize = 0;
100 
101  /// CRC-32 value of the uncompressed file.
102  size_t crc = 0;
103 
104  /// Compression method for this file. See section 4.4.5 of the zip
105  /// file specification for valid values. In particular, a value of 0
106  /// means this file is stored with no compression.
107  uint16_t compressionMethod = 0;
108 
109  /// Whether or not this file is encrypted.
110  bool encrypted = false;
111  };
112 
113  /// \class Iterator
114  /// Iterator for traversing and inspecting the contents of the zip archive.
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;
149  using pointer = _ArrowProxy;
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 
163  /// Returns filename of the current file in the zip archive.
164  USD_API
165  reference operator*() const;
166 
167  /// Returns filename of the current file in the zip archive.
168  USD_API
169  pointer operator->() const;
170 
171  /// Returns pointer to the beginning of the current file in the
172  /// zip archive. The contents of the current file span the range
173  /// [GetFile(), GetFile() + GetFileInfo().size).
174  ///
175  /// Note that this points to the raw data stored in the zip archive;
176  /// no decompression or other transformation is applied.
177  USD_API
178  const char* GetFile() const;
179 
180  /// Returns FileInfo object containing information about the
181  /// current file.
182  USD_API
183  FileInfo GetFileInfo() const;
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 
193  /// Returns iterator pointing to the first file in the zip archive.
194  USD_API
195  Iterator begin() const;
196 
197  /// Returns iterator pointing to the first file in the zip archive.
198  Iterator cbegin() const { return begin(); }
199 
200  /// Returns end iterator for this zip archive.
201  USD_API
202  Iterator end() const;
203 
204  /// Returns end iterator for this zip archive.
205  Iterator cend() const { return end(); }
206 
207  /// Returns iterator to the file with the given \p path in this zip
208  /// archive, or end() if no such file exists.
209  USD_API
210  Iterator Find(const std::string& path) const;
211 
212  /// Print out listing of contents of this zip archive to stdout.
213  /// For diagnostic purposes only.
214  USD_API
215  void DumpContents() const;
216 
217 private:
218  UsdZipFile(std::shared_ptr<_Impl>&& impl);
219 
220  std::shared_ptr<_Impl> _impl;
221 };
222 
223 /// \class UsdZipFileWriter
224 ///
225 /// Class for writing a zip file. This class is primarily intended to support
226 /// the .usdz file format. It is not a general-purpose zip writer, as it does
227 /// not implement the full zip file specification. However, all files written
228 /// by this class should be valid zip files and readable by external zip
229 /// libraries and utilities.
230 ///
232 {
233 public:
234  /// Create a new file writer with \p filePath as the destination file path
235  /// where the zip archive will be written. The zip file will not be written
236  /// to \p filePath until the writer is destroyed or Save() is called.
237  ///
238  /// Returns an invalid object on error.
239  USD_API
240  static UsdZipFileWriter CreateNew(const std::string& filePath);
241 
242  /// Create an invalid UsdZipFileWriter object.
243  USD_API
245 
246  /// Calls Save()
247  USD_API
249 
250  UsdZipFileWriter(const UsdZipFileWriter&) = delete;
251  UsdZipFileWriter& operator=(const UsdZipFileWriter&) = delete;
252 
253  USD_API
254  UsdZipFileWriter(UsdZipFileWriter&& rhs);
255  USD_API
256  UsdZipFileWriter& operator=(UsdZipFileWriter&& rhs);
257 
258  /// Returns true if this is a valid object, false otherwise.
259  USD_API
260  explicit operator bool() const { return static_cast<bool>(_impl); }
261 
262  /// Adds the file at \p filePath to the zip archive with no compression
263  /// applied. If \p filePathInArchive is non-empty, the file will be
264  /// added at that path in the archive. Otherwise, it will be added
265  /// at \p filePath.
266  ///
267  /// Returns the file path used to identify the file in the zip archive
268  /// on success. This path conforms to the zip file specification and may
269  /// not be the same as \p filePath or \p filePathInArchive. Returns an
270  /// empty string on failure.
271  USD_API
272  std::string AddFile(const std::string& filePath,
273  const std::string& filePathInArchive = std::string());
274 
275  /// Finalizes the zip archive and saves it to the destination file path.
276  /// Once saved, the file writer is invalid and may not be reused.
277  /// Returns true on success, false otherwise.
278  USD_API
279  bool Save();
280 
281  /// Discards the zip archive so that it is not saved to the destination
282  /// file path. Once discarded, the file writer is invalid and may not be
283  /// reused.
284  USD_API
285  void Discard();
286 
287 private:
288  class _Impl;
289  UsdZipFileWriter(std::unique_ptr<_Impl>&& impl);
290 
291  std::unique_ptr<_Impl> _impl;
292 };
293 
295 
296 #endif // PXR_USD_USD_ZIP_FILE_H
USD_API Iterator begin() const
Returns iterator pointing to the first file in the zip archive.
#define USD_API
Definition: api.h:40
std::ptrdiff_t difference_type
Definition: zipFile.h:147
USD_API void Discard()
static USD_API UsdZipFileWriter CreateNew(const std::string &filePath)
size_t crc
CRC-32 value of the uncompressed file.
Definition: zipFile.h:102
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
USD_API bool operator==(const Iterator &rhs) const
_ArrowProxy pointer
Definition: zipFile.h:149
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
Definition: asset.h:44
USD_API UsdZipFileWriter()
Create an invalid UsdZipFileWriter object.
GLdouble s
Definition: glad.h:3009
Iterator cend() const
Returns end iterator for this zip archive.
Definition: zipFile.h:205
USD_API bool Save()
USD_API ~UsdZipFileWriter()
Calls Save()
USD_API Iterator & operator=(const Iterator &rhs)
USD_API bool operator!=(const Iterator &rhs) const
USD_API Iterator Find(const std::string &path) const
USD_API FileInfo GetFileInfo() const
std::string reference
Definition: zipFile.h:150
std::string value_type
Definition: zipFile.h:148
USD_API void DumpContents() const
USD_API const char * GetFile() const
Iterator cbegin() const
Returns iterator pointing to the first file in the zip archive.
Definition: zipFile.h:198
GLintptr offset
Definition: glcorearb.h:665
USD_API UsdZipFile()
Create an invalid UsdZipFile object.
USD_API std::string AddFile(const std::string &filePath, const std::string &filePathInArchive=std::string())
uint16_t compressionMethod
Definition: zipFile.h:107
USD_API reference operator*() const
Returns filename of the current file in the zip archive.
USD_API pointer operator->() const
Returns filename of the current file in the zip archive.
static USD_API UsdZipFile Open(const std::string &filePath)
USD_API ~UsdZipFile()
USD_API Iterator end() const
Returns end iterator for this zip archive.
GLsizeiptr size
Definition: glcorearb.h:664
bool encrypted
Whether or not this file is encrypted.
Definition: zipFile.h:110
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
size_t uncompressedSize
Definition: zipFile.h:99
UsdZipFileWriter & operator=(const UsdZipFileWriter &)=delete
std::forward_iterator_tag iterator_category
Definition: zipFile.h:151
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
USD_API Iterator & operator++()