-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZipTools.cpp
372 lines (305 loc) · 8.75 KB
/
ZipTools.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "stdafx.h"
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#define ZLIB_WINAPI
#define WRITEBUFFERSIZE (16384)
#include "ZipTools.h"
#include "DebugTools.h"
#include "FileTools.h"
#include "ioapi.h"
#include "zip.h"
#include "unzip.h"
#include "iowin32.h"
#pragma comment(lib, "zlibstat.lib")
// ////////////////////////////////////////////////////////////////////////////////
// @global 得到相对文件名
//
void GetRelativeFileName(LPCWSTR wzFullName, LPCWSTR wzParentPath, LPWSTR wzRelativeName)
{
LPCWSTR wzMid = wzFullName + wcslen(wzParentPath);
wcscpy_s(wzRelativeName, MAX_PATH, wzMid);
}
// ////////////////////////////////////////////////////////////////////////////////
// @global 将文件写入至 zip
//
void WriteFileToZip(WIN32_FIND_DATAW ffd, zipFile& zf, LPCWSTR wzFullPath, LPCWSTR wzParentPath, PBYTE buf)
{
FILE * fin;
zip_fileinfo zi = {0};
FILETIME ftLocal;
FileTimeToLocalFileTime(&(ffd.ftLastWriteTime),&ftLocal);
FileTimeToDosDateTime(&ftLocal,((LPWORD)&zi.dosDate)+1,((LPWORD)&zi.dosDate)+0);
WCHAR wzRelativeName[MAX_PATH] = {0};
GetRelativeFileName(wzFullPath, wzParentPath, wzRelativeName);
CHAR szRelativeName[1024] = {0};
WideCharToMultiByte(CP_ACP, 0, wzRelativeName, -1, szRelativeName, 1024, NULL, NULL);
int err = zipOpenNewFileInZip3_64(
zf,
szRelativeName,
&zi,
NULL,0,NULL,0,NULL,
Z_DEFLATED,
9,0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, 0);
if ( ZIP_OK != err )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [AddFileToZip] Error In Opening File [%s] \r\n", wzRelativeName);
}
fin = _wfopen(wzRelativeName, L"rb");
if ( NULL == fin )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [AddFileToZip] Error In Opening %s For Reading.\r\n", wzRelativeName);
}
unsigned long size_read = 0;
unsigned long size_buf = WRITEBUFFERSIZE;
do
{
err = ZIP_OK;
size_read = (int)fread(buf,1,size_buf,fin);
if ( size_read > 0 )
{
err = zipWriteInFileInZip (zf,buf,size_read);
if ( err < 0 )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [AddFileToZip] Error In Writing %s In the Zipfile.\r\n",
wzRelativeName);
}
}
} while ((err == ZIP_OK) && (size_read>0));
fclose(fin);
zipCloseFileInZip(zf);
}
// ////////////////////////////////////////////////////////////////////////////////
// @global 将文件夹内文件添加进 zip ( 递归 )
//
void AddFileToZip(zipFile& zf, LPCWSTR wzPath, LPCWSTR wzParentPath, PBYTE buf)
{
WIN32_FIND_DATAW ffd = {0};
WCHAR wzFindStr[MAX_PATH] = {0};
wsprintf(wzFindStr, L"%s\\*", wzPath);
HANDLE hFind = FindFirstFileW(wzFindStr, &ffd);
do
{
if ( 0 == wcscmp(ffd.cFileName, L".")
|| 0 == wcscmp(ffd.cFileName, L"..") )
{
continue;
}
if ( ffd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
WCHAR wzSubDir[MAX_PATH] = {0};
wsprintf(wzSubDir, L"%s\\%s", wzPath, ffd.cFileName);
AddFileToZip(zf, wzSubDir, wzParentPath, buf);
continue;
}
WCHAR wzFullPath[MAX_PATH] = {0};
wsprintf(wzFullPath, L"%s\\%s", wzPath, ffd.cFileName);
WriteFileToZip(ffd, zf, wzFullPath, wzParentPath, buf);
DebugTools::OutputDebugPrintfW(L"[ZipTools] [AddFileToZip] Add File Success. [%s]\r\n", wzFullPath);
} while ( 0 != FindNextFileW(hFind, &ffd));
}
// ////////////////////////////////////////////////////////////////////////////////
// @global 修改文件时间
//
void ChangeFileTime(const char *filename, unsigned long dosdate, tm_unz tmu_date)
{
HANDLE hFile;
FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);
GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
LocalFileTimeToFileTime(&ftLocal,&ftm);
SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
CloseHandle(hFile);
}
// ////////////////////////////////////////////////////////////////////////////////
// @global 释放单个文件
//
int ExtractSingleFile(unzFile& uf)
{
char filename_inzip[256] = {0};
unz_file_info64 file_info;
int err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
if ( UNZ_OK != err )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [ExtractSingleFile] unzGetCurrentFileInfo64 Failed.[%d]\r\n", err);
return err;
}
unsigned int size_buf = WRITEBUFFERSIZE;
PBYTE buf = new BYTE[size_buf];
// 判断名字是否以 '\\' 结尾,如果以 '\\'
// 结尾,代表这是一个目录名,创建目录,
// 反之则创建文件
if ( '\\' == filename_inzip[strlen(filename_inzip)-1] )
{
WCHAR wzDirName[1024] = {0};
MultiByteToWideChar(CP_ACP, 0, filename_inzip, -1, wzDirName, 1024);
FileTools::CreateDirectorys(wzDirName);
}
else
{
err = unzOpenCurrentFilePassword(uf, NULL);
if ( err != UNZ_OK)
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [ExtractSingleFile] unzOpenCurrentFilePassword Failed.[%d]\r\n", err);
}
// 创建文件所在的目录
WCHAR wzFileName[MAX_PATH] = {0};
MultiByteToWideChar(CP_ACP, 0, filename_inzip, -1, wzFileName, MAX_PATH);
WCHAR wzDirName[MAX_PATH] = {0};
FileTools::GetFileDir(wzFileName, wzDirName, L'\\');
FileTools::CreateDirectorys(wzDirName);
DebugTools::OutputDebugPrintfW(
L"[FileTools] [UnZip] Extracting %s ...\r\n", wzFileName);
FILE* fout = fopen64(filename_inzip, "wb");
do
{
err = unzReadCurrentFile(uf,buf,size_buf);
if ( err < 0 )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [ExtractSingleFile] unzReadCurrentFile Failed.[%d]\r\n", err);
break;
}
if ( err > 0 )
{
if ( fwrite(buf,err,1,fout) != 1 )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [ExtractSingleFile] Write File Failed.[%s]\r\n",
filename_inzip);
err = UNZ_ERRNO;
break;
}
}
} while ( err > 0 );
if ( fout )
{
fclose(fout);
}
// 修改文件时间
ChangeFileTime(filename_inzip, file_info.dosDate, file_info.tmu_date);
unzCloseCurrentFile(uf);
delete [] buf;
}
}
// ////////////////////////////////////////////////////////////////////////////////
// @global 释放文件
//
void ExtractFile(unzFile& uf)
{
unz_global_info64 gi;
int err = unzGetGlobalInfo64(uf, &gi);
if ( UNZ_OK != err )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [ExtractFile] unzGetGlobalInfo64 Failed.\r\n");
}
for (int i = 0 ; i < gi.number_entry; i++)
{
if ( ExtractSingleFile(uf) != UNZ_OK )
{
break;
}
if ( i + 1 < gi.number_entry )
{
err = unzGoToNextFile(uf);
if ( err != UNZ_OK )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [ExtractFile] unzGoToNextFile Failed.\r\n");
break;
}
}
}
}
// ////////////////////////////////////////////////////////////////////////////////
// @public @static 将指定文件名打包成指定 zip
//
bool ZipTools::Zip(LPCWSTR wzDirPath, LPCWSTR wzDestName)
{
//
// 检查指定路径是否存在
//
if ( !FileTools::Exist(wzDirPath) )
{
DebugTools::OutputDebugPrintfW(L"[ZipTools] [Zip] Path Not Exist. [%s]\r\n", wzDirPath);
return false;
}
WCHAR wzDestDir[MAX_PATH] = {0};
FileTools::GetFileDir(wzDestName, wzDestDir, L'\\');
if ( !FileTools::Exist(wzDestDir) )
{
DebugTools::OutputDebugPrintfW(L"[ZipTools] [Zip] Path Not Exist. [%s]\r\n", wzDestDir);
return false;
}
// 设置当前工作目录
SetCurrentDirectoryW(wzDirPath);
WCHAR wzParentDir[MAX_PATH] = {0};
wcscpy_s(wzParentDir, wzDirPath);
//FileTools::GetFileDir(wzDirPath, wzParentDir, L'\\');
wcscat_s(wzParentDir, L"\\");
//
// 创建 zip 文件
//
zlib_filefunc64_def ffunc;
fill_win32_filefunc64W(&ffunc);
zipFile zf; zf = zipOpen2_64(wzDestName, 0, NULL, &ffunc);
if ( NULL == zf )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [Zip] Create Zip File Failed. [%s] \r\n", wzDestName);
return false;
}
unsigned long size_read = 0;
unsigned long size_buf = WRITEBUFFERSIZE;
PBYTE buf = new BYTE [size_buf];
//
// 将文件夹下所有文件添加进 zip
//
AddFileToZip(zf, wzDirPath, wzParentDir, buf);
zipClose(zf,NULL);
delete [] buf;
return true;
}
// ////////////////////////////////////////////////////////////////////////////////
// @public @static 将指定 zip 解压至指定路径下
//
bool ZipTools::UnZip(LPCWSTR wzZipName, LPCWSTR wzDestPath)
{
//
// 检查指定文件和文件夹是否存在
//
if ( !FileTools::Exist(wzZipName) )
{
DebugTools::OutputDebugPrintfW(L"[ZipTools] [UnZip] File Not Exist. [%s]\r\n", wzZipName);
return false;
}
if ( !FileTools::Exist(wzDestPath) )
{
DebugTools::OutputDebugPrintfW(L"[ZipTools] [Zip] Path Not Exist. [%s]\r\n", wzDestPath);
return false;
}
// 打开 zip 文件
zlib_filefunc64_def ffunc;
fill_win32_filefunc64W(&ffunc);
unzFile uf = unzOpen2_64(wzZipName, &ffunc);
if ( NULL == uf )
{
DebugTools::OutputDebugPrintfW(
L"[ZipTools] [Zip] Open Zip File Failed.[%s]\r\n", wzZipName);
}
// 设置指定目录为工作目录
SetCurrentDirectoryW(wzDestPath);
// 释放文件
ExtractFile(uf);
unzClose(uf);
return true;
}