使用objective-c压缩文件

时间:2015-04-27 14:04:50

标签: ios objective-c

我正在使用Objective-Zip创建一个zip文件,但它不起作用,Xcode表示有许多弃用和问题。第一件事是导入文件夹中的所有类:

  

ARCHelper,ZLib,MiniZip,Objective-Zip

对于我的项目,现在我需要创建一个zip文件,为此我使用此代码:

#import "ZipFile.h"
#import "ZipWriteStream.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString* str = @"teststring";
    NSData* abcData = [str dataUsingEncoding:NSUTF8StringEncoding];

    ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip"
                                                   mode:ZipFileModeCreate];

    ZipWriteStream *stream = [zipFile writeFileInZipWithName:@"abc.txt"
                                           compressionLevel:ZipCompressionLevelBest];

    [stream writeData:abcData];
    [stream finishedWriting];

    [zipFile close];   
}

当我构建并运行此代码时,我收到消息:

  

***因未捕获的异常“ZipException”而终止应用,原因:'无法打开'test.zip''

首先,我创建了一个zip文件,而不是试图打开他(因此控制台中的消息是错误的是'无法创建test.zip'而不是'打开')。

知道正在给出的错误,我相信这类objective-zip已经很老了,我想知道是否还有一个比这更好的类我可以创建zip文件(并设置这些文件的密码)。 / p>

2 个答案:

答案 0 :(得分:2)

我建议您使用以下库:  https://github.com/pixelglow/zipzap

使用,只需导入标题文件:

#import <zipzap/zipzap.h>

阅读现有的zip文件:

ZZArchive* oldArchive = [ZZArchive archiveWithURL:[NSURL fileURLWithPath:@"/tmp/old.zip"]
                                        error:nil];
ZZArchiveEntry* firstArchiveEntry = oldArchive.entries[0];
NSLog(@"The first entry's uncompressed size is %lu bytes.", (unsigned long)firstArchiveEntry.uncompressedSize);
NSLog(@"The first entry's data is: %@.", [firstArchiveEntry newDataWithError:nil]);

答案 1 :(得分:1)

尝试使用开源版本。在这个答案中提到了一个。

How to unzip a .zip file on iOS?

打包的引用是SSZipArchive:https://github.com/soffes/ssziparchive