XUnZipZilb.cpp 17 KB

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