项目概述:
想在帅哥日语app中的文件列表界面新增解压功能,
经测试验证发现, 上一篇文章中的SSZipArchive代码 移植到正式项目中时会出现莫名奇妙的错误,
因此,另换一种实现思路,使用objective-zip进行解压
具体使用步骤:
1.从github上将objective-zip下载后,拖动到项目里,如图所示
编码
2. 控制器代码如下所示
//
// ViewController.m
// tempOCZIP
//
// Created by beyond on 2010/06/07.
// Copyright © 2010年 beyond. All rights reserved.
//
#import "ViewController.h"
#import "Objective-Zip.h"
@interface ViewController ()
@end
@implementation ViewController
static int BUFFER_SIZE = 1024;
- (void)viewDidLoad {
[super viewDidLoad];
[self startUnZip];
}
- (void)startUnZip
{
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"vwhm.net.zip" ofType:nil];
NSString *unzipPath = [self tempUnzipPathInDoc];
NSLog(@"sg__%@",unzipPath);
[self decompressFileFromPath:filePath toPath:unzipPath];
}
- (void)decompressFileFromPath:(NSString *)from toPath:(NSString *)to{
@try {
OZZipFile *unzipFile = [[OZZipFile alloc] initWithFileName:from mode:OZZipFileModeUnzip];
//解压是否完成
BOOL unzipFinished = NO;
while (!unzipFinished) {
//获取当前遍历到的文件信息
OZFileInZipInfo *info = [unzipFile getCurrentFileInZipInfo];
OZZipReadStream *stream = [unzipFile readCurrentFileInZip];
NSMutableData *buffer = [[NSMutableData alloc] initWithLength:BUFFER_SIZE];
// unzip files to the write path
NSString *writePath = [to stringByAppendingPathComponent:info.name];
if ([info.name hasSuffix:@"/"]) {
//创建目录
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:writePath withIntermediateDirectories:YES attributes:nil error:nil];
// [MFFileToolkit createDrectoryIfNeeded:writePath];
}else{
//创建文件
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:writePath contents:nil attributes:nil];
// [MFFileToolkit createFilePath:writePath];
//create fileHanderler to manage writing data to specified path, before writing data, move
//the cusor to the end of the file first.
NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:writePath];
[buffer setLength:0];
do {
[buffer setLength:BUFFER_SIZE];
int bytesRead = [stream readDataWithBuffer:buffer];
//每次读取BUFFER_SIZE大小的数据,如果读出的数据大小>0,就继续循环读取数据,
//直到读到的数据大小<= 0时,退出循环,当前遍历的文件已解压完毕
if (bytesRead > 0) {
[buffer setLength:bytesRead];
[fileHandler seekToEndOfFile];
[fileHandler writeData:buffer];
}else{
break;
}
} while (YES);
[fileHandler closeFile];
}
[stream finishedReading];
buffer = nil;
// Check if we should continue reading
unzipFinished = ![unzipFile goToNextFileInZip];
}
}
@catch (OZZipException *exception) {
@throw exception;
}
}
- (NSString *)tempUnzipPathInDoc
{
NSString *path = [NSString stringWithFormat:@"%@/\%@",
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0],
[NSUUID UUID].UUIDString];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtURL:url
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error) {
return nil;
}
return url.path;
}
@end