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 
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  // Proxy type for operator->(), needed since this iterator's value
115  // is generated on the fly.
116  class _ArrowProxy
117  {
118  public:
119  explicit _ArrowProxy(const std::string& s) : _s(s) { }
120  const std::string* operator->() const { return &_s; }
121  private:
122  std::string _s;
123  };
124 
125  public:
126  USD_API
127  Iterator();
128 
129  USD_API
130  ~Iterator();
131 
132  USD_API
133  Iterator(const Iterator& rhs);
134 
135  USD_API
136  Iterator(Iterator&& rhs);
137 
138  USD_API
139  Iterator& operator=(const Iterator& rhs);
140 
141  USD_API
142  Iterator& operator=(Iterator&& rhs);
143 
144  using difference_type = std::ptrdiff_t;
145  using value_type = std::string;
146  using pointer = _ArrowProxy;
147  using reference = std::string;
148  using iterator_category = std::forward_iterator_tag;
149 
150  USD_API
151  Iterator& operator++();
152  USD_API
153  Iterator operator++(int);
154 
155  USD_API
156  bool operator==(const Iterator& rhs) const;
157  USD_API
158  bool operator!=(const Iterator& rhs) const;
159 
161  USD_API
162  reference operator*() const;
163 
165  USD_API
166  pointer operator->() const;
167 
174  USD_API
175  const char* GetFile() const;
176 
179  USD_API
180  FileInfo GetFileInfo() const;
181 
182  private:
183  friend class UsdZipFile;
184  Iterator(const _Impl* impl, size_t offset = 0);
185 
186  class _IteratorData;
187  std::unique_ptr<_IteratorData> _data;
188  };
189 
191  USD_API
192  Iterator begin() const;
193 
195  Iterator cbegin() const { return begin(); }
196 
198  USD_API
199  Iterator end() const;
200 
202  Iterator cend() const { return end(); }
203 
206  USD_API
207  Iterator Find(const std::string& path) const;
208 
211  USD_API
212  void DumpContents() const;
213 
214 private:
215  UsdZipFile(std::shared_ptr<_Impl>&& impl);
216 
217  std::shared_ptr<_Impl> _impl;
218 };
219 
229 {
230 public:
236  USD_API
237  static UsdZipFileWriter CreateNew(const std::string& filePath);
238 
240  USD_API
242 
244  USD_API
246 
247  UsdZipFileWriter(const UsdZipFileWriter&) = delete;
248  UsdZipFileWriter& operator=(const UsdZipFileWriter&) = delete;
249 
250  USD_API
252  USD_API
253  UsdZipFileWriter& operator=(UsdZipFileWriter&& rhs);
254 
256  USD_API
257  explicit operator bool() const { return static_cast<bool>(_impl); }
258 
268  USD_API
269  std::string AddFile(const std::string& filePath,
270  const std::string& filePathInArchive = std::string());
271 
275  USD_API
276  bool Save();
277 
281  USD_API
282  void Discard();
283 
284 private:
285  class _Impl;
286  UsdZipFileWriter(std::unique_ptr<_Impl>&& impl);
287 
288  std::unique_ptr<_Impl> _impl;
289 };
290 
291 PXR_NAMESPACE_CLOSE_SCOPE
292 
293 #endif // PXR_USD_USD_ZIP_FILE_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
Class for reading a zip file.
Definition: zipFile.h:56
Iterator for traversing and inspecting the contents of the zip archive.
Definition: zipFile.h:112
Interface for accessing the contents of an asset.
Definition: asset.h:44
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.
Iterator cend() const
Returns end iterator for this zip archive.
Definition: zipFile.h:202
USD_API Iterator end() const
Returns end iterator for this zip archive.
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 void DumpContents() const
Print out listing of contents of this zip archive to stdout.
Information for a file in the zip archive.
Definition: zipFile.h:85
Iterator cbegin() const
Returns iterator pointing to the first file in the zip archive.
Definition: zipFile.h:195
USD_API UsdZipFileWriter()
Create an invalid UsdZipFileWriter object.
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...
bool encrypted
Whether or not this file is encrypted.
Definition: zipFile.h:107
USD_API Iterator begin() const
Returns iterator pointing to the first file in the zip archive.
size_t uncompressedSize
Uncompressed size of this file.
Definition: zipFile.h:99
size_t dataOffset
Offset of the beginning of this file'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 pointer operator->() const
Returns filename of the current file in the zip archive.
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:228
USD_API void Discard()
Discards the zip archive so that it is not saved to the destination file path.
USD_API ~UsdZipFileWriter()
Calls Save()