繁体   English   中英

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

[英]Creating a ZIP file from a folder in documents directory - Objective C (ARC)

我有一个使用ARC开发的iPhone应用程序。 我有一个文件夹,其中包含我需要压缩和发送电子邮件的文档目录中的一堆图像。 我的项目使用ARC。

有没有人有任何示例代码/链接到一个对我有帮助的资源?

我一直在网上闲逛,我能找到的东西与ARC不兼容 - 即使它声称是。

通过此链接http://code.google.com/p/objective-zip/downloads/list(Object-zip )下载并拖动Objective-Zip,MiniZip和ZLib拖入您的项目。 导入文件: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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM