lua 解压缩文件zlib

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用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值