All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 USD_ZIPFILE_H
25 #define USD_ZIPFILE_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 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 class ArAsset;
38 
57 {
58 private:
59  class _Impl;
60 
61 public:
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
74  UsdZipFile();
75 
76  USD_API
77  ~UsdZipFile();
78 
80  USD_API
81  explicit operator bool() const { return static_cast<bool>(_impl); }
82 
85  class FileInfo
86  {
87  public:
90  size_t dataOffset = 0;
91 
95  size_t size = 0;
96 
99  size_t uncompressedSize = 0;
100 
104  uint16_t compressionMethod = 0;
105 
107  bool encrypted = false;
108  };
109 
112  class Iterator
113  {
114  public:
115  USD_API
116  Iterator();
117 
118  // Proxy type for operator->(), needed since this iterator's value
119  // is generated on the fly.
120  class _ArrowProxy
121  {
122  public:
123  explicit _ArrowProxy(const std::string& s) : _s(s) { }
124  const std::string* operator->() const { return &_s; }
125  private:
126  std::string _s;
127  };
128 
129  using difference_type = std::ptrdiff_t;
130  using value_type = std::string;
131  using pointer = _ArrowProxy;
132  using reference = std::string;
133  using iterator_category = std::forward_iterator_tag;
134 
135  USD_API
136  Iterator& operator++();
137  USD_API
138  Iterator operator++(int);
139 
140  USD_API
141  bool operator==(const Iterator& rhs) const;
142  USD_API
143  bool operator!=(const Iterator& rhs) const;
144 
146  USD_API
147  reference operator*() const;
148 
150  USD_API
151  pointer operator->() const;
152 
159  USD_API
160  const char* GetFile() const;
161 
164  USD_API
165  FileInfo GetFileInfo() const;
166 
167  private:
168  friend class UsdZipFile;
169  Iterator(const _Impl* impl);
170 
171  const _Impl* _impl;
172  size_t _offset;
173  };
174 
176  USD_API
177  Iterator begin() const;
178 
180  Iterator cbegin() const { return begin(); }
181 
183  USD_API
184  Iterator end() const;
185 
187  Iterator cend() const { return end(); }
188 
191  USD_API
192  Iterator Find(const std::string& path) const;
193 
196  USD_API
197  void DumpContents() const;
198 
199 private:
200  UsdZipFile(std::shared_ptr<_Impl>&& impl);
201 
202  std::shared_ptr<_Impl> _impl;
203 };
204 
214 {
215 public:
221  USD_API
222  static UsdZipFileWriter CreateNew(const std::string& filePath);
223 
225  USD_API
227 
229  USD_API
231 
232  UsdZipFileWriter(const UsdZipFileWriter&) = delete;
233  UsdZipFileWriter& operator=(const UsdZipFileWriter&) = delete;
234 
235  USD_API
236  UsdZipFileWriter(UsdZipFileWriter&& rhs);
237  USD_API
238  UsdZipFileWriter& operator=(UsdZipFileWriter&& rhs);
239 
241  USD_API
242  explicit operator bool() const { return static_cast<bool>(_impl); }
243 
253  USD_API
254  std::string AddFile(const std::string& filePath,
255  const std::string& filePathInArchive = std::string());
256 
260  USD_API
261  bool Save();
262 
266  USD_API
267  void Discard();
268 
269 private:
270  class _Impl;
271  UsdZipFileWriter(std::unique_ptr<_Impl>&& impl);
272 
273  std::unique_ptr<_Impl> _impl;
274 };
275 
276 PXR_NAMESPACE_CLOSE_SCOPE
277 
278 #endif // USD_ZIPFILE_H
USD_API bool Save()
Finalizes the zip archive and saves it to the destination file path.
USD_API UsdZipFile()
Create an invalid UsdZipFile object.
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...
size_t size
Size of this file as stored in the zip archive.
Definition: zipFile.h:95
USD_API const char * GetFile() const
Returns pointer to the beginning of the current file in the zip archive.
Class for reading a zip file.
Definition: zipFile.h:56
USD_API pointer operator->() const
Returns filename of the current file in the zip archive.
Iterator for traversing and inspecting the contents of the zip archive.
Definition: zipFile.h:112
USD_API Iterator begin() const
Returns iterator pointing to the first file in the zip archive.
Interface for accessing the contents of an asset.
Definition: asset.h:43
USD_API void DumpContents() const
Print out listing of contents of this zip archive to stdout.
Iterator cend() const
Returns end iterator for this zip archive.
Definition: zipFile.h:187
static USD_API UsdZipFile Open(const std::string &filePath)
Opens the zip archive at filePath.
USD_API reference 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 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...
Information for a file in the zip archive.
Definition: zipFile.h:85
USD_API UsdZipFileWriter()
Create an invalid UsdZipFileWriter object.
bool encrypted
Whether or not this file is encrypted.
Definition: zipFile.h:107
USD_API Iterator end() const
Returns end iterator for this zip archive.
Iterator cbegin() const
Returns iterator pointing to the first file in the zip archive.
Definition: zipFile.h:180
size_t uncompressedSize
Uncompressed size of this file.
Definition: zipFile.h:99
size_t dataOffset
Offset of the beginning of this file&#39;s data from the start of the zip archive.
Definition: zipFile.h:90
uint16_t compressionMethod
Compression method for this file.
Definition: zipFile.h:104
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.
Class for writing a zip file.
Definition: zipFile.h:213
USD_API void Discard()
Discards the zip archive so that it is not saved to the destination file path.
USD_API ~UsdZipFileWriter()
Calls Save()