2011-11-16 48 views
8

在我的申请,我以图像查看的屏幕截图,然后我保存在application.Now的文件夹的屏幕截图我想通过电子邮件发送相同文件夹结构,所有这些图像他们in.Zipping都包含文件夹图像,然后将zip文件附加到邮件将解决问题,但我如何压缩这些文件夹,然后将它们附加到邮件?如何在iPhone SDK中压缩文件夹?

任何帮助表示赞赏!

回答

18

我已经使用这个代码来创建文件的zip文件我的应用程序的目录,它的工作

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docDirectory = [paths objectAtIndex:0]; 
BOOL isDir=NO; 
NSArray *subpaths; 
NSString *exportPath = docDirectory; 
NSFileManager *fileManager = [NSFileManager defaultManager];  
if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){ 
    subpaths = [fileManager subpathsAtPath:exportPath]; 
} 

NSString *archivePath = [docDirectory stringByAppendingString:@"/test.zip"]; 

ZipArchive *archiver = [[ZipArchive alloc] init]; 
[archiver CreateZipFile2:archivePath]; 
for(NSString *path in subpaths) 
{ 
    NSString *longPath = [exportPath stringByAppendingPathComponent:path]; 
    if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir) 
    { 
     [archiver addFileToZip:longPath newname:path];  
    } 
} 

if([archiver CloseZipFile2]) 
    NSLog(@"Success"); 
else 
    NSLog(@"Fail"); 
+0

谢谢..它的工作:) – Yogi

+0

瑜伽士我面临着同样的情况。你可以分享你的代码 – thavasidurai

+0

我认为他已经使用了上述代码@thavasidurai – Robin

1

我用ZipArchive在过去的成功。

这是相当ligyweight和使用简单,支持密码保护,ZIP中的多个文件,以及压缩&解压缩。

的基本用法是:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"]; 
ZipArchive *zipArchive = [[ZipArchive alloc] init]; 
[zipArchive UnzipOpenFile:filepath Password:@"xxxxxx"]; 
[zipArchive UnzipFileTo:{pathToDirectory} overWrite:YES]; 
[zipArchive UnzipCloseFile]; 
[zipArchive release]; 

这是解压文件夹/文件。压缩文件夹同样容易。压缩文件(或fodler)

  BOOL ret = [zip CreateZipFile2:l_zipfile]; 
      // OR 
      BOOL ret = [zip CreateZipFile2:l_zipfile Password:@"your password"]; // 
      //if the Password is empty, will get the same effect as [zip CreateZipFile2:l_zipfile]; 

      ret = [zip addFileToZip:l_photo newname:@"photo.jpg"]; 
      if(![zip CloseZipFile2]) 
      { 
        // error handler here 
      } 
      [zip release]; 

我也听说过ObjectiveC-Zip也。

+0

谢谢您response.I了如何解压Zip文件的内容,我读了google.But的文档,我没有得到如何创建一个新的zip文件,并添加项目到它。 – Yogi

+0

@Yogi请检查我更新的答案。 –

+1

谢谢...我们可以在同一个zip文件中添加两个文件:我们称之为[zip addFileToZip:l_photo newname:@“photo.jpg”];对于多个文件?当我试图结束,只有一个文件在zip中。 – Yogi

2

要创建可以使用ZipArchive 下载源代码,并将其添加到您的项目,还可以添加libz.x.dylib一个zip文件(看看维基)。

在你的头文件

然后加:#import "ZipArchive/ZipArchive.h"

要创建一个zip文件很简单,只需使用下面的代码:

BOOL ret = [zip CreateZipFile2:l_zipfile]; 
// OR 
BOOL ret = [zip CreateZipFile2:l_zipfile Password:@"your password"]; // 
//if the Password is empty, will get the same effect as [zip CreateZipFile2:l_zipfile]; 
ret = [zip addFileToZip:l_photo newname:@"photo.jpg"]; 
     if(![zip CloseZipFile2]) 
     { 
     // error handler here 
     } 
     [zip release]; 
+0

它只形成所有文件包含在一个zip格式的文件,但它不会减小大小zip文件。我们如何缩小zip文件的大小?请指导。提前致谢。 – Nico

1

简易模式:添加ZipArchive到您的项目,则:

NSString* zip = ...; 
NSString* dir = ...; 
[SSZipArchive createZipFileAtPath: zip withContentsOfDirectory: dir];