2012-07-25 133 views
5

我有一個使用ARC開發的iPhone應用程序。我有一個文件夾,其中包含我的文檔目錄中的一堆圖像,我需要將它們壓縮起來併發送電子郵件。我的項目使用ARC。從文檔目錄中的文件夾創建ZIP文件 - 目標C(ARC)

有沒有人有任何示例代碼/鏈接到資源,這將有助於我?

我一直在網上搜索,我發現與ARC不兼容 - 即使它聲稱是。

+6

你知道你可以關閉ARC文件的基礎上,正確的?所以如果你有非ARC代碼,你仍然可以在ARC項目中使用它。 ARC不是全有或全無;它是一個每文件編譯器選項。 http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – 2012-07-25 12:17:26

+0

並進一步澄清,如果你有一個Xcode項目,建立這樣的一個Objective-C庫,而不是ARC,您可以將該項目包含在您的項目中並使用該庫,無論其是否使用ARC。 – 2012-07-25 13:00:43

+0

哦,不,我沒有意識到這一點,歡呼聲。 – 2012-07-25 13:03:45

回答

8

從此鏈接下載並拖動Objective-Zip,MiniZip和ZLib到您的項目中http://code.google.com/p/objective-zip/downloads/list(Objective-zip)。導入文件: ZipFile.h, ZipException.h, FileInZipInfo.h, ZipWriteStream.h, ZipReadStream.h, zlib.h

使用此代碼。請看下面:

NSString *stringPath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]]; 
    NSString *FileName=[stringPath1 stringByAppendingPathComponent:@"Your file name"]; 


    NSString *stringPath=[stringPath1 stringByAppendingPathComponent:[@"Your file name" stringByAppendingFormat:@".zip"]]; 
    NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:FileName error:&error]; 
    ZipFile *zipFile = [[ZipFile alloc]initWithFileName:stringPath mode:ZipFileModeCreate]; 

    for(int i = 0;i<files.count;i++){ 

     id myArrayElement = [files objectAtIndex:i]; 
     NSLog(@"add %@", myArrayElement); 

     NSString *path = [FileName 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]; 
     [streem writeData:data]; 
     [streem finishedWriting]; 
    } 

    [zipFile close]; 
+0

抱歉,您提供的鏈接現在已經過時。 – mcy 2013-09-26 13:28:19

+0

對不起,但那是一年多前... – 2013-09-27 05:56:51

+2

找到這個項目的工作鏈接:https://github.com/flyingdolphinstudio/Objective-Zip – 2013-10-21 05:08:49