IOS_49_使用objective-zip解压文件
2018-01-29 10:11
507 查看
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:@"abc.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
运行效果如下:
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:@"abc.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
运行效果如下:
相关文章推荐
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- 使用 minizip 解压 ZIP 文件的代码例子
- 在mvc3中使用Telerik的upload控件上传Zip压缩文件,服务器端解压逐个上传到数据库,使用Ionic.Zip
- 使用apache的工具类进行zip(rar等压缩文件)的解压
- 使用PHP对文件进行压缩解压(zip)
- 使用InfoZip压缩解压zip文件
- 使用Objective-Zip解压Zip文件
- Qt使用QuaZIP 压缩/解压文件
- Win10如何使用自带压缩、解压zip压缩文件功能
- java使用Apache的ant压缩和解压文件(zip)
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- 使用zipfile解压一个压缩了N次的zip文件
- C#函数——使用SharpZipLib解压zip文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 在Java代码中使用ZipFile解压zip文件
- 使用 ZLib 压缩/解压 ZIP 文件
- Iphone 上使用libz库解压zip,gzip文件
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- Iphone 上使用libz库解压zip,gzip文件
- 使用java.util.zip压缩、解压文件