add example support for password

This commit is contained in:
Antoine Cœur
2017-07-12 15:19:45 +08:00
parent 7232850da1
commit 09e7b26686
6 changed files with 41 additions and 11 deletions
@@ -17,6 +17,12 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="KJF-p6-1px">
<rect key="frame" x="258.5" y="46" width="83" height="30"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="uTp-oA-6Sj">
<rect key="frame" x="244" y="84" width="113" height="30"/>
<constraints>
@@ -94,6 +100,8 @@
<constraint firstItem="ZxR-a0-kva" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="jRJ-c6-8LV"/>
<constraint firstItem="2Ts-JV-yCW" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="lEj-O2-aka"/>
<constraint firstItem="2Ts-JV-yCW" firstAttribute="top" secondItem="uTp-oA-6Sj" secondAttribute="bottom" constant="8" id="mnz-Ae-5eG"/>
<constraint firstItem="uTp-oA-6Sj" firstAttribute="top" secondItem="KJF-p6-1px" secondAttribute="bottom" constant="8" symbolic="YES" id="uLh-xJ-Xc6"/>
<constraint firstItem="KJF-p6-1px" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="wVb-JQ-3pN"/>
<constraint firstItem="84L-OB-eyZ" firstAttribute="top" secondItem="b0V-gX-SgI" secondAttribute="bottom" constant="8" id="xAV-Uy-dqy"/>
</constraints>
</view>
@@ -101,6 +109,7 @@
<outlet property="file1" destination="3gB-ZC-0ON" id="GUd-wx-xEa"/>
<outlet property="file2" destination="ZxR-a0-kva" id="AZi-Dj-dI8"/>
<outlet property="file3" destination="b0V-gX-SgI" id="zwq-l2-xZK"/>
<outlet property="passwordField" destination="KJF-p6-1px" id="3yT-Aw-6uA"/>
<outlet property="resetButton" destination="84L-OB-eyZ" id="9VY-KX-sKi"/>
<outlet property="unzipButton" destination="2Ts-JV-yCW" id="2LN-kr-Sei"/>
<outlet property="zipButton" destination="uTp-oA-6Sj" id="Tl8-0B-gHY"/>
@@ -11,6 +11,7 @@
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *zipButton;
@property (weak, nonatomic) IBOutlet UIButton *unzipButton;
@property (weak, nonatomic) IBOutlet UIButton *resetButton;
@@ -42,12 +43,15 @@
URLByAppendingPathComponent:@"Sample Data"
isDirectory:YES].path;
_zipPath = [self tempZipPath];
NSString *password = _passwordField.text;
BOOL success = [SSZipArchive createZipFileAtPath:_zipPath
withContentsOfDirectory:sampleDataPath];
withContentsOfDirectory:sampleDataPath
withPassword:password.length > 0 ? password : nil];
if (success) {
_unzipButton.enabled = YES;
_zipButton.enabled = NO;
}
_resetButton.enabled = YES;
}
- (IBAction)unzipPressed:(id)sender {
@@ -58,9 +62,14 @@
if (!unzipPath) {
return;
}
NSString *password = _passwordField.text;
BOOL success = [SSZipArchive unzipFileAtPath:_zipPath
toDestination:unzipPath];
toDestination:unzipPath
overwrite:YES
password:password.length > 0 ? password : nil
error:nil];
if (!success) {
NSLog(@"No success");
return;
}
NSError *error = nil;
@@ -91,7 +100,6 @@
}
}];
_unzipButton.enabled = NO;
_resetButton.enabled = YES;
}
- (IBAction)resetPressed:(id)sender {
+2 -2
View File
@@ -1,5 +1,5 @@
PODS:
- SSZipArchive (1.8.1)
- SSZipArchive (2.0.0)
DEPENDENCIES:
- SSZipArchive (from `..`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: ".."
SPEC CHECKSUMS:
SSZipArchive: dcfcc7140abf0e3bd81bf2c373099ac627738182
SSZipArchive: 8153333145bf99acaf1918d257fdcef68a9269e1
PODFILE CHECKSUM: 7f4058a9cbc69b4e63808729577a8bb2098bc527
+2 -2
View File
@@ -1,5 +1,5 @@
PODS:
- SSZipArchive (1.8.1)
- SSZipArchive (2.0.0)
DEPENDENCIES:
- SSZipArchive (from `..`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: ".."
SPEC CHECKSUMS:
SSZipArchive: dcfcc7140abf0e3bd81bf2c373099ac627738182
SSZipArchive: 8153333145bf99acaf1918d257fdcef68a9269e1
PODFILE CHECKSUM: 0dc500eb72745751ccba7677de4da5534fcef36d
@@ -22,6 +22,12 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Tal-c0-sro">
<rect key="frame" x="166" y="13" width="83" height="30"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Uwd-kM-cdh">
<rect key="frame" x="150.66666666666669" y="51" width="113.00000000000006" height="30"/>
<state key="normal" title="Zip Sample Data"/>
@@ -67,8 +73,10 @@
<constraint firstAttribute="trailingMargin" secondItem="09F-6Q-slx" secondAttribute="trailing" constant="33" id="3S4-8t-aQA"/>
<constraint firstItem="XUJ-IR-RIS" firstAttribute="centerX" secondItem="Lbq-oS-Rlj" secondAttribute="centerX" id="AYF-iB-SJU"/>
<constraint firstItem="Uwd-kM-cdh" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="CTF-Hv-dt4"/>
<constraint firstItem="Tal-c0-sro" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Czk-W6-E7s"/>
<constraint firstItem="XUJ-IR-RIS" firstAttribute="top" secondItem="Uwd-kM-cdh" secondAttribute="bottom" constant="8" symbolic="YES" id="OIr-Yf-tdY"/>
<constraint firstItem="09F-6Q-slx" firstAttribute="trailing" secondItem="PSK-Jd-Mwc" secondAttribute="trailing" id="TGC-ob-dm0"/>
<constraint firstItem="Uwd-kM-cdh" firstAttribute="top" secondItem="Tal-c0-sro" secondAttribute="bottom" constant="8" symbolic="YES" id="Udb-33-mrs"/>
<constraint firstItem="Uwd-kM-cdh" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="31" id="Uh0-R8-WFe"/>
<constraint firstItem="pW5-rw-iqM" firstAttribute="top" secondItem="PSK-Jd-Mwc" secondAttribute="bottom" constant="8" symbolic="YES" id="VfM-Vg-Yai"/>
<constraint firstItem="09F-6Q-slx" firstAttribute="top" secondItem="XUJ-IR-RIS" secondAttribute="bottom" constant="8" symbolic="YES" id="Wpv-VU-gb1"/>
@@ -85,6 +93,7 @@
<outlet property="file1" destination="09F-6Q-slx" id="Rmp-Xl-xfc"/>
<outlet property="file2" destination="PSK-Jd-Mwc" id="Fek-5n-1P2"/>
<outlet property="file3" destination="pW5-rw-iqM" id="8cy-pX-Oa9"/>
<outlet property="passwordField" destination="Tal-c0-sro" id="E18-xQ-8Zd"/>
<outlet property="resetButton" destination="Lbq-oS-Rlj" id="NMW-xE-8bl"/>
<outlet property="unzipButton" destination="XUJ-IR-RIS" id="mVz-FE-G7d"/>
<outlet property="zipButton" destination="Uwd-kM-cdh" id="zCa-UM-3rx"/>
@@ -16,6 +16,7 @@ import UIKit
class ViewController: UIViewController {
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var zipButton: UIButton!
@IBOutlet weak var unzipButton: UIButton!
@IBOutlet weak var resetButton: UIButton!
@@ -45,12 +46,14 @@ class ViewController: UIViewController {
@IBAction func zipPressed(_: UIButton) {
let sampleDataPath = Bundle.main.bundleURL.appendingPathComponent("Sample Data").path
zipPath = tempZipPath()
let password = passwordField.text
let success = SSZipArchive.createZipFile(atPath: zipPath!, withContentsOfDirectory: sampleDataPath)
let success = SSZipArchive.createZipFile(atPath: zipPath!, withContentsOfDirectory: sampleDataPath, withPassword: password?.isEmpty == false ? password : nil)
if success {
unzipButton.isEnabled = true
zipButton.isEnabled = false
}
resetButton.isEnabled = true
}
@IBAction func unzipPressed(_: UIButton) {
@@ -62,8 +65,10 @@ class ViewController: UIViewController {
return
}
let success = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
if !success {
let password = passwordField.text
let success: Void? = try? SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath, overwrite: true, password: password?.isEmpty == false ? password : nil)
if success == nil {
print("No success")
return
}
@@ -88,7 +93,6 @@ class ViewController: UIViewController {
}
unzipButton.isEnabled = false
resetButton.isEnabled = true
}
@IBAction func resetPressed(_: UIButton) {