首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何压缩文件

如何压缩文件
EN

Stack Overflow用户
提问于 2011-09-12 18:48:41
回答 5查看 3.6K关注 0票数 2

我想在我的应用程序中压缩文件。有谁能告诉我确切的密码。我已经使用这个代码解压了文件:

我使用的是: ZipArchive.h

代码语言:javascript
复制
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];
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-09-11 20:05:37

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

然后在头文件中添加:#import "ZipArchive/ZipArchive.h“

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

代码语言:javascript
复制
BOOL ret = [zip CreateZipFile2:l_zipfile];
ret = [zip addFileToZip:l_photo newname:objectForZip];
      if( ![zip CloseZipFile2] )
      {

      }
      [zip release];
票数 0
EN

Stack Overflow用户

发布于 2011-09-12 18:53:55

我使用SSZipArchive

代码语言:javascript
复制
NSError* error = nil;
BOOL ok = [SSZipArchive unzipFileAtPath:source_path toDestination:dest_path overwrite:YES password:nil error:&error];
DLog(@"unzip status: %i %@", (int)ok, error);
if(ok) {
    [self performSelectorOnMainThread:@selector(unzipCompleted) withObject:nil waitUntilDone:NO];
} else {
    [self performSelectorOnMainThread:@selector(unzipFailed:) withObject:error waitUntilDone:YES];
}
票数 2
EN

Stack Overflow用户

发布于 2011-09-12 18:52:47

你会在这里得到你的答案。这是一个基于c语言的库,您可以通过它以编程方式压缩和解压缩文件。http://stackoverflow.com/questions/1546541/how-can-we-unzip-a-file-in-objective-c

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7386695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档