开发者

How to zip file

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

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开发者_Go百科.fileManager removeItemAtPath:filePath error:NULL];
  }
  else
  {
     NSLog(@"Failure to unzip archive");
  }
}
else
{
   NSLog(@"Failure to open archive");
}
[zipArchive release];


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


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

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

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


you will get your answer is here . a c based library you will get by which you can zip and unzip your files programmatically .http://stackoverflow.com/questions/1546541/how-can-we-unzip-a-file-in-objective-c


This code will work perfectly to zip a file.

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

            }
          }


I have used this to zip my documents folder.
You can separate out the part where you want to zip a file.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜