2008-11-13 59 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];