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

时间:2008-11-13 08:14:35

标签: objective-c ios cocoa-touch

我正在开发一个iOS应用程序,并尝试压缩我在应用程序中创建的文件,是否有任何内置函数能够执行此操作?

4 个答案:

答案 0 :(得分:12)

我绝对推荐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];

答案 1 :(得分:9)

正如Alex指出的那样,我通过指出Cocoadev wiki用户提供的this question来回复NSData category。该类别包括在NSData实例中处理zipped和gzip数据的方法(可以从Zip文件读取或写入一个)。只要您可以将文件数据提供给NSData实例,这应该是实现您描述的文件压缩所需的全部内容。

有关此类别的示例,请参阅我的iPhone应用程序的源代码Molecules。我只使用该方法从gzip压缩文件中提取数据(在SLSMolecule + PDB.m中),但您应该能够从中获取基本概念。

答案 2 :(得分:2)

首先从http://code.google.com/p/objective-zip/downloads/list

下载Objective-zip示例
  

在此示例中查找并复制三个文件夹Objective-Zip,MiniZip和ZLib拖入您的项目

     

在你的.m类中导入两个类    “ZipFile.h”和   “ZipWriteStream.h”

     

创建zip方法我的代码是: -

-(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文件夹中 我希望这是有帮助的

答案 3 :(得分:0)

不确定是否有内置函数,但也许您可以使用InfoZip之类的外部库。或者您可以尝试实现自己的zip库,我很确定Zip格式规范可以在互联网上找到。

RWendi