-
Notifications
You must be signed in to change notification settings - Fork 12
/
ZipUtil.cpp
281 lines (232 loc) · 6.06 KB
/
ZipUtil.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2016 Zarklord1 : https://github.com/Zarklord1/WiiU_Save_Manager
#include <string.h>
#include <fcntl.h>
#include <string>
#include <dirent.h>
#include <stdio.h>
#include <cstdint>
#include <unistd.h>
#include "ZipUtil.hpp"
#include "Utils.hpp"
#define u32 uint32_t
#define u8 uint8_t
using namespace std;
Zip::Zip(const char * zipPath) {
fileToZip = zipOpen(zipPath,APPEND_STATUS_CREATE);
if(fileToZip == NULL) printf("Error Opening: %s for zipping files!\n",zipPath);
}
Zip::~Zip() {
Close();
}
int Zip::AddFile(const char * internalPath, const char * path) {
zipOpenNewFileInZip(fileToZip, internalPath, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
printf("Adding %s to zip, under path %s\n",path,internalPath);
int code = Add(path);
zipCloseFileInZip(fileToZip);
return code;
}
int Zip::AddDir(const char * internalDir, const char * externalDir) {
struct dirent *dirent = NULL;
DIR *dir = NULL;
dir = opendir(externalDir);
if (dir == NULL) {
return -1;
}
while ((dirent = readdir(dir)) != 0) {
if(strcmp(dirent->d_name, "..") == 0 || strcmp(dirent->d_name, ".") == 0)
continue;
std::string zipPath(internalDir);
zipPath += '/';
zipPath += dirent->d_name;
std::string realPath(externalDir);
realPath += '/';
realPath += dirent->d_name;
if(dirent->d_type & DT_DIR) {
AddDir(zipPath.c_str(), realPath.c_str());
} else {
AddFile(zipPath.c_str(),realPath.c_str());
}
}
closedir(dir);
return 0;
}
int Zip::Add(const char * path) {
int fileNumber = open(path, O_RDONLY);
if(fileNumber == -1)
return -1;
u32 filesize = lseek(fileNumber, 0, SEEK_END);
lseek(fileNumber, 0, SEEK_SET);
u32 blocksize = 0x8000;
u8 * buffer = (u8*)malloc(blocksize);
if (buffer == NULL)
return -2;
u32 done = 0;
int readBytes = 0;
while(done < filesize)
{
if(done + blocksize > filesize) {
blocksize = filesize - done;
}
readBytes = read(fileNumber, buffer, blocksize);
if(readBytes <= 0)
break;
zipWriteInFileInZip(fileToZip,buffer,blocksize);
done += readBytes;
}
close(fileNumber);
free(buffer);
if (done != filesize)
{
return -3;
}
return 0;
}
void Zip::Close() {
zipClose(fileToZip,NULL);
}
UnZip::UnZip(const char * zipPath) {
fileToUnzip = unzOpen(zipPath);
}
UnZip::~UnZip() {
Close();
}
void UnZip::Close() {
unzClose(fileToUnzip);
}
int UnZip::ExtractFile(const char * internalPath,const char * path) {
int code = unzLocateFile(fileToUnzip,internalPath,0);
if(code == UNZ_END_OF_LIST_OF_FILE)
return -1;
unz_file_info_s * fileInfo = GetFileInfo();
std::string fullPath(path);
printf("Extracting file %s to: %s\n",internalPath,fullPath.c_str());
code = Extract(fullPath.c_str(),fileInfo);
free(fileInfo);
return code;
}
int UnZip::ExtractDir(const char * internalDir,const char * externalDir) {
int i = 0;
for(;;) {
int code;
if(i == 0) {
code = unzGoToFirstFile(fileToUnzip);
i++;
} else {
code = unzGoToNextFile(fileToUnzip);
}
if(code == UNZ_END_OF_LIST_OF_FILE) {
if(i > 1) {
return 0;
} else {
return -1;
}
}
unz_file_info_s * fileInfo = GetFileInfo();
std::string outputPath = GetFullFileName(fileInfo);
if(outputPath.find(internalDir,0) != 0) {
free(fileInfo);
continue;
}
outputPath.replace(0,strlen(internalDir),externalDir);
if(fileInfo->uncompressed_size != 0 && fileInfo->compression_method != 0) {
//file
i++;
printf("Extracting %s to: %s\n",GetFullFileName(fileInfo).c_str(),outputPath.c_str());
Extract(outputPath.c_str(),fileInfo);
}
free(fileInfo);
}
}
int UnZip::ExtractAll(const char * dirToExtract) {
int i = 0;
for(;;) {
int code;
if(i == 0) {
code = unzGoToFirstFile(fileToUnzip);
i++;
} else {
code = unzGoToNextFile(fileToUnzip);
}
if(code == UNZ_END_OF_LIST_OF_FILE) return 0;
unz_file_info_s * fileInfo = GetFileInfo();
std::string fileName(dirToExtract);
fileName += '/';
fileName += GetFullFileName(fileInfo);
if(fileInfo->uncompressed_size != 0 && fileInfo->compression_method != 0) {
//file
printf("Extracting %s to: %s\n",GetFullFileName(fileInfo).c_str(),fileName.c_str());
Extract(fileName.c_str(),fileInfo);
}
free(fileInfo);
}
}
int UnZip::Extract(const char * path, unz_file_info_s * fileInfo) {
//check to make sure filepath or fileInfo isnt null
if(path == NULL || fileInfo == NULL)
return -1;
if(unzOpenCurrentFile(fileToUnzip) != UNZ_OK)
return -2;
char folderPath[strlen(path)+1];
strcpy(folderPath,path);
char * pos = strrchr(folderPath,'/');
if(pos != NULL) {
*pos = '\0';
CreateSubfolder(folderPath);
}
u32 blocksize = 0x8000;
u8 * buffer = (u8*)malloc(blocksize);
if(buffer == NULL)
return -3;
u32 done = 0;
int writeBytes = 0;
FILE* fp = fopen(path, "w");
if(fp == NULL) {
free(buffer);
return -4;
}
while(done < fileInfo->uncompressed_size)
{
if(done + blocksize > fileInfo->uncompressed_size) {
blocksize = fileInfo->uncompressed_size - done;
}
unzReadCurrentFile(fileToUnzip, buffer, blocksize);
writeBytes = write(fileno(fp), buffer, blocksize);
if(writeBytes <= 0) {
break;
}
done += writeBytes;
}
fflush(fp);
fclose(fp);
free(buffer);
if (done != fileInfo->uncompressed_size)
return -4;
unzCloseCurrentFile(fileToUnzip);
return 0;
}
std::string UnZip::GetFileName(unz_file_info_s * fileInfo) {
char fileName[fileInfo->size_filename+1];
std::string path;
strcpy(fileName,GetFullFileName(fileInfo).c_str());
char * pos = strrchr(fileName, '/');
if (pos != NULL) {
pos++;
path = pos;
} else {
path = fileName;
}
return path;
}
std::string UnZip::GetFullFileName(unz_file_info_s * fileInfo) {
char filePath[fileInfo->size_filename+1];
unzGetCurrentFileInfo(fileToUnzip,fileInfo,filePath,fileInfo->size_filename,NULL,0,NULL,0);
filePath[fileInfo->size_filename] = '\0';
std::string path(filePath);
path.resize(fileInfo->size_filename);
return path;
}
unz_file_info_s * UnZip::GetFileInfo() {
unz_file_info_s * fileInfo = (unz_file_info_s*)malloc(sizeof(unz_file_info_s));
unzGetCurrentFileInfo(fileToUnzip,fileInfo,NULL,0,NULL,0,NULL,0);
return fileInfo;
}