lua 解压缩文件zlib

来源:互联网 发布:c007电话软件下载 编辑:程序博客网 时间:2024/06/04 19:13

bool ZipUtils::uncompressDir(char* filename, char* destPath){    if (filename == NULL || destPath == NULL)    {        CCLOG("source zip is null or dest path is null!");        return false;    }     // Open the zip file    std::string outFileName = filename;    unzFile zipfile = unzOpen(outFileName.c_str());    if (!zipfile)    {        CCLOG("can not open downloaded zip file %s", outFileName.c_str());        return false;    }     // Get info about the zip file    unz_global_info global_info;    if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)    {        CCLOG("can not read file global info of %s", outFileName.c_str());        unzClose(zipfile);        return false;    }     const int BUFFER_SIZE = 8192;    const int MAX_FILENAME = 512;    // Buffer to hold data read from the zip file    char readBuffer[BUFFER_SIZE];     CCLOG("start uncompressing");     // Loop to extract all files.    uLong i;    for (i = 0; i < global_info.number_entry; ++i)    {        // Get info about current file.        unz_file_info fileInfo;        char fileName[MAX_FILENAME];        if (unzGetCurrentFileInfo(zipfile,            &fileInfo,            fileName,            MAX_FILENAME,            NULL,            0,            NULL,            0) != UNZ_OK)        {            CCLOG("can not read file info");            unzClose(zipfile);            return false;        }         std::string storagePath = destPath;        std::string fullPath = storagePath + fileName;         // Check if this entry is a directory or a file.        const size_t filenameLength = strlen(fileName);        if (fileName[filenameLength - 1] == '/')        {            // get all dir            std::string fileNameStr = std::string(fileName);            size_t position = 0;            while ((position = fileNameStr.find_first_of("/", position)) != std::string::npos)            {                std::string dirPath = storagePath + fileNameStr.substr(0, position);                // Entry is a direcotry, so create it.                // If the directory exists, it will failed scilently.                if (!createDirectory(dirPath.c_str()))                {                    CCLOG("can not create directory %s", dirPath.c_str());                    //unzClose(zipfile);                    //return false;                }                position++;            }        }        else        {            // Entry is a file, so extract it.             // Open current file.            if (unzOpenCurrentFile(zipfile) != UNZ_OK)            {                CCLOG("can not open file %s", fileName);                unzClose(zipfile);                return false;            }             // Create a file to store current file.            FILE *out = fopen(fullPath.c_str(), "wb");            if (!out)            {                CCLOG("can not open destination file %s", fullPath.c_str());                unzCloseCurrentFile(zipfile);                unzClose(zipfile);                return false;            }             // Write current file content to destinate file.            int error = UNZ_OK;            do            {                error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);                if (error < 0)                {                    CCLOG("can not read zip file %s, error code is %d", fileName, error);                    unzCloseCurrentFile(zipfile);                    unzClose(zipfile);                    return false;                }                 if (error > 0)                {                    fwrite(readBuffer, error, 1, out);                }            } while (error > 0);             fclose(out);        }         unzCloseCurrentFile(zipfile);         // Goto next entry listed in the zip file.        if ((i + 1) < global_info.number_entry)        {            if (unzGoToNextFile(zipfile) != UNZ_OK)            {                CCLOG("can not read next file");                unzClose(zipfile);                return false;            }        }    }    unzClose(zipfile);    CCLOG("end uncompressing");     return true;}

方法是导出ZipUtils,在ZipUtils里面添加uncompressDir的方法给lua用


0 0
原创粉丝点击