From 252fb76ff42005e83d596b1f9ba7a0a0f37010e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20C=C5=93ur?= Date: Sun, 8 Oct 2017 01:32:33 +0800 Subject: [PATCH] print success in the example projects --- .../ObjectiveCExample/ViewController.m | 173 +++++++++--------- .../SwiftExample/ViewController.swift | 15 +- 2 files changed, 92 insertions(+), 96 deletions(-) diff --git a/ObjectiveCExample/ObjectiveCExample/ViewController.m b/ObjectiveCExample/ObjectiveCExample/ViewController.m index ce93a54..60b1327 100644 --- a/ObjectiveCExample/ObjectiveCExample/ViewController.m +++ b/ObjectiveCExample/ObjectiveCExample/ViewController.m @@ -26,115 +26,114 @@ #pragma mark - Life Cycle - (void)viewDidLoad { - [super viewDidLoad]; + [super viewDidLoad]; _file1.text = @""; _file2.text = @""; _file3.text = @""; } -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; -} - #pragma mark - IBAction - (IBAction)zipPressed:(id)sender { - NSString *sampleDataPath = [[NSBundle mainBundle].bundleURL - URLByAppendingPathComponent:@"Sample Data" - isDirectory:YES].path; - _zipPath = [self tempZipPath]; - NSString *password = _passwordField.text; - BOOL success = [SSZipArchive createZipFileAtPath:_zipPath - withContentsOfDirectory:sampleDataPath - withPassword:password.length > 0 ? password : nil]; - if (success) { - _unzipButton.enabled = YES; - _zipButton.enabled = NO; - } else { - NSLog(@"No success"); - } - _resetButton.enabled = YES; + NSString *sampleDataPath = [[NSBundle mainBundle].bundleURL + URLByAppendingPathComponent:@"Sample Data" + isDirectory:YES].path; + _zipPath = [self tempZipPath]; + NSString *password = _passwordField.text; + BOOL success = [SSZipArchive createZipFileAtPath:_zipPath + withContentsOfDirectory:sampleDataPath + withPassword:password.length > 0 ? password : nil]; + if (success) { + NSLog(@"Success zip"); + _unzipButton.enabled = YES; + _zipButton.enabled = NO; + } else { + NSLog(@"No success zip"); + } + _resetButton.enabled = YES; } - (IBAction)unzipPressed:(id)sender { - if (!_zipPath) { - 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 *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; - } + if (!_zipPath) { + return; } - }]; - _unzipButton.enabled = NO; + 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(@"Success unzip"); + } else { + NSLog(@"No success unzip"); + return; + } + NSError *error = nil; + NSMutableArray *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 { - _file1.text = @""; - _file2.text = @""; - _file3.text = @""; - _zipButton.enabled = YES; - _unzipButton.enabled = NO; - _resetButton.enabled = NO; + _file1.text = @""; + _file2.text = @""; + _file3.text = @""; + _zipButton.enabled = YES; + _unzipButton.enabled = NO; + _resetButton.enabled = NO; } #pragma mark - Private - (NSString *)tempZipPath { - NSString *path = [NSString stringWithFormat:@"%@/\%@.zip", - NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0], - [NSUUID UUID].UUIDString]; - return path; + 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; + 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 diff --git a/SwiftExample/SwiftExample/ViewController.swift b/SwiftExample/SwiftExample/ViewController.swift index 7fa3e13..9ba62ae 100644 --- a/SwiftExample/SwiftExample/ViewController.swift +++ b/SwiftExample/SwiftExample/ViewController.swift @@ -36,11 +36,6 @@ class ViewController: UIViewController { file3.text = "" } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - // MARK: IBAction @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) if success { + print("Success zip") unzipButton.isEnabled = true zipButton.isEnabled = false } else { - print("No success") + print("No success zip") } resetButton.isEnabled = true } @@ -72,8 +68,10 @@ class ViewController: UIViewController { toDestination: unzipPath, overwrite: true, password: password?.isEmpty == false ? password : nil) - if success == nil { - print("No success") + if success != nil { + print("Success unzip") + } else { + print("No success unzip") return } @@ -127,7 +125,6 @@ class ViewController: UIViewController { } catch { return nil } - return url.path }