ZLibWrapLib.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //------------------------------------------------------------------------------
  2. //
  3. // Copyright (C) Streamlet. All rights reserved.
  4. //
  5. // File Name: ZLibWrapLib.cpp
  6. // Author: Streamlet
  7. // Create Time: 2010-09-16
  8. // Description:
  9. //
  10. // Version history:
  11. //
  12. //
  13. //
  14. //------------------------------------------------------------------------------
  15. #include "stdafx.h"
  16. #include "ZLibWrapLib.h"
  17. #include "Encoding.h"
  18. #include "Loki/ScopeGuard.h"
  19. #include "ZLib/zip.h"
  20. #include "ZLib/unzip.h"
  21. #include <atlstr.h>
  22. #define ZIP_GPBF_LANGUAGE_ENCODING_FLAG 0x800
  23. BOOL ZipAddFile(zipFile zf, LPCTSTR lpszFileNameInZip, LPCTSTR lpszFilePath, bool bUtf8 = false)
  24. {
  25. DWORD dwFileAttr = GetFileAttributes(lpszFilePath);
  26. if (dwFileAttr == INVALID_FILE_ATTRIBUTES)
  27. {
  28. return false;
  29. }
  30. DWORD dwOpenAttr = (dwFileAttr & FILE_ATTRIBUTE_DIRECTORY) != 0 ? FILE_FLAG_BACKUP_SEMANTICS : 0;
  31. HANDLE hFile = CreateFile(lpszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, dwOpenAttr, NULL);
  32. if (hFile == INVALID_HANDLE_VALUE)
  33. {
  34. return FALSE;
  35. }
  36. LOKI_ON_BLOCK_EXIT(CloseHandle, hFile);
  37. FILETIME ftUTC, ftLocal;
  38. GetFileTime(hFile, NULL, NULL, &ftUTC);
  39. FileTimeToLocalFileTime(&ftUTC, &ftLocal);
  40. WORD wDate, wTime;
  41. FileTimeToDosDateTime(&ftLocal, &wDate, &wTime);
  42. zip_fileinfo FileInfo;
  43. ZeroMemory(&FileInfo, sizeof(FileInfo));
  44. FileInfo.dosDate = ((((DWORD)wDate) << 16) | (DWORD)wTime);
  45. FileInfo.external_fa |= dwFileAttr;
  46. if (bUtf8)
  47. {
  48. CStringA strFileNameInZipA = UCS2ToANSI(lpszFileNameInZip, CP_UTF8);
  49. if (zipOpenNewFileInZip4(zf, strFileNameInZipA, &FileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9,
  50. 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, NULL, 0, 0, ZIP_GPBF_LANGUAGE_ENCODING_FLAG) != ZIP_OK)
  51. {
  52. return FALSE;
  53. }
  54. }
  55. else
  56. {
  57. CStringA strFileNameInZipA = UCS2ToANSI(lpszFileNameInZip);
  58. if (zipOpenNewFileInZip(zf, strFileNameInZipA, &FileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9) != ZIP_OK)
  59. {
  60. return FALSE;
  61. }
  62. }
  63. LOKI_ON_BLOCK_EXIT(zipCloseFileInZip, zf);
  64. if ((dwFileAttr & FILE_ATTRIBUTE_DIRECTORY) != 0)
  65. {
  66. return TRUE;
  67. }
  68. const DWORD BUFFER_SIZE = 4096;
  69. BYTE byBuffer[BUFFER_SIZE];
  70. LARGE_INTEGER li = {};
  71. if (!GetFileSizeEx(hFile, &li))
  72. {
  73. return FALSE;
  74. }
  75. while (li.QuadPart > 0)
  76. {
  77. DWORD dwSizeToRead = li.QuadPart > (LONGLONG)BUFFER_SIZE ? BUFFER_SIZE : (DWORD)li.LowPart;
  78. DWORD dwRead = 0;
  79. if (!ReadFile(hFile, byBuffer, dwSizeToRead, &dwRead, NULL))
  80. {
  81. return FALSE;
  82. }
  83. if (zipWriteInFileInZip(zf, byBuffer, dwRead) < 0)
  84. {
  85. return FALSE;
  86. }
  87. li.QuadPart -= (LONGLONG)dwRead;
  88. }
  89. return TRUE;
  90. }
  91. BOOL ZipAddFiles(zipFile zf, LPCTSTR lpszFileNameInZip, LPCTSTR lpszFiles, bool bUtf8 = false)
  92. {
  93. WIN32_FIND_DATA wfd;
  94. ZeroMemory(&wfd, sizeof(wfd));
  95. HANDLE hFind = FindFirstFile(lpszFiles, &wfd);
  96. if (hFind == INVALID_HANDLE_VALUE)
  97. {
  98. return FALSE;
  99. }
  100. LOKI_ON_BLOCK_EXIT(FindClose, hFind);
  101. CString strFilePath = lpszFiles;
  102. int nPos = strFilePath.ReverseFind('\\');
  103. if (nPos != -1)
  104. {
  105. strFilePath = strFilePath.Left(nPos + 1);
  106. }
  107. else
  108. {
  109. strFilePath.Empty();
  110. }
  111. CString strFileNameInZip = lpszFileNameInZip;
  112. do
  113. {
  114. CString strFileName = wfd.cFileName;
  115. if (strFileName == _T(".") || strFileName == _T(".."))
  116. {
  117. continue;
  118. }
  119. if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
  120. {
  121. if (!ZipAddFile(zf, strFileNameInZip + strFileName + _T("/"), strFilePath + strFileName, bUtf8))
  122. {
  123. return FALSE;
  124. }
  125. if (!ZipAddFiles(zf, strFileNameInZip + strFileName + _T("/"), strFilePath + strFileName + _T("\\*"), bUtf8))
  126. {
  127. return FALSE;
  128. }
  129. }
  130. else
  131. {
  132. if (!ZipAddFile(zf, strFileNameInZip + strFileName, strFilePath + strFileName, bUtf8))
  133. {
  134. return FALSE;
  135. }
  136. }
  137. } while (FindNextFile(hFind, &wfd));
  138. return TRUE;
  139. }
  140. BOOL ZipCompress(LPCTSTR lpszSourceFiles, LPCTSTR lpszDestFile, bool bUtf8 /*= false*/)
  141. {
  142. CStringA strDestFile = UCS2ToANSI(lpszDestFile);
  143. zipFile zf = zipOpen64(strDestFile, 0);
  144. if (zf == NULL)
  145. {
  146. return FALSE;
  147. }
  148. LOKI_ON_BLOCK_EXIT(zipClose, zf, (const char *)NULL);
  149. if (!ZipAddFiles(zf, _T(""), lpszSourceFiles, bUtf8))
  150. {
  151. return FALSE;
  152. }
  153. return TRUE;
  154. }
  155. BOOL ZipExtractCurrentFile(unzFile uf, LPCTSTR lpszDestFolder)
  156. {
  157. char szFilePathA[MAX_PATH];
  158. unz_file_info64 FileInfo;
  159. if (unzGetCurrentFileInfo64(uf, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0) != UNZ_OK)
  160. {
  161. return FALSE;
  162. }
  163. if (unzOpenCurrentFile(uf) != UNZ_OK)
  164. {
  165. return FALSE;
  166. }
  167. LOKI_ON_BLOCK_EXIT(unzCloseCurrentFile, uf);
  168. CString strDestPath = lpszDestFolder;
  169. CString strFileName;
  170. if ((FileInfo.flag & ZIP_GPBF_LANGUAGE_ENCODING_FLAG) != 0)
  171. {
  172. strFileName = ANSIToUCS2(szFilePathA, CP_UTF8);
  173. }
  174. else
  175. {
  176. strFileName = ANSIToUCS2(szFilePathA);
  177. }
  178. int nLength = strFileName.GetLength();
  179. LPTSTR lpszFileName = strFileName.GetBuffer();
  180. LPTSTR lpszCurrentFile = lpszFileName;
  181. LOKI_ON_BLOCK_EXIT_OBJ(strFileName, &CString::ReleaseBuffer, -1);
  182. for (int i = 0; i <= nLength; ++i)
  183. {
  184. if (lpszFileName[i] == _T('\0'))
  185. {
  186. strDestPath += lpszCurrentFile;
  187. break;
  188. }
  189. if (lpszFileName[i] == '\\' || lpszFileName[i] == '/')
  190. {
  191. lpszFileName[i] = '\0';
  192. strDestPath += lpszCurrentFile;
  193. strDestPath += _T("\\");
  194. CreateDirectory(strDestPath, NULL);
  195. lpszCurrentFile = lpszFileName + i + 1;
  196. }
  197. }
  198. if (lpszCurrentFile[0] == _T('\0'))
  199. {
  200. return TRUE;
  201. }
  202. HANDLE hFile = CreateFile(strDestPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
  203. if (hFile == INVALID_HANDLE_VALUE)
  204. {
  205. DWORD error = GetLastError();
  206. return FALSE;
  207. }
  208. LOKI_ON_BLOCK_EXIT(CloseHandle, hFile);
  209. const DWORD BUFFER_SIZE = 4096;
  210. BYTE byBuffer[BUFFER_SIZE];
  211. while (true)
  212. {
  213. int nSize = unzReadCurrentFile(uf, byBuffer, BUFFER_SIZE);
  214. if (nSize < 0)
  215. {
  216. return FALSE;
  217. }
  218. else if (nSize == 0)
  219. {
  220. break;
  221. }
  222. else
  223. {
  224. DWORD dwWritten = 0;
  225. if (!WriteFile(hFile, byBuffer, (DWORD)nSize, &dwWritten, NULL) || dwWritten != (DWORD)nSize)
  226. {
  227. return FALSE;
  228. }
  229. }
  230. }
  231. FILETIME ftLocal, ftUTC;
  232. DosDateTimeToFileTime((WORD)(FileInfo.dosDate>>16), (WORD)FileInfo.dosDate, &ftLocal);
  233. LocalFileTimeToFileTime(&ftLocal, &ftUTC);
  234. SetFileTime(hFile, &ftUTC, &ftUTC, &ftUTC);
  235. return TRUE;
  236. }
  237. BOOL ZipExtract(LPCTSTR lpszSourceFile, LPCTSTR lpszDestFolder)
  238. {
  239. CStringA strSourceFileA = UCS2ToANSI(lpszSourceFile);
  240. unzFile uf = unzOpen64(strSourceFileA);
  241. if (uf == NULL)
  242. {
  243. return FALSE;
  244. }
  245. LOKI_ON_BLOCK_EXIT(unzClose, uf);
  246. unz_global_info64 gi;
  247. if (unzGetGlobalInfo64(uf, &gi) != UNZ_OK)
  248. {
  249. return FALSE;
  250. }
  251. CString strDestFolder = lpszDestFolder;
  252. CreateDirectory(lpszDestFolder, NULL);
  253. if (!strDestFolder.IsEmpty() && strDestFolder[strDestFolder.GetLength() - 1] != _T('\\'))
  254. {
  255. strDestFolder += _T("\\");
  256. }
  257. for (int i = 0; i < gi.number_entry; ++i)
  258. {
  259. if (!ZipExtractCurrentFile(uf, strDestFolder))
  260. {
  261. return FALSE;
  262. }
  263. if (i < gi.number_entry - 1)
  264. {
  265. if (unzGoToNextFile(uf) != UNZ_OK)
  266. {
  267. return FALSE;
  268. }
  269. }
  270. }
  271. return TRUE;
  272. }