//
// UPHomeViewControl.m
// UnpackPro
//
// Created by ylb on 2021/1/27.
//
#import "UPHomeViewControl.h"
#import <SSZipArchive/SSZipArchive.h>
#import "UPRarViewControl.h"
@interface UPHomeViewControl ()
@property (weak, nonatomic) IBOutlet UITextField *pwd_txt;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (nonatomic,strong)NSString *zipPath;
@end
@implementation UPHomeViewControl
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"ZIP";
[self addNavigationItemWithTitles:@[@"RAR"] isLeft:NO target:self action:@selector(rightClick) tags:nil withTextColor:TYPEFACE_COLOR];
}
-(void)rightClick
{
[self.navigationController pushViewController:[UPRarViewControl new] animated:YES];
}
#pragma mark - 压缩
- (IBAction)zipClick:(id)sender {
NSString *filePath = [[NSBundle mainBundle].bundleURL
URLByAppendingPathComponent:@"说明"
isDirectory:YES].path;
NSLog(@"zip path: %@", filePath);
_zipPath = [self tempZipPath];
NSString *password = _pwd_txt.text;
BOOL success = [SSZipArchive createZipFileAtPath:_zipPath
withContentsOfDirectory:filePath
keepParentDirectory:NO
compressionLevel:-1
password:password.length > 0 ? password : nil
AES:YES
progressHandler:^(NSUInteger entryNumber, NSUInteger total) {
}];
if (success) {
NSLog(@"Success zip");
[self prompt: @"压缩成功"];
} else {
NSLog(@"No success zip");
[self prompt: @"压缩失败"];
}
}
#pragma mark - 解压
- (IBAction)unzipClick:(id)sender {
if (!_zipPath) {
return;
}
NSString *unzipPath = [self tempUnzipPath];
NSLog(@"Unzip path: %@", unzipPath);
if (!unzipPath) {
return;
}
NSString *password = _pwd_txt.text;
BOOL success = [SSZipArchive unzipFileAtPath:_zipPath
toDestination:unzipPath
preserveAttributes:YES
overwrite:YES
nestedZipLevel:0
password:password.length > 0 ? password : nil
error:nil
delegate:nil
progressHandler:nil
completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
}];
if (success) {
NSLog(@"Success unzip");
[self prompt: @"解压成功"];
} else {
NSLog(@"No success unzip");
[self prompt: @"解压失败"];
return;
}
NSError *error = nil;
NSMutableArray<NSString *> *items = [[[NSFileManager defaultManager]
contentsOfDirectoryAtPath:unzipPath
error:&error] mutableCopy];
if (error) {
return;
}
[items enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
switch (idx) {
case 0: {
self.lab1.text = obj;
break;
}
case 1: {
self.lab2.text = obj;
break;
}
case 2: {
self.lab3.text = obj;
break;
}
default: {
NSLog(@"Went beyond index of assumed files");
break;
}
}
}];
}
#pragma mark - Private
- (NSString *)tempZipPath {
NSString *path = [NSString stringWithFormat:@"%@/%@.zip",
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
[NSUUID UUID].UUIDString];
return path;
}
- (NSString *)tempUnzipPath {
NSString *path = [NSString stringWithFormat:@"%@/%@",
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 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
//
// UPRarViewControl.m
// UnpackPro
//
// Created by ylb on 2021/1/27.
//
#import "UPRarViewControl.h"
#import <UnrarKit.h>
@interface UPRarViewControl ()
@property (weak, nonatomic) IBOutlet UITextField *pwd_txt;
@end
@implementation UPRarViewControl
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"RAR";
}
/** 解压 */
- (IBAction)rarClick:(id)sender {
// //获取Document路径
// NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsPath = [documentsPathArr lastObject];
// //拼接上文件
// NSString *fileString = [NSString stringWithFormat:@"Announcement/%@",self.rarFileName];
// 拼接要写入文件的路径
// NSString *path = [documentsPath stringByAppendingPathComponent:fileString];
NSString *filePath = [[NSBundle mainBundle].bundleURL
URLByAppendingPathComponent:@"Mobile - PNG.rar"
isDirectory:YES].path;
NSLog(@"zip path: %@", filePath);
//文件路径
NSError *archiveError = nil;
URKArchive *archive = [[URKArchive alloc] initWithPath:filePath error:&archiveError];
if (!archive) {
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示"
message:@"解压失败,请重试!"
preferredStyle:UIAlertControllerStyleAlert];
[controller addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:controller animated:YES completion:nil];
return;
}
NSError *error = nil;
NSArray *filenames = [archive listFilenames:&error];
if (error) {
[self prompt:@"解压失败"];
return;
}
for (NSString *filename in filenames) {
NSLog(@"File: %@", filename);//打印文件名称
}
NSData *data = [archive extractDataFromFile:filenames[0] progress:^(CGFloat percentDecompressed) {
NSLog(@"");
} error:&error];
if (error) {
[self prompt:@"解压失败"];
if (error.code == ERAR_MISSING_PASSWORD) {
}
}
if (data != nil) {
//解压后的文件操作可以在这里进行处理,比如保存或者显示处理都可以
// 获取路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 拼接文件路径
NSString *path = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"AnnouncementData/%@",filenames[0]]];
BOOL isSuccess = [data writeToFile:path atomically:YES];//存储
if (isSuccess) {
[self prompt:@"写入成功"];
} else {
[self prompt:@"写入失败"];
}
}
}
@end
网友评论