Loading...
Searching...
No Matches
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_SDF_ZIP_FILE_H
8#define PXR_USD_SDF_ZIP_FILE_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
12
13#include <cstdint>
14#include <memory>
15#include <string>
16#include <utility>
17#include <vector>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21class ArAsset;
22
41{
42private:
43 class _Impl;
44
45public:
48 SDF_API
49 static SdfZipFile Open(const std::string& filePath);
50
53 SDF_API
54 static SdfZipFile Open(const std::shared_ptr<ArAsset>& asset);
55
57 SDF_API
59
60 SDF_API
62
64 SDF_API
65 explicit operator bool() const { return static_cast<bool>(_impl); }
66
70 {
71 public:
74 size_t dataOffset = 0;
75
79 size_t size = 0;
80
83 size_t uncompressedSize = 0;
84
86 size_t crc = 0;
87
91 uint16_t compressionMethod = 0;
92
94 bool encrypted = false;
95 };
96
99 class Iterator
100 {
101 // Proxy type for operator->(), needed since this iterator's value
102 // is generated on the fly.
103 class _ArrowProxy
104 {
105 public:
106 explicit _ArrowProxy(const std::string& s) : _s(s) { }
107 const std::string* operator->() const { return &_s; }
108 private:
109 std::string _s;
110 };
111
112 public:
113 SDF_API
114 Iterator();
115
116 SDF_API
117 ~Iterator();
118
119 SDF_API
120 Iterator(const Iterator& rhs);
121
122 SDF_API
123 Iterator(Iterator&& rhs);
124
125 SDF_API
126 Iterator& operator=(const Iterator& rhs);
127
128 SDF_API
129 Iterator& operator=(Iterator&& rhs);
130
131 using difference_type = std::ptrdiff_t;
132 using value_type = std::string;
133 using pointer = _ArrowProxy;
134 using reference = std::string;
135 using iterator_category = std::forward_iterator_tag;
136
137 SDF_API
138 Iterator& operator++();
139 SDF_API
140 Iterator operator++(int);
141
142 SDF_API
143 bool operator==(const Iterator& rhs) const;
144 SDF_API
145 bool operator!=(const Iterator& rhs) const;
146
148 SDF_API
149 reference operator*() const;
150
152 SDF_API
153 pointer operator->() const;
154
161 SDF_API
162 const char* GetFile() const;
163
166 SDF_API
168
169 private:
170 friend class SdfZipFile;
171 Iterator(const _Impl* impl, size_t offset = 0);
172
173 class _IteratorData;
174 std::unique_ptr<_IteratorData> _data;
175 };
176
178 SDF_API
180
182 Iterator cbegin() const { return begin(); }
183
185 SDF_API
186 Iterator end() const;
187
189 Iterator cend() const { return end(); }
190
193 SDF_API
194 Iterator Find(const std::string& path) const;
195
198 SDF_API
199 void DumpContents() const;
200
201private:
202 SdfZipFile(std::shared_ptr<_Impl>&& impl);
203
204 std::shared_ptr<_Impl> _impl;
205};
206
216{
217public:
223 SDF_API
224 static SdfZipFileWriter CreateNew(const std::string& filePath);
225
227 SDF_API
229
231 SDF_API
233
234 SdfZipFileWriter(const SdfZipFileWriter&) = delete;
235 SdfZipFileWriter& operator=(const SdfZipFileWriter&) = delete;
236
237 SDF_API
239 SDF_API
240 SdfZipFileWriter& operator=(SdfZipFileWriter&& rhs);
241
243 SDF_API
244 explicit operator bool() const { return static_cast<bool>(_impl); }
245
255 SDF_API
256 std::string AddFile(const std::string& filePath,
257 const std::string& filePathInArchive = std::string());
258
262 SDF_API
263 bool Save();
264
268 SDF_API
269 void Discard();
270
271private:
272 class _Impl;
273 SdfZipFileWriter(std::unique_ptr<_Impl>&& impl);
274
275 std::unique_ptr<_Impl> _impl;
276};
277
278PXR_NAMESPACE_CLOSE_SCOPE
279
280#endif // PXR_USD_SDF_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:100
SDF_API pointer operator->() const
Returns filename of the current file in the zip archive.
SDF_API const char * GetFile() const
Returns pointer to the beginning of the current file in the zip archive.
SDF_API reference operator*() const
Returns filename of the current file in the zip archive.
SDF_API FileInfo GetFileInfo() const
Returns FileInfo object containing information about the current file.
Class for reading a zip file.
Definition: zipFile.h:41
size_t uncompressedSize
Uncompressed size of this file.
Definition: zipFile.h:83
SDF_API Iterator begin() const
Returns iterator pointing to the first file in the zip archive.
SDF_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:189
uint16_t compressionMethod
Compression method for this file.
Definition: zipFile.h:91
Iterator cbegin() const
Returns iterator pointing to the first file in the zip archive.
Definition: zipFile.h:182
size_t size
Size of this file as stored in the zip archive.
Definition: zipFile.h:79
bool encrypted
Whether or not this file is encrypted.
Definition: zipFile.h:94
size_t dataOffset
Offset of the beginning of this file's data from the start of the zip archive.
Definition: zipFile.h:74
SDF_API Iterator end() const
Returns end iterator for this zip archive.
SDF_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...
static SDF_API SdfZipFile Open(const std::shared_ptr< ArAsset > &asset)
Opens the zip archive asset.
SDF_API SdfZipFile()
Create an invalid SdfZipFile object.
static SDF_API SdfZipFile Open(const std::string &filePath)
Opens the zip archive at filePath.
size_t crc
CRC-32 value of the uncompressed file.
Definition: zipFile.h:86
Information for a file in the zip archive.
Definition: zipFile.h:70
Class for writing a zip file.
Definition: zipFile.h:216
SDF_API void Discard()
Discards the zip archive so that it is not saved to the destination file path.
SDF_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.
static SDF_API SdfZipFileWriter CreateNew(const std::string &filePath)
Create a new file writer with filePath as the destination file path where the zip archive will be wri...
SDF_API ~SdfZipFileWriter()
Calls Save()
SDF_API SdfZipFileWriter()
Create an invalid SdfZipFileWriter object.
SDF_API bool Save()
Finalizes the zip archive and saves it to the destination file path.