Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/VFS/ZipFileReader.h
3187 views
1
#pragma once
2
3
#ifdef SHARED_LIBZIP
4
#include <zip.h>
5
#else
6
#include "ext/libzip/zip.h"
7
#endif
8
9
#include <mutex>
10
#include <set>
11
#include <string>
12
13
#include "Common/File/VFS/VFS.h"
14
#include "Common/File/FileUtil.h"
15
#include "Common/File/Path.h"
16
17
class ZipFileReader : public VFSBackend {
18
public:
19
static ZipFileReader *Create(const Path &zipFile, const char *inZipPath, bool logErrors = true);
20
~ZipFileReader();
21
22
bool IsValid() const { return zip_file_ != nullptr; }
23
24
// use delete[] on the returned value.
25
uint8_t *ReadFile(const char *path, size_t *size) override;
26
27
VFSFileReference *GetFile(const char *path) override;
28
bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) override;
29
void ReleaseFile(VFSFileReference *vfsReference) override;
30
31
VFSOpenFile *OpenFileForRead(VFSFileReference *vfsReference, size_t *size) override;
32
void Rewind(VFSOpenFile *vfsOpenFile) override;
33
size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) override;
34
void CloseFile(VFSOpenFile *vfsOpenFile) override;
35
36
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) override;
37
bool GetFileInfo(const char *path, File::FileInfo *info) override;
38
std::string toString() const override {
39
std::string retval = zipPath_.ToVisualString();
40
if (!inZipPath_.empty()) {
41
retval += ": ";
42
retval += inZipPath_;
43
}
44
return retval;
45
}
46
47
private:
48
ZipFileReader(zip *zip_file, const Path &zipPath, const std::string &inZipPath) : zip_file_(zip_file), zipPath_(zipPath), inZipPath_(inZipPath) {}
49
// Path has to be either an empty string, or a string ending with a /.
50
bool GetZipListings(const std::string &path, std::set<std::string> &files, std::set<std::string> &directories);
51
52
zip *zip_file_ = nullptr;
53
std::mutex lock_;
54
std::string inZipPath_;
55
Path zipPath_;
56
};
57
58
// When you just want a single file from a ZIP, and don't care about accurate error reporting, use this.
59
// The buffer should be free'd with free. Mutex will be locked while updating data, if non-null.
60
bool ReadSingleFileFromZip(Path zipFile, const char *path, std::string *data, std::mutex *mutex);
61
62