Objective c 读取Zip文件会导致iphone中目标Zip库出错

Objective c 读取Zip文件会导致iphone中目标Zip库出错,objective-c,iphone,Objective C,Iphone,unzOpen()函数在使用目标zip库打开zip文件时给出错误“Cann't open” 我尝试了以下操作:-其中路径是具有读/写过量的zip文件的有效路径 - (id) initWithFileName:(NSString *)fileName mode:(ZipFileMode)mode { if (self= [super init]) { _fileName= [fileName retain]; _mode= mode;

unzOpen()函数在使用目标zip库打开zip文件时给出错误“Cann't open”

我尝试了以下操作:-其中路径是具有读/写过量的zip文件的有效路径

- (id) initWithFileName:(NSString *)fileName mode:(ZipFileMode)mode 
{
    if (self= [super init])
 {
        _fileName= [fileName retain];
        _mode= mode;

        switch (mode) {
            case ZipFileModeUnzip:

                _unzFile = unzOpen( (const char*)[_fileName UTF8String] );
                if (_unzFile == NULL) {
                    NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName];
                    @throw [[[ZipException alloc] initWithReason:reason] autorelease];
                }
                break;

            case ZipFileModeCreate:
                _zipFile= zipOpen([_fileName cStringUsingEncoding:NSUTF8StringEncoding], APPEND_STATUS_CREATE);
                if (_zipFile == NULL) {
                    NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName];
                    @throw [[[ZipException alloc] initWithReason:reason] autorelease];
                }
                break;

            case ZipFileModeAppend:
                _zipFile= zipOpen([_fileName cStringUsingEncoding:NSUTF8StringEncoding], APPEND_STATUS_ADDINZIP);
                if (_zipFile == NULL) {
                    NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName];
                    @throw [[[ZipException alloc] initWithReason:reason] autorelease];
                }
                break;

            default: {
                NSString *reason= [NSString stringWithFormat:@"Unknown mode %d", _mode];
                @throw [[[ZipException alloc] initWithReason:reason] autorelease];
            }
        }
    }

    return self;
}
无法打开该文件时会出现错误


其中模式为ZipFileModeUnzip

我在我的一个应用程序中遇到了相同的问题,经过长时间的研究,我花了几天时间才知道问题出在服务器端,zip文件在服务器端没有正确存档。所以我建议您首先检查zip文件是否正确。首先尝试从Mac解压本地zip文件,并确保zip文件格式已正确存档


希望这能有所帮助。

我知道这个话题已经超过3年了,但也许我的答案对其他人有用

我也有同样的问题。 问题出在iOS 7.1上的64位体系结构上

首先,检查您是否有1.1版本的MiniZip。 不要用adler32.c、compress.c复制Zlib目录。。等等文件。 改用Xcode libs.1.2.5.dylib提供的libs

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:zipFilePath mode:ZipFileModeUnzip];




[unzipFile locateFileInZip:fileName];
FileInZipInfo *fileInfo = [unzipFile getCurrentFileInZipInfo];

ZipReadStream *read;

if(password){

    read = [unzipFile readCurrentFileInZipWithPassword:password];
} else {

    read = [unzipFile readCurrentFileInZip];
}

NSMutableData *data= [[NSMutableData alloc] initWithLength:[fileInfo length]];

[read readDataWithBuffer:data];
[read finishedReading];
[unzipFile close];

谢谢你的回复。我已经检查了zip文件是否正确存档我成功地压缩并解压了数据库文件,没有程序。你对zip文件有问题吗?嘿,那里-你从哪里得到了1.1的Minizip,你如何使用libs“由Xcode提供”?Minizip 1.1如何添加libs.1.2.5.dylib你的项目?构建阶段->将二进制文件链接到库->+->libz.1.2.5.dylib