从文档目录中的文件夹创建ZIP文件 - 目标C(ARC)

问题描述:

我有一个使用ARC开发的iPhone应用程序。我有一个文件夹,其中包含我的文档目录中的一堆图像,我需要将它们压缩起来并发送电子邮件。我的项目使用ARC。从文档目录中的文件夹创建ZIP文件 - 目标C(ARC)

有没有人有任何示例代码/链接到资源,这将有助于我?

我一直在网上搜索,我发现与ARC不兼容 - 即使它声称是。

+6

你知道你可以关闭ARC文件的基础上,正确的?所以如果你有非ARC代码,你仍然可以在ARC项目中使用它。 ARC不是全有或全无;它是一个每文件编译器选项。 http://*.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

从此链接下载并拖动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