我想在我的应用程序中压缩文件。任何人都可以准确地告诉我代码。 我已使用此代码解压缩文件:
我用过: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 :(得分: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];
}
答案 1 :(得分:0)
你会得到你的回答。您可以通过编程方式压缩和解压缩文件 .http://stackoverflow.com/questions/1546541/how-can-we-unzip-a-file-in-objective-c
答案 2 :(得分: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");
}
}
答案 3 :(得分: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];
答案 4 :(得分:0)
您可以使用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];