Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathzip_utils.cpp
More file actions
Latest commit
140 lines (131 loc) · 4.61 KB
/
Copy pathzip_utils.cpp
File metadata and controls
140 lines (131 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<filesystem>
#include<fstream>
#include<iostream>
#include<sstream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#include<miniz.h>
#include"zip_utils.hpp"
namespacefs= std::filesystem;
staticFILE *openRead(const fs::path &p){
#ifdef _WIN32
return_wfopen(p.c_str(), L"rb");
#else
returnfopen(p.c_str(), "rb");
#endif
}
boolisZipArchive(const std::string &path){
fs::path p(path);
if (!fs::is_regular_file(p)) returnfalse;
std::string ext = p.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
return ext == ".zip";
}
static std::string crc32Hex(const fs::path &p){
FILE *f = openRead(p);
if (!f) throwstd::runtime_error("Cannot open " + p.string());
std::vector<unsignedchar> buf(1 << 20);
mz_ulong crc = MZ_CRC32_INIT;
size_t n;
while ((n = fread(buf.data(), 1, buf.size(), f)) > 0){
crc = mz_crc32(crc, buf.data(), n);
}
fclose(f);
std::ostringstream ss;
ss << std::hex << std::setw(8) << std::setfill('0') << crc;
return ss.str();
}
staticboolsafeEntryPath(const std::string &name, fs::path &rel){
std::string s = name;
std::replace(s.begin(), s.end(), '\\', '/');
fs::path p = fs::path(s).lexically_normal();
if (p.empty() || p.is_absolute() || p.has_root_name()) returnfalse;
for (constauto &c : p){
if (c == "..") returnfalse;
}
rel = p;
returntrue;
}
staticsize_twriteCallback(void *opaque, mz_uint64 ofs, constvoid *buf, size_t n){
std::ofstream *out = static_cast<std::ofstream *>(opaque);
out->write(static_cast<constchar *>(buf), n);
return out->good() ? n : 0;
}
staticvoidextractAll(const fs::path &zipPath, const fs::path &destDir){
FILE *f = openRead(zipPath);
if (!f) throwstd::runtime_error("Cannot open " + zipPath.string());
mz_zip_archive zip;
std::memset(&zip, 0, sizeof(zip));
if (!mz_zip_reader_init_cfile(&zip, f, fs::file_size(zipPath), 0)){
fclose(f);
throwstd::runtime_error("Invalid zip archive: " + zipPath.string());
}
try{
mz_uint count = mz_zip_reader_get_num_files(&zip);
for (mz_uint i = 0; i < count; i++){
mz_zip_archive_file_stat st;
if (!mz_zip_reader_file_stat(&zip, i, &st)){
throwstd::runtime_error("Cannot read zip entry in " + zipPath.string());
}
std::string name = st.m_filename;
if (name.empty()) continue;
fs::path rel;
if (!safeEntryPath(name, rel)) continue;
fs::path out = destDir / rel;
if (mz_zip_reader_is_file_a_directory(&zip, i) || name.back() == '/' || name.back() == '\\'){
fs::create_directories(out);
continue;
}
if (out.has_parent_path()) fs::create_directories(out.parent_path());
std::ofstream os(out, std::ios::binary);
if (!os || !mz_zip_reader_extract_to_callback(&zip, i, writeCallback, &os, 0)){
throwstd::runtime_error("Failed to extract " + rel.string());
}
}
}catch(...){
mz_zip_reader_end(&zip);
fclose(f);
throw;
}
mz_zip_reader_end(&zip);
fclose(f);
}
static fs::path descendSingleDir(fs::path dir){
for (int depth = 0; depth < 4; depth++){
fs::path only;
int count = 0;
for (constauto &e : fs::directory_iterator(dir)){
std::string name = e.path().filename().string();
if (name == "__MACOSX" || name == ".DS_Store") continue;
only = e.path();
if (++count > 1) break;
}
if (count == 1 && fs::is_directory(only)) dir = only;
elsebreak;
}
return dir;
}
std::string extractZipToCache(const std::string &zipPath){
fs::path zp = fs::absolute(zipPath);
fs::path dest = fs::temp_directory_path() / ("opensplat-" + crc32Hex(zp));
if (!fs::exists(dest)){
fs::path staging = dest;
staging += ".partial";
if (fs::exists(staging)) fs::remove_all(staging);
std::cout << "Extracting " << zp.string() << " to " << dest.string() << std::endl;
fs::create_directories(staging);
extractAll(zp, staging);
std::error_code ec;
fs::rename(staging, dest, ec);
if (ec){
if (fs::exists(dest)) fs::remove_all(staging);
elsethrowstd::runtime_error("Cannot finalize extraction to " + dest.string());
}
}else{
std::cout << "Using cached extraction: " << dest.string() << std::endl;
}
returndescendSingleDir(dest).string();
}