XUnZipZilb.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include <sys/stat.h>
  9. #endif // RVC_OS_WIN
  10. #define ZIP_ZILP_MAX_FILENAME 512
  11. #define ZIP_ZILP_READ_SIZE 4096
  12. int UnZipToDir(string zipFileName, string destDirPath)
  13. {
  14. // Dbg("zipFileName %s destDirPath %s", zipFileName.c_str(), destDirPath.c_str());
  15. unzFile zfile = unzOpen(zipFileName.c_str());
  16. if (zfile == NULL)
  17. {
  18. Dbg("could open zipFile (%s)", zipFileName.c_str());
  19. return -1;
  20. }
  21. //create directory
  22. if (!ExistsDirA(destDirPath.c_str())) {
  23. if (!CreateDirA(destDirPath.c_str(), true)) {
  24. Dbg("create temp unzip root dir fail: %s",destDirPath.c_str());
  25. return -1;
  26. }
  27. }
  28. unz_global_info global_info;
  29. if (unzGetGlobalInfo(zfile, &global_info) != UNZ_OK)
  30. {
  31. Dbg("could not read file global info (%s)", zipFileName.c_str());
  32. unzClose(zfile);
  33. return -1;
  34. }
  35. for (int i = 0; i < global_info.number_entry; i++) {
  36. if (unZipCurrentFile(zfile, destDirPath) != UNZ_OK) {
  37. Dbg("unzip fail %s", zipFileName.c_str());
  38. unzClose(zfile);
  39. return -1;
  40. }
  41. if ((i + 1) < global_info.number_entry) {
  42. int ret = unzGoToNextFile(zfile);
  43. if (ret != UNZ_OK) {
  44. Dbg("error %d with zipfile in unzGoToNextFile", ret);
  45. unzClose(zfile);
  46. return -1;
  47. }
  48. }
  49. }
  50. return 0;
  51. }
  52. int unZipCurrentFile(zipFile zf, string destDirPath)
  53. {
  54. unz_file_info file_info;
  55. char zipFilename[ZIP_ZILP_MAX_FILENAME];
  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, %s", fileNamePath.c_str());
  127. unzCloseCurrentFile(zf);
  128. return -1;
  129. }
  130. int err = UNZ_OK;
  131. unsigned char * read_buffer= new unsigned char[ZIP_ZILP_READ_SIZE];
  132. memset(read_buffer, 0, ZIP_ZILP_READ_SIZE);
  133. do
  134. {
  135. err = unzReadCurrentFile(zf, read_buffer, ZIP_ZILP_READ_SIZE);
  136. if (err < 0)
  137. {
  138. Dbg("error %d with zipfile in unzReadCurrentFile", err);
  139. break;
  140. }
  141. // Write data to file.
  142. if (err > 0)
  143. {
  144. if (fwrite(read_buffer, err, 1, out) != 1) {
  145. Dbg("error in writing extracted file");
  146. err = UNZ_ERRNO;
  147. break;
  148. }
  149. }
  150. } while (err > 0);
  151. delete[] read_buffer;//删除临时对象
  152. if (out!=NULL) {
  153. if (fclose(out)!=0) {
  154. Dbg("fclose new file from zip fail, %s", fileNamePath.c_str());
  155. unzCloseCurrentFile(zf);
  156. return -1;
  157. }
  158. }
  159. if (err == UNZ_OK) {
  160. //正常结束
  161. err = unzCloseCurrentFile(zf);
  162. if (err != UNZ_OK) {
  163. Dbg("error %d with zipfile in unzCloseCurrentFile", err);
  164. return -1;
  165. }
  166. if (changeUnZipFileAtt(fileNamePath.c_str())!=0) {
  167. return -1;
  168. }
  169. return 0;
  170. }
  171. else {
  172. //异常结束
  173. unzCloseCurrentFile(zf); /* don't lose the error */
  174. return -1;
  175. }
  176. }
  177. return 0;
  178. }
  179. bool is_str_utf8(const char* str)
  180. {
  181. unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
  182. unsigned char chr = *str;
  183. bool bAllAscii = true;
  184. for (unsigned int i = 0; str[i] != '\0'; ++i) {
  185. chr = *(str + i);
  186. //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
  187. if (nBytes == 0 && (chr & 0x80) != 0) {
  188. bAllAscii = false;
  189. }
  190. if (nBytes == 0) {
  191. //如果不是ASCII码,应该是多字节符,计算字节数
  192. if (chr >= 0x80) {
  193. if (chr >= 0xFC && chr <= 0xFD) {
  194. nBytes = 6;
  195. }
  196. else if (chr >= 0xF8) {
  197. nBytes = 5;
  198. }
  199. else if (chr >= 0xF0) {
  200. nBytes = 4;
  201. }
  202. else if (chr >= 0xE0) {
  203. nBytes = 3;
  204. }
  205. else if (chr >= 0xC0) {
  206. nBytes = 2;
  207. }
  208. else {
  209. return false;
  210. }
  211. nBytes--;
  212. }
  213. }
  214. else {
  215. //多字节符的非首字节,应为 10xxxxxx
  216. if ((chr & 0xC0) != 0x80) {
  217. return false;
  218. }
  219. //减到为零为止
  220. nBytes--;
  221. }
  222. }
  223. //违返UTF8编码规则
  224. if (nBytes != 0) {
  225. return false;
  226. }
  227. if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
  228. return true;
  229. }
  230. return true;
  231. }
  232. int changeUnZipFileAtt(const char* path)
  233. {
  234. #ifdef RVC_OS_WIN
  235. return 0;//windows 默认返回成功
  236. #else
  237. struct stat attr_of_del;
  238. if (lstat(path, &attr_of_del) == 0)
  239. {
  240. //修改为775属性
  241. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  242. if (chmod(path, f_attrib) != 0)
  243. {
  244. Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. else {
  250. Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
  251. return -1;
  252. }
  253. #endif
  254. }
  255. #ifdef RVC_OS_WIN
  256. #else
  257. unsigned char* utf8_string_create(const char* string) {
  258. iconv_t cd;
  259. const char* from_encoding = "CP936";
  260. size_t result = 0;
  261. size_t string_length = 0;
  262. size_t string_utf8_size = 0;
  263. unsigned char* string_utf8 = NULL;
  264. unsigned char* string_utf8_ptr = NULL;
  265. if (string == NULL)
  266. return NULL;
  267. cd = iconv_open("UTF-8", from_encoding);
  268. if (cd == (iconv_t)-1)
  269. return NULL;
  270. string_length = strlen(string);
  271. string_utf8_size = string_length * 2;
  272. string_utf8 = (unsigned char*)malloc((int)(string_utf8_size + 1));
  273. string_utf8_ptr = string_utf8;
  274. if (string_utf8) {
  275. memset(string_utf8, 0, string_utf8_size + 1);
  276. result = iconv(cd, (char**)&string, &string_length,
  277. (char**)&string_utf8_ptr, &string_utf8_size);
  278. }
  279. iconv_close(cd);
  280. if (result == (size_t)-1) {
  281. free(string_utf8);
  282. string_utf8 = NULL;
  283. }
  284. return string_utf8;
  285. }
  286. #endif //RVC_OS_WIN