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.fileManager removeItemAtPath:filePath error:NULL];
}
else
{
NSLog(@"Failure to unzip archive");
}
}
else
{
NSLog(@"Failure to open archive");
}
[zipArchive release];
I use SSZipArchive
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
I have used this to zip my documents folder.
You can separate out the part where you want to zip a file.
This code will work perfectly to zip a file.
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: