我创建了以下类别方法来NSFileManager
使用ZipKit
.
- (BOOL)zipContentsOfDirectoryAtPath:(NSString *)directory toPath:(NSString *)filename recursive:(BOOL)recursive {
// If there is already a file at the destination, delete it
if ([self fileExistsAtPath:filename]) {
[self removeItemAtPath:filename error:nil];
}
@try {
ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:filename];
NSInteger result = [archive deflateDirectory:directory relativeToPath:directory usingResourceFork:NO];
return result == zkSucceeded;
}
@catch (NSException *exception) {
if ([self fileExistsAtPath:filename]) {
[self removeItemAtPath:filename error:nil];
}
}
return NO;
}
该directory
参数是您希望压缩的目录(及其内容)的路径。该filename
参数是您想要的结果 zip 文件的路径。