XUnZipZilb.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. #include <errno.h>
  10. #endif // RVC_OS_WIN
  11. #define ZIP_ZILP_MAX_FILENAME 512
  12. #define ZIP_ZILP_READ_SIZE 4096
  13. #define ZIP_ZILP_HOST_SYSTEM(VERSION_MADEBY) ((uint8_t)(VERSION_MADEBY >> 8))
  14. #define ZIP_ZILP_HOST_SYSTEM_MSDOS (0)
  15. #define ZIP_ZILP_HOST_SYSTEM_UNIX (3)
  16. #define ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS (10)
  17. #define ZIP_ZILP_HOST_SYSTEM_RISCOS (13)
  18. #define ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN (19)
  19. int UnZipToDir(string zipFileName, string destDirPath)
  20. {
  21. // Dbg("zipFileName %s destDirPath %s", zipFileName.c_str(), destDirPath.c_str());
  22. unzFile zfile = unzOpen(zipFileName.c_str());
  23. if (zfile == NULL)
  24. {
  25. Dbg("could open zipFile (%s)", zipFileName.c_str());
  26. return -1;
  27. }
  28. //create directory
  29. if (!ExistsDirA(destDirPath.c_str())) {
  30. if (!CreateDirA(destDirPath.c_str(), true)) {
  31. Dbg("create temp unzip root dir fail: %s",destDirPath.c_str());
  32. return -1;
  33. }
  34. }
  35. unz_global_info global_info;
  36. if (unzGetGlobalInfo(zfile, &global_info) != UNZ_OK)
  37. {
  38. Dbg("could not read file global info (%s)", zipFileName.c_str());
  39. unzClose(zfile);
  40. return -1;
  41. }
  42. for (int i = 0; i < global_info.number_entry; i++) {
  43. if (unZipCurrentFile(zfile, destDirPath) != UNZ_OK) {
  44. Dbg("unzip fail %s", zipFileName.c_str());
  45. unzClose(zfile);
  46. return -1;
  47. }
  48. if ((i + 1) < global_info.number_entry) {
  49. int ret = unzGoToNextFile(zfile);
  50. if (ret != UNZ_OK) {
  51. Dbg("error %d with zipfile in unzGoToNextFile", ret);
  52. unzClose(zfile);
  53. return -1;
  54. }
  55. }
  56. }
  57. return 0;
  58. }
  59. //int unZipCurrentFile(zipFile zf, string destDirPath)
  60. //{
  61. // unz_file_info file_info;
  62. // char zipFilename[ZIP_ZILP_MAX_FILENAME];
  63. // memset(zipFilename, 0, ZIP_ZILP_MAX_FILENAME);
  64. //
  65. // string newFileName = "";
  66. // bool isDir = false;
  67. // if (unzGetCurrentFileInfo(zf, &file_info, zipFilename, ZIP_ZILP_MAX_FILENAME, NULL, 0, NULL, 0) != UNZ_OK)
  68. // {
  69. // Dbg("could not read file info");
  70. // return -1;
  71. // }
  72. //#ifdef RVC_OS_WIN
  73. // if (is_str_utf8(zipFilename)) {
  74. // //Dbg("file name is UTF8");
  75. // newFileName = utf8_to_gbk(zipFilename);
  76. // }
  77. // else {
  78. // //Dbg("file name is GBK");
  79. // newFileName = zipFilename;
  80. // }
  81. //#else
  82. // if (!is_str_utf8(zipFilename)) {
  83. // //Dbg("file name is GBK");
  84. // //newFileName = gbk_to_utf8(zipFilename);
  85. // unsigned char* tempName = utf8_string_create((const char*)zipFilename);
  86. // if (tempName == NULL) {
  87. // Dbg("get utf8 str is null");
  88. // return -1;
  89. // }
  90. // newFileName = (const char*)tempName;
  91. // free(tempName);
  92. // }
  93. // else {
  94. // //Dbg("file name is UTF8");
  95. // newFileName = zipFilename;
  96. // }
  97. //#endif // RVC_OS_WIN
  98. // Dbg("unZipCurrentFile newFileName %s", newFileName.c_str());
  99. // string filestr = newFileName;
  100. // //判断是文件还是文件夹
  101. //
  102. // if (filestr.substr(filestr.length() - 1, 1) == "/") {
  103. // isDir = true;
  104. // }
  105. //
  106. // if (isDir)
  107. // { //创建文件夹
  108. // string dirPath = destDirPath + SPLIT_SLASH_STR + newFileName;
  109. // Dbg("creating directory: %s", dirPath.c_str());
  110. // if (!CreateDirA(dirPath.c_str(), true))
  111. // {
  112. // Dbg("creating directory fail: dirPath(%s) zipFileName(%s)", dirPath.c_str(), newFileName.c_str());
  113. // return -1;
  114. // }
  115. // }
  116. // else
  117. // { //打开zip里面的文件
  118. // string fileNamePath = destDirPath + SPLIT_SLASH_STR + newFileName;
  119. // Dbg("creating file:%s", fileNamePath.c_str());
  120. //
  121. // //先创建文件的父文件夹
  122. // int pos = fileNamePath.find_last_of(SPLIT_SLASH);
  123. // string newFileDirPath(fileNamePath.substr(0, pos));
  124. // //Dbg("creating dir:%s", newFileDirPath.c_str());
  125. // if (!CreateDirA(newFileDirPath.c_str(), true)) {
  126. // Dbg("creating zip file dir fail: dirPath(%s)", newFileDirPath.c_str());
  127. // return -1;
  128. // }
  129. //
  130. // if (unzOpenCurrentFile(zf) != UNZ_OK)
  131. // {
  132. // Dbg("could not open zip file ,%s", newFileName);
  133. // return -1;
  134. // }
  135. // // Open a file to write out the data.
  136. // FILE* out = fopen(fileNamePath.c_str(), "wb");
  137. //
  138. // if (out == NULL)
  139. // {
  140. // Dbg("could not open destination file, %s", fileNamePath.c_str());
  141. // unzCloseCurrentFile(zf);
  142. // return -1;
  143. // }
  144. //
  145. // int err = UNZ_OK;
  146. // unsigned char * read_buffer= new unsigned char[ZIP_ZILP_READ_SIZE];
  147. // memset(read_buffer, 0, ZIP_ZILP_READ_SIZE);
  148. // do
  149. // {
  150. // err = unzReadCurrentFile(zf, read_buffer, ZIP_ZILP_READ_SIZE);
  151. // if (err < 0)
  152. // {
  153. // Dbg("error %d with zipfile in unzReadCurrentFile", err);
  154. // break;
  155. // }
  156. //
  157. // // Write data to file.
  158. // if (err > 0)
  159. // {
  160. // if (fwrite(read_buffer, err, 1, out) != 1) {
  161. // Dbg("error in writing extracted file");
  162. // err = UNZ_ERRNO;
  163. // break;
  164. // }
  165. // }
  166. // } while (err > 0);
  167. //
  168. // delete[] read_buffer;//删除临时对象
  169. //
  170. // if (out!=NULL) {
  171. // if (fclose(out)!=0) {
  172. // Dbg("fclose new file from zip fail, %s", fileNamePath.c_str());
  173. // unzCloseCurrentFile(zf);
  174. // return -1;
  175. // }
  176. // }
  177. //
  178. // if (err == UNZ_OK) {
  179. // //正常结束
  180. // err = unzCloseCurrentFile(zf);
  181. // if (err != UNZ_OK) {
  182. // Dbg("error %d with zipfile in unzCloseCurrentFile", err);
  183. // return -1;
  184. // }
  185. // if (changeUnZipFileAtt(fileNamePath.c_str())!=0) {
  186. // return -1;
  187. // }
  188. // return 0;
  189. // }
  190. // else {
  191. // //异常结束
  192. // unzCloseCurrentFile(zf); /* don't lose the error */
  193. // return -1;
  194. // }
  195. // }
  196. // return 0;
  197. //}
  198. int unZipCurrentFile(zipFile zf, string destDirPath)
  199. {
  200. unz_file_info file_info;
  201. char zipFilename[ZIP_ZILP_MAX_FILENAME];
  202. memset(zipFilename, 0, ZIP_ZILP_MAX_FILENAME);
  203. string newFileName = "";
  204. bool isDir = false;
  205. if (unzGetCurrentFileInfo(zf, &file_info, zipFilename, ZIP_ZILP_MAX_FILENAME, NULL, 0, NULL, 0) != UNZ_OK)
  206. {
  207. Dbg("could not read file info");
  208. return -1;
  209. }
  210. //转码
  211. #ifdef RVC_OS_WIN
  212. if (is_str_utf8(zipFilename)) {
  213. //Dbg("file name is UTF8");
  214. newFileName = utf8_to_gbk(zipFilename);
  215. }
  216. else {
  217. //Dbg("file name is GBK");
  218. newFileName = zipFilename;
  219. }
  220. #else
  221. if (!is_str_utf8(zipFilename)) {
  222. //Dbg("file name is GBK");
  223. //newFileName = gbk_to_utf8(zipFilename);
  224. unsigned char* tempName = utf8_string_create((const char*)zipFilename);
  225. if (tempName == NULL) {
  226. Dbg("get utf8 str is null");
  227. return -1;
  228. }
  229. newFileName = (const char*)tempName;
  230. free(tempName);
  231. }
  232. else {
  233. //Dbg("file name is UTF8");
  234. newFileName = zipFilename;
  235. }
  236. #endif // RVC_OS_WIN
  237. Dbg("unZipCurrentFile newFileName %s", newFileName.c_str());
  238. string filestr = newFileName;
  239. //判断是文件还是文件夹
  240. if (filestr.substr(filestr.length() - 1, 1) == "/") {
  241. isDir = true;
  242. }
  243. //替换newFileName字符串中的'/'到对应平台路径
  244. #ifdef RVC_OS_WIN
  245. if (!replacePlace(newFileName, "/", "\\")) {
  246. Dbg("replaceInPlace zip fileName fail:zipFileName(%s)", newFileName.c_str());
  247. return -1;
  248. }
  249. #else
  250. if (!replacePlace(newFileName, "\\", "/")) {
  251. Dbg("replaceInPlace zip fileName fail:zipFileName(%s)", newFileName.c_str());
  252. return -1;
  253. }
  254. #endif // DEBUG
  255. if (isDir)
  256. { //创建文件夹
  257. string dirPath = destDirPath + SPLIT_SLASH_STR + newFileName;
  258. Dbg("creating directory: %s", dirPath.c_str());
  259. if (!CreateDirA(dirPath.c_str(), true))
  260. {
  261. Dbg("creating directory fail: dirPath(%s) zipFileName(%s)", dirPath.c_str(), newFileName.c_str());
  262. return -1;
  263. }
  264. }
  265. else
  266. { //打开zip里面的文件
  267. string fileNamePath = destDirPath + SPLIT_SLASH_STR + newFileName;
  268. Dbg("creating file:%s", fileNamePath.c_str());
  269. //先创建文件的父文件夹
  270. int pos = fileNamePath.find_last_of(SPLIT_SLASH);
  271. string newFileDirPath(fileNamePath.substr(0, pos));
  272. if (!CreateDirA(newFileDirPath.c_str(), true)) {
  273. Dbg("creating zip file dir fail: dirPath(%s)", newFileDirPath.c_str());
  274. return -1;
  275. }
  276. if (unzOpenCurrentFile(zf) != UNZ_OK)
  277. {
  278. Dbg("could not open zip file ,%s", newFileName);
  279. return -1;
  280. }
  281. bool isSymlink = false;
  282. #ifdef RVC_OS_WIN
  283. isSymlink = false;//windows 默认不创建链接文件
  284. //if (entry_is_symlink(file_info) == 0) {
  285. // isSymlink = true;
  286. // printf("entry is link file, %s\n", newFileName.c_str());
  287. //}
  288. #else
  289. isSymlink = false;//linux 默认不创建链接文件
  290. //if (entry_is_symlink(file_info) == 0) {
  291. // isSymlink = true;
  292. // Dbg("entry is link file, %s\n", newFileName.c_str());
  293. //}
  294. #endif
  295. // Open a file to write out the data.
  296. int saveSucc = 0;
  297. if (isSymlink) {
  298. //链接文件
  299. saveSucc = saveSymlink(zf, fileNamePath);
  300. }
  301. else {
  302. //一般文件
  303. saveSucc = saveNormalFile(zf, fileNamePath);
  304. }
  305. if (saveSucc == 0) {
  306. //正常结束
  307. int err = unzCloseCurrentFile(zf);
  308. if (err != UNZ_OK) {
  309. Dbg("zipfile unzCloseCurrentFile is %d", err);
  310. return -1;
  311. }
  312. //文件赋权
  313. if (changeUnZipFileAtt(fileNamePath.c_str()) != 0) {
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. else {
  319. //保存异常
  320. unzCloseCurrentFile(zf); /* don't lose the error */
  321. return -1;
  322. }
  323. }
  324. return 0;
  325. }
  326. bool is_str_utf8(const char* str)
  327. {
  328. unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
  329. unsigned char chr = *str;
  330. bool bAllAscii = true;
  331. for (unsigned int i = 0; str[i] != '\0'; ++i) {
  332. chr = *(str + i);
  333. //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
  334. if (nBytes == 0 && (chr & 0x80) != 0) {
  335. bAllAscii = false;
  336. }
  337. if (nBytes == 0) {
  338. //如果不是ASCII码,应该是多字节符,计算字节数
  339. if (chr >= 0x80) {
  340. if (chr >= 0xFC && chr <= 0xFD) {
  341. nBytes = 6;
  342. }
  343. else if (chr >= 0xF8) {
  344. nBytes = 5;
  345. }
  346. else if (chr >= 0xF0) {
  347. nBytes = 4;
  348. }
  349. else if (chr >= 0xE0) {
  350. nBytes = 3;
  351. }
  352. else if (chr >= 0xC0) {
  353. nBytes = 2;
  354. }
  355. else {
  356. return false;
  357. }
  358. nBytes--;
  359. }
  360. }
  361. else {
  362. //多字节符的非首字节,应为 10xxxxxx
  363. if ((chr & 0xC0) != 0x80) {
  364. return false;
  365. }
  366. //减到为零为止
  367. nBytes--;
  368. }
  369. }
  370. //违返UTF8编码规则
  371. if (nBytes != 0) {
  372. return false;
  373. }
  374. if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
  375. return true;
  376. }
  377. return true;
  378. }
  379. int changeUnZipFileAtt(const char* path)
  380. {
  381. #ifdef RVC_OS_WIN
  382. return 0;//windows 默认返回成功
  383. #else
  384. struct stat attr_of_del;
  385. if (lstat(path, &attr_of_del) == 0)
  386. {
  387. //修改为775属性
  388. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  389. if (chmod(path, f_attrib) != 0)
  390. {
  391. Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. else {
  397. Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
  398. return -1;
  399. }
  400. #endif
  401. }
  402. bool replacePlace(string& str, string const& replaceThis, string const& withThis)
  403. {
  404. bool replaced = false;
  405. size_t i = str.find(replaceThis);
  406. while (i != string::npos) {
  407. replaced = true;
  408. str = str.substr(0, i) + withThis + str.substr(i + replaceThis.size());
  409. if (i < str.size() - withThis.size())
  410. i = str.find(replaceThis, i + withThis.size());
  411. else
  412. i = string::npos;
  413. }
  414. return replaced;
  415. }
  416. int saveNormalFile(zipFile zf, string savePath) {
  417. // Open a file to write out the data.
  418. FILE* out = fopen(savePath.c_str(), "wb");
  419. if (out == NULL)
  420. {
  421. Dbg("could not open destination file, %s", savePath.c_str());
  422. return -1;
  423. }
  424. int err = 0;
  425. unsigned char* read_buffer = new unsigned char[ZIP_ZILP_READ_SIZE];
  426. memset(read_buffer, 0, ZIP_ZILP_READ_SIZE);
  427. do
  428. {
  429. err = unzReadCurrentFile(zf, read_buffer, ZIP_ZILP_READ_SIZE);
  430. if (err < 0)
  431. {
  432. Dbg("zipfile in unzReadCurrentFile is fail:%d", err);
  433. err = -1;
  434. break;
  435. }
  436. if (err > 0)
  437. {
  438. // Write data to file.
  439. if (fwrite(read_buffer, err, 1, out) != 1) {
  440. Dbg("writing extracted normal file is fail");
  441. err = -1;
  442. break;
  443. }
  444. else {
  445. //及时写入缓存数据
  446. if (fflush(out) != 0) {
  447. Dbg("fflush extracted normal file is fail");
  448. err = -1;
  449. break;
  450. }
  451. }
  452. }
  453. if (err == 0) {
  454. //已经到结尾
  455. break;
  456. }
  457. } while (true);
  458. delete[] read_buffer;//删除临时对象
  459. if (out != NULL) {
  460. if (fclose(out) != 0) {
  461. Dbg("fclose normal file fail");
  462. return -1;
  463. }
  464. }
  465. return err;
  466. }
  467. int saveSymlink(zipFile zf, string savePath) {
  468. //链接文件
  469. char* srcPath = new char[4096];//链接的原地址路径
  470. memset(srcPath, 0, 4096);
  471. string destPath = savePath;//新地址
  472. unsigned char* readStr = new unsigned char[512];
  473. memset(readStr, 0, 512);
  474. int err = 0;
  475. int copy = 0;
  476. do
  477. {
  478. err = unzReadCurrentFile(zf, readStr, 512);
  479. if (err < 0)
  480. {
  481. Dbg("zipfile in unzReadCurrentFile is fail:%d", err);
  482. err = -1;
  483. break;
  484. }
  485. // Write data to file.
  486. if (err > 0)
  487. {
  488. if ((copy + err) <= 4096) {
  489. memcpy(srcPath + copy, readStr, err);
  490. copy += err;
  491. }
  492. else {
  493. Dbg("link content len over 4096 ,check zipfile in unzReadCurrentFile, %s ", savePath.c_str());
  494. err = -1;
  495. break;
  496. }
  497. }
  498. if (err == 0) {
  499. //已经到结尾
  500. break;
  501. }
  502. } while (true);
  503. delete[] readStr;
  504. if (err == 0) {
  505. err = os_make_symlink((const char*)srcPath, destPath.c_str());
  506. delete[] srcPath; //删除临时对象
  507. return err;
  508. }
  509. else {
  510. delete[] srcPath;//删除临时对象
  511. return -1;
  512. }
  513. return 0;
  514. }
  515. int entry_is_symlink(unz_file_info file_info)
  516. {
  517. uint32_t posix_attrib = 0;
  518. uint8_t system = ZIP_ZILP_HOST_SYSTEM(file_info.version);
  519. int32_t err = 0;
  520. err = zip_attrib_convert(system, file_info.external_fa, ZIP_ZILP_HOST_SYSTEM_UNIX, &posix_attrib);
  521. if (err == 0) {
  522. if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
  523. return 0;
  524. }
  525. return -1;
  526. }
  527. int zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t* target_attrib)
  528. {
  529. if (target_attrib == NULL)
  530. return -1;
  531. *target_attrib = 0;
  532. if ((src_sys == ZIP_ZILP_HOST_SYSTEM_MSDOS) || (src_sys == ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS)) {
  533. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_MSDOS) || (target_sys == ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS)) {
  534. *target_attrib = src_attrib;
  535. return 0;
  536. }
  537. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_UNIX) || (target_sys == ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN) || (target_sys == ZIP_ZILP_HOST_SYSTEM_RISCOS))
  538. return zip_attrib_win32_to_posix(src_attrib, target_attrib);
  539. }
  540. else if ((src_sys == ZIP_ZILP_HOST_SYSTEM_UNIX) || (src_sys == ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN) || (src_sys == ZIP_ZILP_HOST_SYSTEM_RISCOS)) {
  541. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_UNIX) || (target_sys == ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN) || (target_sys == ZIP_ZILP_HOST_SYSTEM_RISCOS)) {
  542. /* If high bytes are set, it contains unix specific attributes */
  543. if ((src_attrib >> 16) != 0)
  544. src_attrib >>= 16;
  545. *target_attrib = src_attrib;
  546. return 0;
  547. }
  548. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_MSDOS) || (target_sys == ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS))
  549. return zip_attrib_posix_to_win32(src_attrib, target_attrib);
  550. }
  551. return -2;
  552. }
  553. int zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t* win32_attrib)
  554. {
  555. if (win32_attrib == NULL)
  556. return -1;
  557. *win32_attrib = 0;
  558. /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
  559. if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
  560. *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
  561. /* S_IFLNK */
  562. if ((posix_attrib & 0170000) == 0120000)
  563. *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
  564. /* S_IFDIR */
  565. else if ((posix_attrib & 0170000) == 0040000)
  566. *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
  567. /* S_IFREG */
  568. else
  569. *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
  570. return 0;
  571. }
  572. int zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t* posix_attrib)
  573. {
  574. if (posix_attrib == NULL)
  575. return -1;
  576. *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
  577. /* FILE_ATTRIBUTE_READONLY */
  578. if ((win32_attrib & 0x01) == 0)
  579. *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
  580. /* FILE_ATTRIBUTE_REPARSE_POINT */
  581. if ((win32_attrib & 0x400) == 0x400)
  582. *posix_attrib |= 0120000; /* S_IFLNK */
  583. /* FILE_ATTRIBUTE_DIRECTORY */
  584. else if ((win32_attrib & 0x10) == 0x10)
  585. *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
  586. else
  587. *posix_attrib |= 0100000; /* S_IFREG */
  588. return 0;
  589. }
  590. int os_make_symlink(const char* srcPath, const char* target_path) {
  591. #ifdef RVC_OS_WIN
  592. return 0;//暂不实现,暂时写入后缀名是link的文件
  593. #else
  594. if (symlink(srcPath, target_path) != 0) {
  595. Dbg("symlink fail,errno = %d", errno);
  596. return -1;
  597. }
  598. return 0;
  599. #endif // RVC_OS_WIN
  600. }
  601. #ifdef RVC_OS_WIN
  602. #else
  603. unsigned char* utf8_string_create(const char* string) {
  604. iconv_t cd;
  605. const char* from_encoding = "CP936";
  606. size_t result = 0;
  607. size_t string_length = 0;
  608. size_t string_utf8_size = 0;
  609. unsigned char* string_utf8 = NULL;
  610. unsigned char* string_utf8_ptr = NULL;
  611. if (string == NULL)
  612. return NULL;
  613. cd = iconv_open("UTF-8", from_encoding);
  614. if (cd == (iconv_t)-1)
  615. return NULL;
  616. string_length = strlen(string);
  617. string_utf8_size = string_length * 2;
  618. string_utf8 = (unsigned char*)malloc((int)(string_utf8_size + 1));
  619. string_utf8_ptr = string_utf8;
  620. if (string_utf8) {
  621. memset(string_utf8, 0, string_utf8_size + 1);
  622. result = iconv(cd, (char**)&string, &string_length,
  623. (char**)&string_utf8_ptr, &string_utf8_size);
  624. }
  625. iconv_close(cd);
  626. if (result == (size_t)-1) {
  627. free(string_utf8);
  628. string_utf8 = NULL;
  629. }
  630. return string_utf8;
  631. }
  632. #endif //RVC_OS_WIN