2011-09-12 17 views
2

私のアプリケーションでファイルを圧縮したいと思います。誰も私にコードを正確に教えてもらえますか? 私は、ファイルを解凍するために、このコードを使用していますファイルを開く方法

私が使用: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]; 

答えて

0

zipArchiveを使用してzipファイルを作成できます。 ZipArchiveソースコードをダウンロードし、アプリケーションフォルダに追加します。

その後、あなたのヘッダファイルに追加します:zipファイルを作成するには#import "ZIPARCHIVE/ZipArchive.h"

は簡単です、ちょうど次のコードを使用し

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

     } 
     [zip release]; 
0

を、あなたの答えはここにあるでしょう。あなたはあなたのファイルをプログラムで解凍して解凍することができます。 .http://stackoverflow.com/questions/1546541/how-can-we-unzip-a-file-in-objective-c

2

私は、ファイルを圧縮するために完璧に動作します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]; 
} 
+0

を私は2014 SSZipArchiveのようだと思います現在のiOS 7および7.1 SDKでは動作しません。 2年以上の更新は見られません。 –

+0

私はちょうど移動したと思う、すべてhttps://github.com/soffes/ssziparchive –

+0

ありがとう@コードモンキー、私はリンクを更新しました:-) – neoneye

0

このコードを使用します。

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"); 

      } 
      } 
0

私は、マイドキュメントフォルダを圧縮するために、これを使用しています。
ファイルを圧縮したい部分を分けることができます。

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]; 
関連する問題