2008-11-13 58 views
11

我正在开发一个iOS应用程序,并试图压缩我在应用程序中创建的文件,是否有任何内置函数能够做到这一点?如何使用Objective C创建zip文件?

+0

你需要什么?只是压缩并发送文件的地方?如果是这样,您可以使用已包含的zlib。 – 2008-11-13 08:32:45

+0

请检查您在[这里]答案[1] [1]:http://stackoverflow.com/questions/7386695/how-to-zip-file/18740716#18740716 – Nirmalsinh 2013-09-11 12:09:27

回答

9

亚历克斯指出,我通过指出NSData category贡献的Cocoadev维基的用户回应this question。该类别包括处理NSData实例中的压缩和压缩数据的方法(可以从Zip文件读取或写入一个)。这应该是您实现所描述的压缩文件所需的一切,只要您可以将文件数据提供给NSData实例即可。

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

+4

到NSData类别的链接已中断。 – 2013-12-08 12:31:44

+1

@GlennHowes - 它可以在这里的网页存档:http://web.archive.org/web/20100102154917/http://cocoadev.com/index.pl?NSDataCategory,但我编辑我的旧问题,包括整体的类别在这里保存它。 – 2013-12-08 23:43:50

0

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

RWendi

+0

我做它。不是火箭科学,而是一大堆枯燥乏味的东西。 – 2012-12-19 21:29:41

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 我希望这是帮助

12

我绝对推荐的Objective-邮编。这只是最近搬到https://github.com/flyingdolphinstudio/Objective-Zip

创建一个ZIP文件:

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

将文件添加到压缩文件:

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];