如何使用Objective C创建zip文件?

12

我正在开发一款iOS应用程序,想要压缩我在应用程序中创建的文件,有没有内置函数可以实现这个功能?


你需要这个是干什么的?只是压缩并发送文件到某个地方吗?如果是这样,你可以使用已经包含的zlib。 - Jason Coco
请在这里检查您的答案。 - Nirmalsinh Rathod
3个回答

13

我强烈推荐使用Objective-Zip。它最近刚迁移到https://github.com/flyingdolphinstudio/Objective-Zip:

以下是其文档中的一些示例:

创建Zip文件:

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

将文件添加到zip文件中:

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

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

从zip文件中读取文件:

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];

[unzipFile goToFirstFileInZip];

ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];

[read finishedReading];

记得通过 [zipfile close] 关闭文件。 - Tunvir Rahman Tusher
我成功地使用了这个库,尽管它说它已经过时了。 - JAnton

9
如Alex所指出的那样,我回答了这个问题,并指出了Cocoadev wiki用户贡献的NSData类别。该类别包括用于处理NSData实例中的压缩和gzipped数据的方法(可以从Zip文件中读取或写入)。只要您能将文件数据输入NSData实例,这应该就是您实现所描述的文件压缩所需的全部内容。
有关此类别实际应用的示例,请参见我的iPhone应用程序Molecules的源代码。我仅使用该方法从gzipped文件(在SLSMolecule+PDB.m中)提取数据,但您应该能够从中获得基本概念。

4
NSData类别的链接已经失效。 - Glenn Howes
1
@GlennHowes - 这里可以在Web存档中找到:http://web.archive.org/web/20100102154917/http://cocoadev.com/index.pl?NSDataCategory,但我编辑了我的旧问题,以包含整个类别,以便在此处保留它。 - Brad Larson

2

首先,您需要从http://code.google.com/p/objective-zip/downloads/list下载Objective-zip示例。

在此示例中,找到并复制三个文件夹Objective-Zip、MiniZip和ZLib,然后将它们拖入您的项目中。

在您的.m类中导入两个类:"ZipFile.h"和"ZipWriteStream.h"

创建压缩方法,我的代码如下:

-(IBAction)Zip{
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);

NSString *ZipLibrary = [paths objectAtIndex:0];


NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:@"backUp.zip"];

//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil];

//self.documentsDir = [paths objectAtIndex:0];


ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate];

NSError *error = nil;
self.fileManager = [NSFileManager defaultManager];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);

self.documentsDir = [paths1 objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error];
//for(NSString *filename in files){
    for(int i = 0;i<files.count;i++){

        id myArrayElement = [files  objectAtIndex:i];


    if([myArrayElement rangeOfString:@".png" ].location !=NSNotFound){
            NSLog(@"add %@", myArrayElement);


    NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
    NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
    NSDate *Date = [attributes objectForKey:NSFileCreationDate];

    ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
    NSData *data = [NSData dataWithContentsOfFile:path];
    //  NSLog(@"%d",data);
    [streem writeData:data];
    [streem finishedWriting];
        }else if([myArrayElement rangeOfString:@".txt" ].location !=NSNotFound)
        {

            NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
            NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
            NSDate *Date = [attributes objectForKey:NSFileCreationDate];

            ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
            NSData *data = [NSData dataWithContentsOfFile:path];
            //  NSLog(@"%d",data);
            [streem writeData:data];
            [streem finishedWriting];
    }
}

[self testcsv];
[zipFile close];

}

您的文档目录中保存了一些 .png 和 .txt 文件,现在将它们压缩成 backup.zip 并存放在 Library 文件夹中,希望这可以帮到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接