print success in the example projects

This commit is contained in:
Antoine Cœur 2017-10-08 01:32:33 +08:00
parent b8a68d1a08
commit 252fb76ff4
2 changed files with 92 additions and 96 deletions

View File

@ -26,115 +26,114 @@
#pragma mark - Life Cycle #pragma mark - Life Cycle
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
_file1.text = @""; _file1.text = @"";
_file2.text = @""; _file2.text = @"";
_file3.text = @""; _file3.text = @"";
} }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - IBAction #pragma mark - IBAction
- (IBAction)zipPressed:(id)sender { - (IBAction)zipPressed:(id)sender {
NSString *sampleDataPath = [[NSBundle mainBundle].bundleURL NSString *sampleDataPath = [[NSBundle mainBundle].bundleURL
URLByAppendingPathComponent:@"Sample Data" URLByAppendingPathComponent:@"Sample Data"
isDirectory:YES].path; isDirectory:YES].path;
_zipPath = [self tempZipPath]; _zipPath = [self tempZipPath];
NSString *password = _passwordField.text; NSString *password = _passwordField.text;
BOOL success = [SSZipArchive createZipFileAtPath:_zipPath BOOL success = [SSZipArchive createZipFileAtPath:_zipPath
withContentsOfDirectory:sampleDataPath withContentsOfDirectory:sampleDataPath
withPassword:password.length > 0 ? password : nil]; withPassword:password.length > 0 ? password : nil];
if (success) { if (success) {
_unzipButton.enabled = YES; NSLog(@"Success zip");
_zipButton.enabled = NO; _unzipButton.enabled = YES;
} else { _zipButton.enabled = NO;
NSLog(@"No success"); } else {
} NSLog(@"No success zip");
_resetButton.enabled = YES; }
_resetButton.enabled = YES;
} }
- (IBAction)unzipPressed:(id)sender { - (IBAction)unzipPressed:(id)sender {
if (!_zipPath) { if (!_zipPath) {
return; return;
}
NSString *unzipPath = [self tempUnzipPath];
if (!unzipPath) {
return;
}
NSString *password = _passwordField.text;
BOOL success = [SSZipArchive unzipFileAtPath:_zipPath
toDestination:unzipPath
overwrite:YES
password:password.length > 0 ? password : nil
error:nil];
if (!success) {
NSLog(@"No success");
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.file1.text = obj;
break;
}
case 1: {
self.file2.text = obj;
break;
}
case 2: {
self.file3.text = obj;
break;
}
default: {
NSLog(@"Went beyond index of assumed files");
break;
}
} }
}]; NSString *unzipPath = [self tempUnzipPath];
_unzipButton.enabled = NO; if (!unzipPath) {
return;
}
NSString *password = _passwordField.text;
BOOL success = [SSZipArchive unzipFileAtPath:_zipPath
toDestination:unzipPath
overwrite:YES
password:password.length > 0 ? password : nil
error:nil];
if (success) {
NSLog(@"Success unzip");
} else {
NSLog(@"No success unzip");
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.file1.text = obj;
break;
}
case 1: {
self.file2.text = obj;
break;
}
case 2: {
self.file3.text = obj;
break;
}
default: {
NSLog(@"Went beyond index of assumed files");
break;
}
}
}];
_unzipButton.enabled = NO;
} }
- (IBAction)resetPressed:(id)sender { - (IBAction)resetPressed:(id)sender {
_file1.text = @""; _file1.text = @"";
_file2.text = @""; _file2.text = @"";
_file3.text = @""; _file3.text = @"";
_zipButton.enabled = YES; _zipButton.enabled = YES;
_unzipButton.enabled = NO; _unzipButton.enabled = NO;
_resetButton.enabled = NO; _resetButton.enabled = NO;
} }
#pragma mark - Private #pragma mark - Private
- (NSString *)tempZipPath { - (NSString *)tempZipPath {
NSString *path = [NSString stringWithFormat:@"%@/\%@.zip", NSString *path = [NSString stringWithFormat:@"%@/\%@.zip",
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0], NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
[NSUUID UUID].UUIDString]; [NSUUID UUID].UUIDString];
return path; return path;
} }
- (NSString *)tempUnzipPath { - (NSString *)tempUnzipPath {
NSString *path = [NSString stringWithFormat:@"%@/\%@", NSString *path = [NSString stringWithFormat:@"%@/\%@",
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0], NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
[NSUUID UUID].UUIDString]; [NSUUID UUID].UUIDString];
NSURL *url = [NSURL fileURLWithPath:path]; NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil; NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtURL:url [[NSFileManager defaultManager] createDirectoryAtURL:url
withIntermediateDirectories:YES withIntermediateDirectories:YES
attributes:nil attributes:nil
error:&error]; error:&error];
if (error) { if (error) {
return nil; return nil;
} }
return url.path; return url.path;
} }
@end @end

View File

@ -36,11 +36,6 @@ class ViewController: UIViewController {
file3.text = "" file3.text = ""
} }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: IBAction // MARK: IBAction
@IBAction func zipPressed(_: UIButton) { @IBAction func zipPressed(_: UIButton) {
@ -50,10 +45,11 @@ class ViewController: UIViewController {
let success = SSZipArchive.createZipFile(atPath: zipPath!, withContentsOfDirectory: sampleDataPath, withPassword: password?.isEmpty == false ? password : nil) let success = SSZipArchive.createZipFile(atPath: zipPath!, withContentsOfDirectory: sampleDataPath, withPassword: password?.isEmpty == false ? password : nil)
if success { if success {
print("Success zip")
unzipButton.isEnabled = true unzipButton.isEnabled = true
zipButton.isEnabled = false zipButton.isEnabled = false
} else { } else {
print("No success") print("No success zip")
} }
resetButton.isEnabled = true resetButton.isEnabled = true
} }
@ -72,8 +68,10 @@ class ViewController: UIViewController {
toDestination: unzipPath, toDestination: unzipPath,
overwrite: true, overwrite: true,
password: password?.isEmpty == false ? password : nil) password: password?.isEmpty == false ? password : nil)
if success == nil { if success != nil {
print("No success") print("Success unzip")
} else {
print("No success unzip")
return return
} }
@ -127,7 +125,6 @@ class ViewController: UIViewController {
} catch { } catch {
return nil return nil
} }
return url.path return url.path
} }