XUnZipZilb.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "XUnZipZilb.h"
  2. #include "libtoolkit/path.h"
  3. #include "fileutil.h"
  4. #include <string.h>
  5. #ifdef RVC_OS_WIN
  6. #else
  7. #include <iconv.h>
  8. #endif // RVC_OS_WIN
  9. #define ZIP_ZILP_MAX_FILENAME 256
  10. #define ZIP_ZILP_READ_SIZE 8192
  11. int UnZipToDir(string zipFileName, string destDirPath)
  12. {
  13. // Dbg("zipFileName %s destDirPath %s", zipFileName.c_str(), destDirPath.c_str());
  14. unzFile zfile = unzOpen(zipFileName.c_str());
  15. if (zfile == NULL)
  16. {
  17. Dbg("could open zipFile (%s)", zipFileName.c_str());
  18. return -1;
  19. }
  20. //create directory
  21. if (!ExistsDirA(destDirPath.c_str())) {
  22. if (!CreateDirA(destDirPath.c_str(), true)) {
  23. Dbg("create temp unzip root dir fail: %s",destDirPath.c_str());
  24. return -1;
  25. }
  26. }
  27. unz_global_info global_info;
  28. if (unzGetGlobalInfo(zfile, &global_info) != UNZ_OK)
  29. {
  30. Dbg("could not read file global info (%s)", zipFileName.c_str());
  31. unzClose(zfile);
  32. return -1;
  33. }
  34. for (int i = 0; i < global_info.number_entry; i++) {
  35. if (unZipCurrentFile(zfile, destDirPath) != UNZ_OK) {
  36. Dbg("unzip fail %s", zipFileName.c_str());
  37. unzClose(zfile);
  38. return -1;
  39. }
  40. if ((i + 1) < global_info.number_entry) {
  41. int ret = unzGoToNextFile(zfile);
  42. if (ret != UNZ_OK) {
  43. Dbg("error %d with zipfile in unzGoToNextFile", ret);
  44. unzClose(zfile);
  45. return -1;
  46. }
  47. }
  48. }
  49. return 0;
  50. }
  51. int unZipCurrentFile(zipFile zf, string destDirPath)
  52. {
  53. unz_file_info file_info;
  54. char zipFilename[ZIP_ZILP_MAX_FILENAME];
  55. char read_buffer[ZIP_ZILP_READ_SIZE];
  56. string newFileName = "";
  57. bool isDir = false;
  58. if (unzGetCurrentFileInfo(zf, &file_info, zipFilename, ZIP_ZILP_MAX_FILENAME, NULL, 0, NULL, 0) != UNZ_OK)
  59. {
  60. Dbg("could not read file info");
  61. return -1;
  62. }
  63. #ifdef RVC_OS_WIN
  64. if (is_str_utf8(zipFilename)) {
  65. //Dbg("file name is UTF8");
  66. newFileName = utf8_to_gbk(zipFilename);
  67. }
  68. else {
  69. //Dbg("file name is GBK");
  70. newFileName = zipFilename;
  71. }
  72. #else
  73. if (!is_str_utf8(zipFilename)) {
  74. //Dbg("file name is GBK");
  75. //newFileName = gbk_to_utf8(zipFilename);
  76. unsigned char* tempName = utf8_string_create((const char*)zipFilename);
  77. if (tempName == NULL) {
  78. Dbg("get utf8 str is null");
  79. return -1;
  80. }
  81. newFileName = (const char*)tempName;
  82. free(tempName);
  83. }
  84. else {
  85. //Dbg("file name is UTF8");
  86. newFileName = zipFilename;
  87. }
  88. #endif // RVC_OS_WIN
  89. Dbg("unZipCurrentFile newFileName %s", newFileName.c_str());
  90. string filestr = newFileName;
  91. //判断是文件还是文件夹
  92. if (filestr.substr(filestr.length() - 1, 1) == "/") {
  93. isDir = true;
  94. }
  95. if (isDir)
  96. { //创建文件夹
  97. string dirPath = destDirPath + SPLIT_SLASH_STR + newFileName;
  98. Dbg("creating directory: %s", dirPath.c_str());
  99. if (!CreateDirA(dirPath.c_str(), true))
  100. {
  101. Dbg("creating directory fail: dirPath(%s) zipFileName(%s)", dirPath.c_str(), newFileName.c_str());
  102. return -1;
  103. }
  104. }
  105. else
  106. { //打开zip里面的文件
  107. string fileNamePath = destDirPath + SPLIT_SLASH_STR + newFileName;
  108. Dbg("creating file:%s", fileNamePath.c_str());
  109. //先创建文件的父文件夹
  110. int pos = fileNamePath.find_last_of(SPLIT_SLASH);
  111. string newFileDirPath(fileNamePath.substr(0, pos));
  112. //Dbg("creating dir:%s", newFileDirPath.c_str());
  113. if (!CreateDirA(newFileDirPath.c_str(), true)) {
  114. Dbg("creating zip file dir fail: dirPath(%s)", newFileDirPath.c_str());
  115. return -1;
  116. }
  117. if (unzOpenCurrentFile(zf) != UNZ_OK)
  118. {
  119. Dbg("could not open zip file ,%s", newFileName);
  120. return -1;
  121. }
  122. // Open a file to write out the data.
  123. FILE* out = fopen(fileNamePath.c_str(), "wb");
  124. if (out == NULL)
  125. {
  126. Dbg("could not open destination file");
  127. unzCloseCurrentFile(zf);
  128. return -1;
  129. }
  130. int err = UNZ_OK;
  131. do
  132. {
  133. err = unzReadCurrentFile(zf, read_buffer, ZIP_ZILP_READ_SIZE);
  134. if (err < 0)
  135. {
  136. Dbg("error %d with zipfile in unzReadCurrentFile", err);
  137. break;
  138. }
  139. // Write data to file.
  140. if (err > 0)
  141. {
  142. if (fwrite(read_buffer, err, 1, out) != 1) {
  143. Dbg("error in writing extracted file");
  144. err = UNZ_ERRNO;
  145. break;
  146. }
  147. }
  148. } while (err > 0);
  149. if (out) {
  150. fclose(out);
  151. }
  152. if (err == 0) {
  153. //TODO 转换文件时间
  154. }
  155. if (err == UNZ_OK) {
  156. err = unzCloseCurrentFile(zf);
  157. if (err != UNZ_OK) {
  158. Dbg("error %d with zipfile in unzCloseCurrentFile", err);
  159. }
  160. }
  161. else {
  162. unzCloseCurrentFile(zf); /* don't lose the error */
  163. }
  164. }
  165. return 0;
  166. }
  167. bool is_str_utf8(const char* str)
  168. {
  169. unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
  170. unsigned char chr = *str;
  171. bool bAllAscii = true;
  172. for (unsigned int i = 0; str[i] != '\0'; ++i) {
  173. chr = *(str + i);
  174. //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
  175. if (nBytes == 0 && (chr & 0x80) != 0) {
  176. bAllAscii = false;
  177. }
  178. if (nBytes == 0) {
  179. //如果不是ASCII码,应该是多字节符,计算字节数
  180. if (chr >= 0x80) {
  181. if (chr >= 0xFC && chr <= 0xFD) {
  182. nBytes = 6;
  183. }
  184. else if (chr >= 0xF8) {
  185. nBytes = 5;
  186. }
  187. else if (chr >= 0xF0) {
  188. nBytes = 4;
  189. }
  190. else if (chr >= 0xE0) {
  191. nBytes = 3;
  192. }
  193. else if (chr >= 0xC0) {
  194. nBytes = 2;
  195. }
  196. else {
  197. return false;
  198. }
  199. nBytes--;
  200. }
  201. }
  202. else {
  203. //多字节符的非首字节,应为 10xxxxxx
  204. if ((chr & 0xC0) != 0x80) {
  205. return false;
  206. }
  207. //减到为零为止
  208. nBytes--;
  209. }
  210. }
  211. //违返UTF8编码规则
  212. if (nBytes != 0) {
  213. return false;
  214. }
  215. if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
  216. return true;
  217. }
  218. return true;
  219. }
  220. #ifdef RVC_OS_WIN
  221. #else
  222. unsigned char* utf8_string_create(const char* string) {
  223. iconv_t cd;
  224. const char* from_encoding = "CP936";
  225. size_t result = 0;
  226. size_t string_length = 0;
  227. size_t string_utf8_size = 0;
  228. unsigned char* string_utf8 = NULL;
  229. unsigned char* string_utf8_ptr = NULL;
  230. if (string == NULL)
  231. return NULL;
  232. cd = iconv_open("UTF-8", from_encoding);
  233. if (cd == (iconv_t)-1)
  234. return NULL;
  235. string_length = strlen(string);
  236. string_utf8_size = string_length * 2;
  237. string_utf8 = (unsigned char*)malloc((int)(string_utf8_size + 1));
  238. string_utf8_ptr = string_utf8;
  239. if (string_utf8) {
  240. memset(string_utf8, 0, string_utf8_size + 1);
  241. result = iconv(cd, (char**)&string, &string_length,
  242. (char**)&string_utf8_ptr, &string_utf8_size);
  243. }
  244. iconv_close(cd);
  245. if (result == (size_t)-1) {
  246. free(string_utf8);
  247. string_utf8 = NULL;
  248. }
  249. return string_utf8;
  250. }
  251. #endif //RVC_OS_WIN