如何压缩文件 [英] How to zip file

查看:118
本文介绍了如何压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中压缩文件。任何人都可以准确地告诉我代码。
我使用此代码解压缩文件:

I want to zip file in my application. Can anybody tell me the code exactly. I have used this code to unzip file:

我使用过:ZipArchive.h

I used: ZipArchive.h

self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"document directory path:%@",paths);

self.documentDirectory = [paths objectAtIndex:0];

NSString *filePath = [NSString stringWithFormat:@"%@/abc", self.documentDirectory];

NSLog(@"file path is:%@",filePath);

NSString *fileContent = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.zip"];


NSData *unzipData = [NSData dataWithContentsOfFile:fileContent];

[self.fileManager createFileAtPath:filePath contents:unzipData attributes:nil];

// here we go, unzipping code

ZipArchive *zipArchive = [[ZipArchive alloc] init];

if ([zipArchive UnzipOpenFile:filePath])
{
  if ([zipArchive UnzipFileTo:self.documentDirectory overWrite:NO])
  {
     NSLog(@"Archive unzip success");
     [self.fileManager removeItemAtPath:filePath error:NULL];
  }
  else
  {
     NSLog(@"Failure to unzip archive");
  }
}
else
{
   NSLog(@"Failure to open archive");
}
[zipArchive release];


推荐答案

您可以使用zipArchive创建zip文件。 ZipArchive 下载源代码代码并添加到您的应用程序文件夹中。

You can create zip file using zipArchive. ZipArchive Download the source code and add into your application folder.

然后在您的头文件中添加:#importZipArchive / ZipArchive.h

Then in your header file add: #import "ZipArchive/ZipArchive.h"

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

To create a zip file is simple, just use the following code:

BOOL ret = [zip CreateZipFile2:l_zipfile];
ret = [zip addFileToZip:l_photo newname:objectForZip];
      if( ![zip CloseZipFile2] )
      {

      }
      [zip release];

这篇关于如何压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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