-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuzip.cpp
74 lines (63 loc) · 2.36 KB
/
uzip.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
#define STRICT
#include <string>
#include <windows.h>
#include "minizip/unzip.h"
#include "minizip/ioapi.h"
#include "zconf.h"
using namespace std;
#define for if (0) ; else for
#define IsShiftJIS(x) ((BYTE)((x ^ 0x20) - 0xA1) <= 0x3B)
//---------------------------------------------------------------------------
// ファイルが存在するかどうか
bool IsFileExist(const string &strFilename) {
return GetFileAttributesA(strFilename.c_str()) != 0xffffffff;
}
//---------------------------------------------------------------------------
// 再帰的にディレクトリを作成(strPathの末尾には必ず\をつけること)
bool CreateDirectoryReflex(const string &strPath) {
const char *p = strPath.c_str();
for (; *p; p += IsShiftJIS(*p) ? 2 : 1)
if (*p == '\\') {
string strSubPath(strPath.c_str(), p);
if (!IsFileExist(strSubPath.c_str()))
if (!CreateDirectoryA(strSubPath.c_str(), NULL))
return false;
}
return true;
}
//---------------------------------------------------------------------------
// ZIPファイルを解凍する
bool Unzip(const string &strZipFilename, const string &strTargetPath) {
unzFile hUnzip = unzOpen(strZipFilename.c_str());
if (!hUnzip)
return false;
do {
char szConFilename[512];
unz_file_info fileInfo;
if (unzGetCurrentFileInfo(hUnzip, &fileInfo, szConFilename, sizeof szConFilename, NULL, 0, NULL, 0) != UNZ_OK)
break;
int nLen = strlen(szConFilename);
for (int i = 0; i < nLen; ++i)
if (szConFilename[i] == '/')
szConFilename[i] = '\\';
// ディレクトリの場合
if (nLen >= 2 && !IsShiftJIS(szConFilename[nLen - 2]) && szConFilename[nLen - 1] == '\\') {
CreateDirectoryReflex(strTargetPath + szConFilename);
continue;
}
// ファイルの場合
if (unzOpenCurrentFile(hUnzip) != UNZ_OK)
break;
CreateDirectoryReflex(strTargetPath + szConFilename);
HANDLE hFile = CreateFileA((strTargetPath + szConFilename).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
BYTE szBuffer[8192];
DWORD dwSizeRead;
while ((dwSizeRead = unzReadCurrentFile(hUnzip, szBuffer, sizeof szBuffer)) > 0)
WriteFile(hFile, szBuffer, dwSizeRead, &dwSizeRead, NULL);
FlushFileBuffers(hFile);
CloseHandle(hFile);
unzCloseCurrentFile(hUnzip);
} while (unzGoToNextFile(hUnzip) != UNZ_END_OF_LIST_OF_FILE);
unzClose(hUnzip);
return true;
}