2011-09-12 52 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下載源代碼並添加到您的應用程序文件夾中。

然後在你的頭文件中加入:#進口 「ZipArchive/ZipArchive.h」

要創建一個zip文件很簡單,只需使用下面的代碼:

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

     } 
     [zip release]; 
0

你會得到你的答案就在這裏。基於ac的圖書館,你將通過它可以編程壓縮和解壓縮你的文件 .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

我覺得作爲SSZipArchive 2014不再適用於當前的iOS 7和7.1 SDK。在2年以上沒有看到更新。 –

+0

我認爲它只是感動,全是https://github.com/soffes/ssziparchive –

+0

謝謝@CodeMonkey,我已經更新了鏈接:-) – 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];