【问题标题】:How to zip file如何压缩文件
【发布时间】:2011-09-12 10:48:40
【问题描述】:

我想在我的应用程序中压缩文件。谁能准确地告诉我代码。 我已使用此代码解压缩文件:

我用过: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];

【问题讨论】:

    标签: iphone


    【解决方案1】:

    我用SSZipArchive

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

    【讨论】:

    • 我认为截至 2014 年 SSZipArchive 不再适用于当前的 iOS 7 和 7.1 SDK。 2 年多没有更新了。
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        此代码可以完美地压缩文件。

            ZipArchive *zip = [[ZipArchive alloc] init];
            if(![zip UnzipOpenFile:fileToZipPath]) {
            //open file is there
        
                        if ([zip CreateZipFile2:newZipFilePath overWrite:YES]) {
        
                            //zipped successfully
        
                            NSLog(@"Archive zip Success");
        
                        } 
        
                    } else  {
        
                        NSLog(@"Failure To Zip Archive");
        
                    }
                  }
        

        【讨论】:

          【解决方案5】:

          我用它来压缩我的文档文件夹。
          您可以将要压缩文件的部分分开。

          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *docDirectory = [paths objectAtIndex:0];
          BOOL isDir = NO;
          NSArray *subpaths;
          NSFileManager *fileManager = [NSFileManager defaultManager];    
          if([fileManager fileExistsAtPath:docDirectory isDirectory:&isDir] && isDir)
          {
              subpaths = [fileManager subpathsAtPath:docDirectory];
          }
          NSString *archivePath = [docDirectory stringByAppendingString:@"/doc.zip"];
          ZipArchive *archiver = [[ZipArchive alloc] init];
          [archiver CreateZipFile2:archivePath];
          for(NSString *path in subpaths)
          {
              NSString *longPath = [docDirectory stringByAppendingPathComponent:path];
              if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
              {
                  [archiver addFileToZip:longPath newname:path];      
              }
          }
          BOOL successCompressing = [archiver CloseZipFile2];
          if(successCompressing)
          {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                              message:@"Zipping Successful!!!"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
              [alert show];
              [alert release];
          }
          else
          {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Cannot zip Docs Folder"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
              [alert show];
              [alert release];
          }
          [archiver release];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-09-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多