从文档目录中的文件夹创建ZIP文件 - Objective C(ARC) [英] Creating a ZIP file from a folder in documents directory - Objective C (ARC)

查看:185
本文介绍了从文档目录中的文件夹创建ZIP文件 - Objective C(ARC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an iPhone application that I have developed using ARC. I have a folder that contains a heap of images in my documents directory that I need to zip up and e-mail. My project uses ARC.

任何人都有任何示例代码/链接到资源,这对我有帮助吗?

Does anyone have any example code/links to a resource that would be helpful to me?

我一直在网上嘲笑,我可以找到与ARC不兼容,即使它声称是。

I've been raking around online and what I can find is not compatible with ARC - even when it claims to be.

推荐答案

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

Download and drag the Objective-Zip, MiniZip and ZLib drag in to your project from this link http://code.google.com/p/objective-zip/downloads/list (Objective-zip). import the files: ZipFile.h, ZipException.h, FileInZipInfo.h, ZipWriteStream.h, ZipReadStream.h, zlib.h

使用此代码。请参见下面:

Use this code. Please see below:

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

这篇关于从文档目录中的文件夹创建ZIP文件 - Objective C(ARC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆