DJZipArchive 不能加压文件问题

我改完了复制过去替换原来的就能用了

.h

//
//  DJZipArchive.h
//  TestUI
//
//  Created by 杜甲 on 14-5-3.
//
//

#ifndef __TestUI__DJZipArchive__
#define __TestUI__DJZipArchive__

#include "cocos2d.h"
#include "unzip/unzip.h"
#include "cocos-ext.h"

USING_NS_CC;


class DJZipArchive
{
    
public:
    DJZipArchive();
    ~DJZipArchive();
    
    bool unzipOpenFile(const char* zipFile);
    
    bool unzipFileToAndOverWrite(std::string path,bool overwrite);
    
    bool createDirectory(const char *path);
    
private:
    unzFile _unzFile;
    std::string _password;
};

#endif /* defined(__TestUI__DJZipArchive__) */

.cpp

//
//  DJZipArchive.cpp
//  TestUI
//
//  Created by 杜甲 on 14-5-3.
//
//

#include "DJZipArchive.h"
#include "stdio.h"
#include "sys/stat.h"

DJZipArchive::DJZipArchive()
{
    _unzFile=  nullptr;
}

bool DJZipArchive::unzipOpenFile(const char *zipFile)
{
    _unzFile = cocos2d::unzOpen(zipFile);
    
    if (_unzFile)
    {
        unz_global_info globalInfo = {0};
        
        if (unzGetGlobalInfo(_unzFile, &globalInfo) == UNZ_OK)
        {
            log("%lu  entries in the zip file",globalInfo.number_entry);
        }
    }
    return _unzFile != nullptr;
}

bool DJZipArchive::unzipFileToAndOverWrite(std::string path, bool overwrite)
{
    bool success = true;
    int ret = unzGoToFirstFile(_unzFile);
    
    unsigned char buffer[4096] = {0};
    
    do {
        if (_password.size() == 0)
            ret = unzOpenCurrentFile(_unzFile);
        else
            ret = unzOpenCurrentFilePassword(_unzFile, _password.c_str());
        
        if (ret != UNZ_OK)
        {
            
            success = false;
            break;
        }
        
        // reading data and write to file
        int read ;
        unz_file_info fileInfo = {0};
        ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, nullptr, 0, nullptr, 0, nullptr, 0);
        if (ret != UNZ_OK) {
            success = false;
            unzCloseCurrentFile(_unzFile);
        }
        
        char* filename = (char*)malloc(fileInfo.size_filename + 1);
        unzGetCurrentFileInfo(_unzFile, &fileInfo, filename, fileInfo.size_filename + 1, nullptr, 0, nullptr, 0);
        filename[fileInfo.size_filename] = '\0';
        log("filename = %s",filename);
        
        bool isDirectory = false;
        if (filename[fileInfo.size_filename - 1] == '/' || filename[fileInfo.size_filename - 1] == '\\')
            isDirectory = true;
        
        free(filename);
        
        
        std::string fullPath = path + StringUtils::format("%s",filename);
        log("%s",path.c_str());

        if (isDirectory) {
            this->createDirectory(fullPath.c_str());
        }

        FILE* fp = fopen(fullPath.c_str(), "wb");
        
        while (fp)
        {

            read = unzReadCurrentFile(_unzFile, buffer, 4096);
            if (read > 0) {
                fwrite(buffer, read, 1, fp);
            }
            else if (read < 0)
            {
                break;
            }
            else
                break;
            
        }
        
        if (fp)
        {
            fclose(fp);
        }
        
        unzCloseCurrentFile(_unzFile);
        ret = unzGoToNextFile(_unzFile);
        
        
        
    }
    while (ret == UNZ_OK && UNZ_OK != UNZ_END_OF_LIST_OF_FILE);
    return success;
}


//创建子目录
bool DJZipArchive::createDirectory(const char *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    mode_t processMask = umask(0);
    int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
    umask(processMask);
    if (ret != 0 && (errno != EEXIST))
    {
        return false;
    }
    
    return true;
#else
    BOOL ret = CreateDirectoryA(path, NULL);
	if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
	{
		return false;
	}
    return true;
#endif
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值