XUnZipZilb.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. size_t fi = newFileName.find("/");//判断是否有"/"
  246. if (fi != string::npos) {
  247. if (!replacePlace(newFileName, "/", "\\")) {
  248. Dbg("replaceInPlace zip fileName fail:zipFileName(%s)", newFileName.c_str());
  249. return -1;
  250. }
  251. }
  252. #else
  253. size_t fi = newFileName.find("\\");//判断是否有"\\"
  254. if (fi != string::npos) {
  255. if (!replacePlace(newFileName, "\\", "/")) {
  256. Dbg("replaceInPlace zip fileName fail:zipFileName(%s)", newFileName.c_str());
  257. return -1;
  258. }
  259. }
  260. #endif // DEBUG
  261. if (isDir)
  262. { //创建文件夹
  263. string dirPath = destDirPath + SPLIT_SLASH_STR + newFileName;
  264. Dbg("creating directory: %s", dirPath.c_str());
  265. if (!CreateDirA(dirPath.c_str(), true))
  266. {
  267. Dbg("creating directory fail: dirPath(%s) zipFileName(%s)", dirPath.c_str(), newFileName.c_str());
  268. return -1;
  269. }
  270. }
  271. else
  272. { //打开zip里面的文件
  273. string fileNamePath = destDirPath + SPLIT_SLASH_STR + newFileName;
  274. Dbg("creating file:%s", fileNamePath.c_str());
  275. //先创建文件的父文件夹
  276. int pos = fileNamePath.find_last_of(SPLIT_SLASH);
  277. string newFileDirPath(fileNamePath.substr(0, pos));
  278. if (!CreateDirA(newFileDirPath.c_str(), true)) {
  279. Dbg("creating zip file dir fail: dirPath(%s)", newFileDirPath.c_str());
  280. return -1;
  281. }
  282. if (unzOpenCurrentFile(zf) != UNZ_OK)
  283. {
  284. Dbg("could not open zip file ,%s", newFileName);
  285. return -1;
  286. }
  287. bool isSymlink = false;
  288. #ifdef RVC_OS_WIN
  289. isSymlink = false;//windows 默认不创建链接文件
  290. //if (entry_is_symlink(file_info) == 0) {
  291. // isSymlink = true;
  292. // printf("entry is link file, %s\n", newFileName.c_str());
  293. //}
  294. #else
  295. isSymlink = false;//linux 默认不创建链接文件
  296. //if (entry_is_symlink(file_info) == 0) {
  297. // isSymlink = true;
  298. // Dbg("entry is link file, %s\n", newFileName.c_str());
  299. //}
  300. #endif
  301. // Open a file to write out the data.
  302. int saveSucc = 0;
  303. if (isSymlink) {
  304. //链接文件
  305. saveSucc = saveSymlink(zf, fileNamePath);
  306. }
  307. else {
  308. //一般文件
  309. saveSucc = saveNormalFile(zf, fileNamePath);
  310. }
  311. if (saveSucc == 0) {
  312. //正常结束
  313. int err = unzCloseCurrentFile(zf);
  314. if (err != UNZ_OK) {
  315. Dbg("zipfile unzCloseCurrentFile is %d", err);
  316. return -1;
  317. }
  318. //文件赋权
  319. if (changeUnZipFileAtt(fileNamePath.c_str()) != 0) {
  320. return -1;
  321. }
  322. return 0;
  323. }
  324. else {
  325. //保存异常
  326. unzCloseCurrentFile(zf); /* don't lose the error */
  327. return -1;
  328. }
  329. }
  330. return 0;
  331. }
  332. bool is_str_utf8(const char* str)
  333. {
  334. unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
  335. unsigned char chr = *str;
  336. bool bAllAscii = true;
  337. for (unsigned int i = 0; str[i] != '\0'; ++i) {
  338. chr = *(str + i);
  339. //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
  340. if (nBytes == 0 && (chr & 0x80) != 0) {
  341. bAllAscii = false;
  342. }
  343. if (nBytes == 0) {
  344. //如果不是ASCII码,应该是多字节符,计算字节数
  345. if (chr >= 0x80) {
  346. if (chr >= 0xFC && chr <= 0xFD) {
  347. nBytes = 6;
  348. }
  349. else if (chr >= 0xF8) {
  350. nBytes = 5;
  351. }
  352. else if (chr >= 0xF0) {
  353. nBytes = 4;
  354. }
  355. else if (chr >= 0xE0) {
  356. nBytes = 3;
  357. }
  358. else if (chr >= 0xC0) {
  359. nBytes = 2;
  360. }
  361. else {
  362. return false;
  363. }
  364. nBytes--;
  365. }
  366. }
  367. else {
  368. //多字节符的非首字节,应为 10xxxxxx
  369. if ((chr & 0xC0) != 0x80) {
  370. return false;
  371. }
  372. //减到为零为止
  373. nBytes--;
  374. }
  375. }
  376. //违返UTF8编码规则
  377. if (nBytes != 0) {
  378. return false;
  379. }
  380. if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
  381. return true;
  382. }
  383. return true;
  384. }
  385. int changeUnZipFileAtt(const char* path)
  386. {
  387. #ifdef RVC_OS_WIN
  388. return 0;//windows 默认返回成功
  389. #else
  390. struct stat attr_of_del;
  391. if (lstat(path, &attr_of_del) == 0)
  392. {
  393. //修改为775属性
  394. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  395. if (chmod(path, f_attrib) != 0)
  396. {
  397. Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
  398. return -1;
  399. }
  400. return 0;
  401. }
  402. else {
  403. Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
  404. return -1;
  405. }
  406. #endif
  407. }
  408. bool replacePlace(string& str, string const& replaceThis, string const& withThis)
  409. {
  410. bool replaced = false;
  411. size_t i = str.find(replaceThis);
  412. while (i != string::npos) {
  413. replaced = true;
  414. str = str.substr(0, i) + withThis + str.substr(i + replaceThis.size());
  415. if (i < str.size() - withThis.size())
  416. i = str.find(replaceThis, i + withThis.size());
  417. else
  418. i = string::npos;
  419. }
  420. return replaced;
  421. }
  422. int saveNormalFile(zipFile zf, string savePath) {
  423. // Open a file to write out the data.
  424. FILE* out = fopen(savePath.c_str(), "wb");
  425. if (out == NULL)
  426. {
  427. Dbg("could not open destination file, %s", savePath.c_str());
  428. return -1;
  429. }
  430. int err = 0;
  431. unsigned char* read_buffer = new unsigned char[ZIP_ZILP_READ_SIZE];
  432. memset(read_buffer, 0, ZIP_ZILP_READ_SIZE);
  433. do
  434. {
  435. err = unzReadCurrentFile(zf, read_buffer, ZIP_ZILP_READ_SIZE);
  436. if (err < 0)
  437. {
  438. Dbg("zipfile in unzReadCurrentFile is fail:%d", err);
  439. err = -1;
  440. break;
  441. }
  442. if (err > 0)
  443. {
  444. // Write data to file.
  445. if (fwrite(read_buffer, err, 1, out) != 1) {
  446. Dbg("writing extracted normal file is fail");
  447. err = -1;
  448. break;
  449. }
  450. else {
  451. //及时写入缓存数据
  452. if (fflush(out) != 0) {
  453. Dbg("fflush extracted normal file is fail");
  454. err = -1;
  455. break;
  456. }
  457. }
  458. }
  459. if (err == 0) {
  460. //已经到结尾
  461. break;
  462. }
  463. } while (true);
  464. delete[] read_buffer;//删除临时对象
  465. if (out != NULL) {
  466. if (fclose(out) != 0) {
  467. Dbg("fclose normal file fail");
  468. return -1;
  469. }
  470. }
  471. return err;
  472. }
  473. int saveSymlink(zipFile zf, string savePath) {
  474. //链接文件
  475. char* srcPath = new char[4096];//链接的原地址路径
  476. memset(srcPath, 0, 4096);
  477. string destPath = savePath;//新地址
  478. unsigned char* readStr = new unsigned char[512];
  479. memset(readStr, 0, 512);
  480. int err = 0;
  481. int copy = 0;
  482. do
  483. {
  484. err = unzReadCurrentFile(zf, readStr, 512);
  485. if (err < 0)
  486. {
  487. Dbg("zipfile in unzReadCurrentFile is fail:%d", err);
  488. err = -1;
  489. break;
  490. }
  491. // Write data to file.
  492. if (err > 0)
  493. {
  494. if ((copy + err) <= 4096) {
  495. memcpy(srcPath + copy, readStr, err);
  496. copy += err;
  497. }
  498. else {
  499. Dbg("link content len over 4096 ,check zipfile in unzReadCurrentFile, %s ", savePath.c_str());
  500. err = -1;
  501. break;
  502. }
  503. }
  504. if (err == 0) {
  505. //已经到结尾
  506. break;
  507. }
  508. } while (true);
  509. delete[] readStr;
  510. if (err == 0) {
  511. err = os_make_symlink((const char*)srcPath, destPath.c_str());
  512. delete[] srcPath; //删除临时对象
  513. return err;
  514. }
  515. else {
  516. delete[] srcPath;//删除临时对象
  517. return -1;
  518. }
  519. return 0;
  520. }
  521. int entry_is_symlink(unz_file_info file_info)
  522. {
  523. uint32_t posix_attrib = 0;
  524. uint8_t system = ZIP_ZILP_HOST_SYSTEM(file_info.version);
  525. int32_t err = 0;
  526. err = zip_attrib_convert(system, file_info.external_fa, ZIP_ZILP_HOST_SYSTEM_UNIX, &posix_attrib);
  527. if (err == 0) {
  528. if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
  529. return 0;
  530. }
  531. return -1;
  532. }
  533. int zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t* target_attrib)
  534. {
  535. if (target_attrib == NULL)
  536. return -1;
  537. *target_attrib = 0;
  538. if ((src_sys == ZIP_ZILP_HOST_SYSTEM_MSDOS) || (src_sys == ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS)) {
  539. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_MSDOS) || (target_sys == ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS)) {
  540. *target_attrib = src_attrib;
  541. return 0;
  542. }
  543. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_UNIX) || (target_sys == ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN) || (target_sys == ZIP_ZILP_HOST_SYSTEM_RISCOS))
  544. return zip_attrib_win32_to_posix(src_attrib, target_attrib);
  545. }
  546. else if ((src_sys == ZIP_ZILP_HOST_SYSTEM_UNIX) || (src_sys == ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN) || (src_sys == ZIP_ZILP_HOST_SYSTEM_RISCOS)) {
  547. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_UNIX) || (target_sys == ZIP_ZILP_HOST_SYSTEM_OSX_DARWIN) || (target_sys == ZIP_ZILP_HOST_SYSTEM_RISCOS)) {
  548. /* If high bytes are set, it contains unix specific attributes */
  549. if ((src_attrib >> 16) != 0)
  550. src_attrib >>= 16;
  551. *target_attrib = src_attrib;
  552. return 0;
  553. }
  554. if ((target_sys == ZIP_ZILP_HOST_SYSTEM_MSDOS) || (target_sys == ZIP_ZILP_HOST_SYSTEM_WINDOWS_NTFS))
  555. return zip_attrib_posix_to_win32(src_attrib, target_attrib);
  556. }
  557. return -2;
  558. }
  559. int zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t* win32_attrib)
  560. {
  561. if (win32_attrib == NULL)
  562. return -1;
  563. *win32_attrib = 0;
  564. /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
  565. if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
  566. *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
  567. /* S_IFLNK */
  568. if ((posix_attrib & 0170000) == 0120000)
  569. *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
  570. /* S_IFDIR */
  571. else if ((posix_attrib & 0170000) == 0040000)
  572. *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
  573. /* S_IFREG */
  574. else
  575. *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
  576. return 0;
  577. }
  578. int zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t* posix_attrib)
  579. {
  580. if (posix_attrib == NULL)
  581. return -1;
  582. *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
  583. /* FILE_ATTRIBUTE_READONLY */
  584. if ((win32_attrib & 0x01) == 0)
  585. *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
  586. /* FILE_ATTRIBUTE_REPARSE_POINT */
  587. if ((win32_attrib & 0x400) == 0x400)
  588. *posix_attrib |= 0120000; /* S_IFLNK */
  589. /* FILE_ATTRIBUTE_DIRECTORY */
  590. else if ((win32_attrib & 0x10) == 0x10)
  591. *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
  592. else
  593. *posix_attrib |= 0100000; /* S_IFREG */
  594. return 0;
  595. }
  596. int os_make_symlink(const char* srcPath, const char* target_path) {
  597. #ifdef RVC_OS_WIN
  598. return 0;//暂不实现,暂时写入后缀名是link的文件
  599. #else
  600. if (symlink(srcPath, target_path) != 0) {
  601. Dbg("symlink fail,errno = %d", errno);
  602. return -1;
  603. }
  604. return 0;
  605. #endif // RVC_OS_WIN
  606. }
  607. #ifdef RVC_OS_WIN
  608. #else
  609. unsigned char* utf8_string_create(const char* string) {
  610. iconv_t cd;
  611. const char* from_encoding = "CP936";
  612. size_t result = 0;
  613. size_t string_length = 0;
  614. size_t string_utf8_size = 0;
  615. unsigned char* string_utf8 = NULL;
  616. unsigned char* string_utf8_ptr = NULL;
  617. if (string == NULL)
  618. return NULL;
  619. cd = iconv_open("UTF-8", from_encoding);
  620. if (cd == (iconv_t)-1)
  621. return NULL;
  622. string_length = strlen(string);
  623. string_utf8_size = string_length * 2;
  624. string_utf8 = (unsigned char*)malloc((int)(string_utf8_size + 1));
  625. string_utf8_ptr = string_utf8;
  626. if (string_utf8) {
  627. memset(string_utf8, 0, string_utf8_size + 1);
  628. result = iconv(cd, (char**)&string, &string_length,
  629. (char**)&string_utf8_ptr, &string_utf8_size);
  630. }
  631. iconv_close(cd);
  632. if (result == (size_t)-1) {
  633. free(string_utf8);
  634. string_utf8 = NULL;
  635. }
  636. return string_utf8;
  637. }
  638. #endif //RVC_OS_WIN