第三方解压缩框架——SSZipArchive
下载地址:https://github.com/samsoffes/ssziparchive
注意:需要引入libz.dylib框架
// Unzipping
NSString *zipPath = @"path_to_your_zip_file";
NSString *destinationPath [email protected]"path_to_the_folder_where_you_want_it_unzipped";
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
// Zipping
NSString *zippedPath = @"path_where_you_want_the_file_created";
NSArray *inputPaths = [NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"],
[[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"]
nil];
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
一、技术方案
1.第三方框架:SSZipArchive
2.依赖的动态库:libz.dylib
二、压缩1
1.第一个方法
/**
zipFile :产生的zip文件的最终路径
directory : 需要进行的压缩的文件夹路径
*/
[SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:directory];
2.第一个方法
/**
zipFile :产生的zip文件的最终路径
files : 这是一个数组,数组里面存放的是需要压缩的文件的路径
files = @[@”/Users/apple/Destop/1.png”, @”/Users/apple/Destop/3.txt”]
*/
[SSZipArchive createZipFileAtPath:zipFile withFilesAtPaths:files];
三、解压缩
/**
zipFile :需要解压的zip文件的路径
dest : 解压到什么地方
*/
[SSZipArchive unzipFileAtPath:zipFile toDestination:dest];
文件压缩实例
#import "MalJobViewController.h"
#import "SSZipArchive.h"
#define MalJobFileBoundary @"heima"
#define MalJobNewLine @"\r\n"
#define MalJobEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
@interface MalJobViewController ()
@end
@implementation MalJobViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSString *)MIMEType:(NSURL *)url
{
// 1.创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求(返回响应)
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// 3.获得MIMEType
return response.MIMEType;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// 0.获得需要压缩的文件夹
NSString *images = [caches stringByAppendingPathComponent:@"images"];
// 1.创建一个zip文件(压缩)
NSString *zipFile = [caches stringByAppendingPathComponent:@"images.zip"];
BOOL result = [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];
if(result) {
NSString *MIMEType = [self MIMEType:[NSURL fileURLWithPath:zipFile]];
NSData *data = [NSData dataWithContentsOfFile:zipFile];
[self upload:@"images.zip" mimeType:MIMEType fileData:data params:@{@"username" : @"lisi"}];
}
}
- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params
{
// 1.请求路径
NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/Server/upload"];
// 2.创建一个POST请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 3.设置请求体
NSMutableData *body = [NSMutableData data];
// 3.1.文件参数
[body appendData:MalJobEncode(@"--")];
[body appendData:MalJobEncode(MalJobFileBoundary)];
[body appendData:MalJobEncode(MalJobNewLine)];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename];
[body appendData:MalJobEncode(disposition)];
[body appendData:MalJobEncode(MalJobNewLine)];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType];
[body appendData:MalJobEncode(type)];
[body appendData:MalJobEncode(MalJobNewLine)];
[body appendData:MalJobEncode(MalJobNewLine)];
[body appendData:fileData];
[body appendData:MalJobEncode(MalJobNewLine)];
// 3.2.非文件参数
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[body appendData:MalJobEncode(@"--")];
[body appendData:MalJobEncode(MalJobFileBoundary)];
[body appendData:MalJobEncode(MalJobNewLine)];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key];
[body appendData:MalJobEncode(disposition)];
[body appendData:MalJobEncode(MalJobNewLine)];
[body appendData:MalJobEncode(MalJobNewLine)];
[body appendData:MalJobEncode([obj description])];
[body appendData:MalJobEncode(MalJobNewLine)];
}];
// 3.3.结束标记
[body appendData:MalJobEncode(@"--")];
[body appendData:MalJobEncode(MalJobFileBoundary)];
[body appendData:MalJobEncode(@"--")];
[body appendData:MalJobEncode(MalJobNewLine)];
request.HTTPBody = body;
// 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求)
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MalJobFileBoundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 5.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", dict);
}];
}
@end
文件解压缩实例
#import "MalJobViewController.h"
#import "SSZipArchive.h"
@implementation MalJobViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/resources/images.zip"];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
[SSZipArchive unzipFileAtPath:location.path toDestination:caches];
}];
[task resume];
}
@end
时间: 2024-11-05 20:33:53