简体   繁体   English

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

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

I have an iPhone application that I have developed using ARC. 我有一个使用ARC开发的iPhone应用程序。 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. 我的项目使用ARC。

Does anyone have any example code/links to a resource that would be helpful to me? 有没有人有任何示例代码/链接到一个对我有帮助的资源?

I've been raking around online and what I can find is not compatible with ARC - even when it claims to be. 我一直在网上闲逛,我能找到的东西与ARC不兼容 - 即使它声称是。

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

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

相关问题 从文档文件夹-目标C中存储的文件进行解析后,在TableView中显示内容 - Display Contents In TableView After Parsing From a File stored in Documents Folder - Objective C MPMoviePlayerViewController播放来自Documents目录的电影 - objective-c - MPMoviePlayerViewController play movie from Documents directory - objective-c 从应用程序文档目录复制文件,然后移至iPhone / Application文件夹-目标C? - Copy file from Application document directory and move to iPhone /Application folder- Objective C? 目标C - 如何从iPhone /应用程序/文档访问文件 - Objective C - how to access a file from iPhone/Application/Documents ARC和Objective C子类化 - ARC and Objective C Subclassing 将图像从项目资源复制到Objective-C中的documents文件夹时出现问题 - Problem when copying image from project resources to documents folder in Objective-C 如何使用Objective-C从保管箱删除文件或文件夹 - how to delete file or Folder from the dropbox using Objective-C 如何在目标c中从文件夹中读取文件而不给出文件名 - how to read a file from folder without giving filename , in objective c Objective-C:无法将图像写入文档目录 - objective-c: failed to write image to documents directory 使用目标C在iPhone中创建MS Word文档 - Creating MS Word Documents in iPhone using objective C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM