这是一个直接操作文件的ObjC库,用于打包/解包以及压缩/解压缩。它不是一个在NSData
上的分类实现(与GZIP或Godzippa不同),所以不需要首先将整个文件加载到内存中。
名为tar的实现基于Light-Untar-for-iOS,但扩展了通过NSProgress
进行进度报告的功能。
[[NVHTarGzip shared] unGzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* gzipError) {
if (gzipError != nil) {
NSLog(@"Error ungzipping %@", gzipError);
}
}];
[[NVHTarGzip shared] unTarFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* tarError) {
if (tarError != nil) {
NSLog(@"Error untarring %@", tarError);
}
}];
[[NVHTarGzip shared] unTarGzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error extracting %@", error);
}
}];
[[NVHTarGzip shared] gzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* gzipError) {
if (gzipError != nil) {
NSLog(@"Error gzipping %@", gzipError);
}
}];
[[NVHTarGzip shared] tarFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* tarError) {
if (tarError != nil) {
NSLog(@"Error tarring %@", tarError);
}
}];
[[NVHTarGzip shared] tarGzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error packing %@", error);
}
}];
[[NVHTarGzip shared] unGzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* gzipError) {
if (gzipError != nil) {
NSLog(@"Error ungzipping %@", gzipError);
}
}];
[[NVHTarGzip shared] unTarFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* tarError) {
if (tarError != nil) {
NSLog(@"Error untarring %@", tarError);
}
}];
[[NVHTarGzip shared] unTarGzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error extracting %@", error);
}
}];
[[NVHTarGzip shared] gzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* gzipError) {
if (gzipError != nil) {
NSLog(@"Error gzipping %@", gzipError);
}
}];
[[NVHTarGzip shared] tarFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* tarError) {
if (tarError != nil) {
NSLog(@"Error untarring %@", tarError);
}
}];
[[NVHTarGzip shared] tarGzipFileAtPath:sourcePath toPath:destinationPath completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error extracting %@", error);
}
}];
连续的tar.gz
打包和解包将将中间的tar
文件tar或ungzip到临时目录中,随后/gzip或untar它。在gzip/untar后,将删除临时文件。您可以通过在提取前设置单例对象来自定义cachePath。
[[NVHTarGzip shared] setCachePath:customCachePath];
NVHTarGzip
使用 NSProgress
来处理进度报告。为了跟踪进度,请创建自己的进度实例并使用 KVO 来检查 fractionCompleted
属性。请参阅 NSProgress 的文档 和 Ole Begemann 的这篇出色的文章 这篇优秀的文章 以获取更多信息。
NSProgress* progress = [NSProgress progressWithTotalUnitCount:1];
NSString* keyPath = NSStringFromSelector(@selector(fractionCompleted));
[progress addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionInitial context:NVHProgressFractionCompletedObserverContext];
[progress becomeCurrentWithPendingUnitCount:1];
[[NVHTarGzip shared] unTarGzipFileAtPath:self.demoSourceFilePath toPath:self.demoDestinationFilePath completion:^(NSError* error) {
[progress resignCurrent];
[progress removeObserver:self forKeyPath:keyPath];
}];
在示例项目中查看完整的用法示例;首先克隆仓库,然后从 Example 目录运行 pod install
。
添加流支持(《NSStream》)。这将允许在打包和解包 tar.gz
时使用中间文件,从而加快速度。
欢迎提交拉取请求!
NVHTarGzip 可通过 CocoaPods 获取,安装它只需将以下行添加到您的 Podfile
文件中
pod "NVHTarGzip"
Niels van Hoorn, [email protected]
NVHTarGzip 在 MIT 许可 下可用。有关更多信息,请参阅 LICENSE
文件。