All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
zipFile.h
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_USD_ZIP_FILE_H
8#define PXR_USD_USD_ZIP_FILE_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/usd/api.h"
12
13#include <memory>
14#include <string>
15#include <utility>
16#include <vector>
17
18PXR_NAMESPACE_OPEN_SCOPE
19
20class ArAsset;
21
40{
41private:
42 class _Impl;
43
44public:
47 USD_API
48 static UsdZipFile Open(const std::string& filePath);
49
52 USD_API
53 static UsdZipFile Open(const std::shared_ptr<ArAsset>& asset);
54
56 USD_API
58
59 USD_API
61
63 USD_API
64 explicit operator bool() const { return static_cast<bool>(_impl); }
65
69 {
70 public:
73 size_t dataOffset = 0;
74
78 size_t size = 0;
79
82 size_t uncompressedSize = 0;
83
85 size_t crc = 0;
86
90 uint16_t compressionMethod = 0;
91
93 bool encrypted = false;
94 };
95
98 class Iterator
99 {
100 // Proxy type for operator->(), needed since this iterator's value
101 // is generated on the fly.
102 class _ArrowProxy
103 {
104 public:
105 explicit _ArrowProxy(const std::string& s) : _s(s) { }
106 const std::string* operator->() const { return &_s; }
107 private:
108 std::string _s;
109 };
110
111 public:
112 USD_API
113 Iterator();
114
115 USD_API
116 ~Iterator();
117
118 USD_API
119 Iterator(const Iterator& rhs);
120
121 USD_API
122 Iterator(Iterator&& rhs);
123
124 USD_API
125 Iterator& operator=(const Iterator& rhs);
126
127 USD_API
128 Iterator& operator=(Iterator&& rhs);
129
130 using difference_type = std::ptrdiff_t;
131 using value_type = std::string;
132 using pointer = _ArrowProxy;
133 using reference = std::string;
134 using iterator_category = std::forward_iterator_tag;
135
136 USD_API
137 Iterator& operator++();
138 USD_API
139 Iterator operator++(int);
140
141 USD_API
142 bool operator==(const Iterator& rhs) const;
143 USD_API
144 bool operator!=(const Iterator& rhs) const;
145
147 USD_API
148 reference operator*() const;
149
151 USD_API
152 pointer operator->() const;
153
160 USD_API
161 const char* GetFile() const;
162
165 USD_API
167
168 private:
169 friend class UsdZipFile;
170 Iterator(const _Impl* impl, size_t offset = 0);
171
172 class _IteratorData;
173 std::unique_ptr<_IteratorData> _data;
174 };
175
177 USD_API
179
181 Iterator cbegin() const { return begin(); }
182
184 USD_API
185 Iterator end() const;
186
188 Iterator cend() const { return end(); }
189
192 USD_API
193 Iterator Find(const std::string& path) const;
194
197 USD_API
198 void DumpContents() const;
199
200private:
201 UsdZipFile(std::shared_ptr<_Impl>&& impl);
202
203 std::shared_ptr<_Impl> _impl;
204};
205
215{
216public:
222 USD_API
223 static UsdZipFileWriter CreateNew(const std::string& filePath);
224
226 USD_API
228
230 USD_API
232
233 UsdZipFileWriter(const UsdZipFileWriter&) = delete;
234 UsdZipFileWriter& operator=(const UsdZipFileWriter&) = delete;
235
236 USD_API
238 USD_API
239 UsdZipFileWriter& operator=(UsdZipFileWriter&& rhs);
240
242 USD_API
243 explicit operator bool() const { return static_cast<bool>(_impl); }
244
254 USD_API
255 std::string AddFile(const std::string& filePath,
256 const std::string& filePathInArchive = std::string());
257
261 USD_API
262 bool Save();
263
267 USD_API
268 void Discard();
269
270private:
271 class _Impl;
272 UsdZipFileWriter(std::unique_ptr<_Impl>&& impl);
273
274 std::unique_ptr<_Impl> _impl;
275};
276
277PXR_NAMESPACE_CLOSE_SCOPE
278
279#endif // PXR_USD_USD_ZIP_FILE_H
Interface for accessing the contents of an asset.
Definition: asset.h:27
Iterator for traversing and inspecting the contents of the zip archive.
Definition: zipFile.h:99
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:40
size_t uncompressedSize
Uncompressed size of this file.
Definition: zipFile.h:82
Iterator cend() const
Returns end iterator for this zip archive.
Definition: zipFile.h:188
uint16_t compressionMethod
Compression method for this file.
Definition: zipFile.h:90
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:181
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:78
bool encrypted
Whether or not this file is encrypted.
Definition: zipFile.h:93
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:73
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:85
Information for a file in the zip archive.
Definition: zipFile.h:69
Class for writing a zip file.
Definition: zipFile.h:215
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...