【问题标题】:How can I create a zip file by using Objective C?如何使用 Objective C 创建一个 zip 文件?
【发布时间】:2010-09-22 03:18:57
【问题描述】:

我正在开发一个 iOS 应用程序,并尝试压缩我在应用程序中创建的文件,是否有任何内置函数可以做到这一点?

【问题讨论】:

标签: objective-c ios cocoa-touch


【解决方案1】:

我绝对推荐 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]关闭文件;
  • 我成功使用了这个库,尽管它说它已被弃用
【解决方案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 压缩到库文件夹中 我希望这会有所帮助

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 到 NSData 类别的链接已损坏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多