123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #include "XUnZipZilb.h"
- #include "libtoolkit/path.h"
- #include "fileutil.h"
- #include <string.h>
- #ifdef RVC_OS_WIN
- #else
- #include <iconv.h>
- #endif // RVC_OS_WIN
- #define ZIP_ZILP_MAX_FILENAME 256
- #define ZIP_ZILP_READ_SIZE 8192
- int UnZipToDir(string zipFileName, string destDirPath)
- {
- // Dbg("zipFileName %s destDirPath %s", zipFileName.c_str(), destDirPath.c_str());
- unzFile zfile = unzOpen(zipFileName.c_str());
- if (zfile == NULL)
- {
- Dbg("could open zipFile (%s)", zipFileName.c_str());
- return -1;
- }
- //create directory
- if (!ExistsDirA(destDirPath.c_str())) {
- if (!CreateDirA(destDirPath.c_str(), true)) {
- Dbg("create temp unzip root dir fail: %s",destDirPath.c_str());
- return -1;
- }
- }
- unz_global_info global_info;
- if (unzGetGlobalInfo(zfile, &global_info) != UNZ_OK)
- {
- Dbg("could not read file global info (%s)", zipFileName.c_str());
- unzClose(zfile);
- return -1;
- }
- for (int i = 0; i < global_info.number_entry; i++) {
- if (unZipCurrentFile(zfile, destDirPath) != UNZ_OK) {
- Dbg("unzip fail %s", zipFileName.c_str());
- unzClose(zfile);
- return -1;
- }
- if ((i + 1) < global_info.number_entry) {
- int ret = unzGoToNextFile(zfile);
- if (ret != UNZ_OK) {
- Dbg("error %d with zipfile in unzGoToNextFile", ret);
- unzClose(zfile);
- return -1;
- }
- }
- }
- return 0;
- }
- int unZipCurrentFile(zipFile zf, string destDirPath)
- {
- unz_file_info file_info;
- char zipFilename[ZIP_ZILP_MAX_FILENAME];
- char read_buffer[ZIP_ZILP_READ_SIZE];
- string newFileName = "";
- bool isDir = false;
- if (unzGetCurrentFileInfo(zf, &file_info, zipFilename, ZIP_ZILP_MAX_FILENAME, NULL, 0, NULL, 0) != UNZ_OK)
- {
- Dbg("could not read file info");
- return -1;
- }
- #ifdef RVC_OS_WIN
- if (is_str_utf8(zipFilename)) {
- //Dbg("file name is UTF8");
- newFileName = utf8_to_gbk(zipFilename);
- }
- else {
- //Dbg("file name is GBK");
- newFileName = zipFilename;
- }
- #else
- if (!is_str_utf8(zipFilename)) {
- //Dbg("file name is GBK");
- //newFileName = gbk_to_utf8(zipFilename);
- unsigned char* tempName = utf8_string_create((const char*)zipFilename);
- if (tempName == NULL) {
- Dbg("get utf8 str is null");
- return -1;
- }
- newFileName = (const char*)tempName;
- free(tempName);
- }
- else {
- //Dbg("file name is UTF8");
- newFileName = zipFilename;
- }
- #endif // RVC_OS_WIN
- Dbg("unZipCurrentFile newFileName %s", newFileName.c_str());
- string filestr = newFileName;
- //判断是文件还是文件夹
- if (filestr.substr(filestr.length() - 1, 1) == "/") {
- isDir = true;
- }
- if (isDir)
- { //创建文件夹
- string dirPath = destDirPath + SPLIT_SLASH_STR + newFileName;
- Dbg("creating directory: %s", dirPath.c_str());
- if (!CreateDirA(dirPath.c_str(), true))
- {
- Dbg("creating directory fail: dirPath(%s) zipFileName(%s)", dirPath.c_str(), newFileName.c_str());
- return -1;
- }
- }
- else
- { //打开zip里面的文件
- string fileNamePath = destDirPath + SPLIT_SLASH_STR + newFileName;
- Dbg("creating file:%s", fileNamePath.c_str());
- //先创建文件的父文件夹
- int pos = fileNamePath.find_last_of(SPLIT_SLASH);
- string newFileDirPath(fileNamePath.substr(0, pos));
- //Dbg("creating dir:%s", newFileDirPath.c_str());
- if (!CreateDirA(newFileDirPath.c_str(), true)) {
- Dbg("creating zip file dir fail: dirPath(%s)", newFileDirPath.c_str());
- return -1;
- }
- if (unzOpenCurrentFile(zf) != UNZ_OK)
- {
- Dbg("could not open zip file ,%s", newFileName);
- return -1;
- }
- // Open a file to write out the data.
- FILE* out = fopen(fileNamePath.c_str(), "wb");
- if (out == NULL)
- {
- Dbg("could not open destination file");
- unzCloseCurrentFile(zf);
- return -1;
- }
- int err = UNZ_OK;
- do
- {
- err = unzReadCurrentFile(zf, read_buffer, ZIP_ZILP_READ_SIZE);
- if (err < 0)
- {
- Dbg("error %d with zipfile in unzReadCurrentFile", err);
- break;
- }
- // Write data to file.
- if (err > 0)
- {
- if (fwrite(read_buffer, err, 1, out) != 1) {
- Dbg("error in writing extracted file");
- err = UNZ_ERRNO;
- break;
- }
- }
- } while (err > 0);
- if (out) {
- fclose(out);
- }
- if (err == 0) {
- //TODO 转换文件时间
- }
- if (err == UNZ_OK) {
- err = unzCloseCurrentFile(zf);
- if (err != UNZ_OK) {
- Dbg("error %d with zipfile in unzCloseCurrentFile", err);
- }
- }
- else {
- unzCloseCurrentFile(zf); /* don't lose the error */
- }
- }
- return 0;
- }
- bool is_str_utf8(const char* str)
- {
- unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
- unsigned char chr = *str;
- bool bAllAscii = true;
- for (unsigned int i = 0; str[i] != '\0'; ++i) {
- chr = *(str + i);
- //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
- if (nBytes == 0 && (chr & 0x80) != 0) {
- bAllAscii = false;
- }
- if (nBytes == 0) {
- //如果不是ASCII码,应该是多字节符,计算字节数
- if (chr >= 0x80) {
- if (chr >= 0xFC && chr <= 0xFD) {
- nBytes = 6;
- }
- else if (chr >= 0xF8) {
- nBytes = 5;
- }
- else if (chr >= 0xF0) {
- nBytes = 4;
- }
- else if (chr >= 0xE0) {
- nBytes = 3;
- }
- else if (chr >= 0xC0) {
- nBytes = 2;
- }
- else {
- return false;
- }
- nBytes--;
- }
- }
- else {
- //多字节符的非首字节,应为 10xxxxxx
- if ((chr & 0xC0) != 0x80) {
- return false;
- }
- //减到为零为止
- nBytes--;
- }
- }
- //违返UTF8编码规则
- if (nBytes != 0) {
- return false;
- }
- if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
- return true;
- }
- return true;
- }
- #ifdef RVC_OS_WIN
- #else
- unsigned char* utf8_string_create(const char* string) {
- iconv_t cd;
- const char* from_encoding = "CP936";
- size_t result = 0;
- size_t string_length = 0;
- size_t string_utf8_size = 0;
- unsigned char* string_utf8 = NULL;
- unsigned char* string_utf8_ptr = NULL;
- if (string == NULL)
- return NULL;
- cd = iconv_open("UTF-8", from_encoding);
- if (cd == (iconv_t)-1)
- return NULL;
- string_length = strlen(string);
- string_utf8_size = string_length * 2;
- string_utf8 = (unsigned char*)malloc((int)(string_utf8_size + 1));
- string_utf8_ptr = string_utf8;
- if (string_utf8) {
- memset(string_utf8, 0, string_utf8_size + 1);
- result = iconv(cd, (char**)&string, &string_length,
- (char**)&string_utf8_ptr, &string_utf8_size);
- }
- iconv_close(cd);
- if (result == (size_t)-1) {
- free(string_utf8);
- string_utf8 = NULL;
- }
- return string_utf8;
- }
- #endif //RVC_OS_WIN
|