Skip to content

Commit 9fe601b

Browse files
committed
Merge branch 'libraryincluded'
2 parents 92d6f8e + 5726205 commit 9fe601b

16 files changed

+5225
-192
lines changed

Diff for: .gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Zip/minizip/* linguist-vendored

Diff for: README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
![Zip - Zip and unzip files in Swift](https://cloud.githubusercontent.com/assets/889949/12374908/252373d0-bcac-11e5-8ece-6933aeae8222.png)
22

33
[![Build Status](https://travis-ci.org/marmelroy/Zip.svg?branch=master)](https://travis-ci.org/marmelroy/Zip) [![Version](http://img.shields.io/cocoapods/v/Zip.svg)](http://cocoapods.org/?q=Zip)
4+
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
45

56
# Zip
67
A Swift framework for zipping and unzipping files. Simple and quick to use. Built on top of [minizip](https://github.com/nmoinvaz/minizip).
@@ -18,7 +19,7 @@ import Zip
1819
The easiest way to use Zip is through quick functions. Both take local file paths as NSURLs, throw if an error is encountered and return an NSURL to the destination if successful.
1920
```swift
2021
do {
21-
let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
22+
let filePath = Bundle.main.url(forResource: "file", withExtension: "zip")!
2223
let unzipDirectory = try Zip.quickUnzipFile(filePath) // Unzip
2324
let zipFilePath = try Zip.quickZipFiles([filePath], fileName: "archive") // Zip
2425
}
@@ -32,14 +33,13 @@ catch {
3233
For more advanced usage, Zip has functions that let you set custom destination paths, work with password protected zips and use a progress handling closure. These functions throw if there is an error but don't return.
3334
```swift
3435
do {
35-
let filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "zip")!
36-
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
37-
36+
let filePath = Bundle.main.url(forResource: "file", withExtension: "zip")!
37+
let documentsDirectory = FileManager.default.urls(for:.documentDirectory, in: .userDomainMask)[0]
3838
try Zip.unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
3939
print(progress)
4040
}) // Unzip
4141

42-
let zipFilePath = documentsFolder.URLByAppendingPathComponent("archive.zip")
42+
let zipFilePath = documentsFolder.appendingPathComponent("archive.zip")
4343
try Zip.zipFiles([filePath], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in
4444
print(progress)
4545
}) //Zip
@@ -60,5 +60,22 @@ Zip.addCustomFileExtension("file-extension-here")
6060
### Setting up with [CocoaPods](http://cocoapods.org/?q=Zip)
6161
```ruby
6262
source 'https://github.com/CocoaPods/Specs.git'
63-
pod 'Zip', '~> 0.5', :submodules => true
63+
pod 'Zip', '~> 0.6'
64+
```
65+
66+
### Setting up with Carthage
67+
68+
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
69+
70+
You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
71+
72+
```bash
73+
$ brew update
74+
$ brew install carthage
75+
```
76+
77+
To integrate Zip into your Xcode project using Carthage, specify it in your `Cartfile`:
78+
79+
```ogdl
80+
github "marmelroy/Zip"
6481
```

Diff for: Zip.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = "Zip"
11-
s.version = "0.5"
11+
s.version = "0.6"
1212
s.summary = "Zip and unzip files in Swift."
1313

1414
# This description is used to generate tags and improve search results.
@@ -24,7 +24,7 @@ Pod::Spec.new do |s|
2424
# s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2"
2525
s.license = 'MIT'
2626
s.author = { "Roy Marmelstein" => "[email protected]" }
27-
s.source = { :git => "https://github.com/marmelroy/Zip.git", :tag => s.version.to_s, :submodules => true}
27+
s.source = { :git => "https://github.com/marmelroy/Zip.git", :tag => s.version.to_s}
2828
s.social_media_url = "http://twitter.com/marmelroy"
2929

3030
s.ios.deployment_target = '8.0'

Diff for: Zip.xcodeproj/project.pbxproj

+6-176
Large diffs are not rendered by default.

Diff for: Zip/QuickZip.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extension Zip {
8989
let fileManager = FileManager.default
9090
let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
9191
let destinationUrl = documentsUrl.appendingPathComponent("\(fileName).zip")
92-
try self.zipFiles(paths, zipFilePath: destinationUrl, password: nil, progress: progress)
92+
try self.zipFiles(paths: paths, zipFilePath: destinationUrl, password: nil, progress: progress)
9393
return destinationUrl
9494
}
9595

Diff for: Zip/Zip.swift

+25-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ public enum ZipError: Error {
2828
}
2929
}
3030

31+
public enum ZipCompression: Int {
32+
case NoCompression
33+
case BestSpeed
34+
case DefaultCompression
35+
case BestCompression
36+
37+
internal var minizipCompression: Int32 {
38+
switch self {
39+
case .NoCompression:
40+
return Z_NO_COMPRESSION
41+
case .BestSpeed:
42+
return Z_BEST_SPEED
43+
case .DefaultCompression:
44+
return Z_DEFAULT_COMPRESSION
45+
case .BestCompression:
46+
return Z_BEST_COMPRESSION
47+
}
48+
}
49+
}
50+
3151
/// Zip class
3252
public class Zip {
3353

@@ -197,19 +217,21 @@ public class Zip {
197217

198218
// MARK: Zip
199219

220+
200221
/**
201222
Zip files.
202223

203224
- parameter paths: Array of NSURL filepaths.
204225
- parameter zipFilePath: Destination NSURL, should lead to a .zip filepath.
205226
- parameter password: Password string. Optional.
227+
- parameter compression: Compression strategy
206228
- parameter progress: A progress closure called after unzipping each file in the archive. Double value betweem 0 and 1.
207229

208230
- throws: Error if zipping fails.
209231

210232
- notes: Supports implicit progress composition
211233
*/
212-
public class func zipFiles(_ paths: [URL], zipFilePath: URL, password: String?, progress: ((_ progress: Double) -> ())?) throws {
234+
public class func zipFiles(paths: [URL], zipFilePath: URL, password: String?, compression: ZipCompression = .DefaultCompression, progress: ((_ progress: Double) -> ())?) throws {
213235

214236
// File manager
215237
let fileManager = FileManager.default
@@ -275,10 +297,10 @@ public class Zip {
275297
catch {}
276298
let buffer = malloc(chunkSize)
277299
if let password = password, let fileName = fileName {
278-
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, 0)
300+
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, compression.minizipCompression, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, 0)
279301
}
280302
else if let fileName = fileName {
281-
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, nil, 0)
303+
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil,Z_DEFLATED, compression.minizipCompression, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, nil, 0)
282304
}
283305
else {
284306
throw ZipError.zipFail

Diff for: Zip/minizip

Submodule minizip deleted from 202b541

Diff for: Zip/minizip/crypt.h

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* crypt.h -- base code for traditional PKWARE encryption
2+
Version 1.01e, February 12th, 2005
3+
4+
Copyright (C) 1998-2005 Gilles Vollant
5+
Modifications for Info-ZIP crypting
6+
Copyright (C) 2003 Terry Thorsen
7+
8+
This code is a modified version of crypting code in Info-ZIP distribution
9+
10+
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
11+
12+
See the Info-ZIP LICENSE file version 2000-Apr-09 or later for terms of use
13+
which also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
14+
15+
The encryption/decryption parts of this source code (as opposed to the
16+
non-echoing password parts) were originally written in Europe. The
17+
whole source package can be freely distributed, including from the USA.
18+
(Prior to January 2000, re-export from the US was a violation of US law.)
19+
20+
This encryption code is a direct transcription of the algorithm from
21+
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
22+
file (appnote.txt) is distributed with the PKZIP program (even in the
23+
version without encryption capabilities).
24+
25+
If you don't need crypting in your application, just define symbols
26+
NOCRYPT and NOUNCRYPT.
27+
*/
28+
29+
#define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
30+
31+
/***********************************************************************
32+
* Return the next byte in the pseudo-random sequence
33+
*/
34+
static int decrypt_byte(unsigned long* pkeys)
35+
{
36+
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
37+
* unpredictable manner on 16-bit systems; not a problem
38+
* with any known compiler so far, though */
39+
40+
temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
41+
return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
42+
}
43+
44+
/***********************************************************************
45+
* Update the encryption keys with the next byte of plain text
46+
*/
47+
static int update_keys(unsigned long* pkeys, const unsigned long* pcrc_32_tab, int c)
48+
{
49+
(*(pkeys+0)) = CRC32((*(pkeys+0)), c);
50+
(*(pkeys+1)) += (*(pkeys+0)) & 0xff;
51+
(*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
52+
{
53+
register int keyshift = (int)((*(pkeys+1)) >> 24);
54+
(*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
55+
}
56+
return c;
57+
}
58+
59+
60+
/***********************************************************************
61+
* Initialize the encryption keys and the random header according to
62+
* the given password.
63+
*/
64+
static void init_keys(const char* passwd, unsigned long* pkeys, const unsigned long* pcrc_32_tab)
65+
{
66+
*(pkeys+0) = 305419896L;
67+
*(pkeys+1) = 591751049L;
68+
*(pkeys+2) = 878082192L;
69+
while (*passwd != 0)
70+
{
71+
update_keys(pkeys,pcrc_32_tab,(int)*passwd);
72+
passwd++;
73+
}
74+
}
75+
76+
#define zdecode(pkeys,pcrc_32_tab,c) \
77+
(update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys)))
78+
79+
#define zencode(pkeys,pcrc_32_tab,c,t) \
80+
(t=decrypt_byte(pkeys), update_keys(pkeys,pcrc_32_tab,c), t^(c))
81+
82+
#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
83+
84+
#define RAND_HEAD_LEN 12
85+
/* "last resort" source for second part of crypt seed pattern */
86+
# ifndef ZCR_SEED2
87+
# define ZCR_SEED2 3141592654UL /* use PI as default pattern */
88+
# endif
89+
90+
static int crypthead(const char* passwd, /* password string */
91+
unsigned char* buf, /* where to write header */
92+
int bufSize,
93+
unsigned long* pkeys,
94+
const unsigned long* pcrc_32_tab,
95+
unsigned long crcForCrypting)
96+
{
97+
int n; /* index in random header */
98+
int t; /* temporary */
99+
int c; /* random byte */
100+
unsigned char header[RAND_HEAD_LEN-2]; /* random header */
101+
static unsigned calls = 0; /* ensure different random header each time */
102+
103+
if (bufSize < RAND_HEAD_LEN)
104+
return 0;
105+
106+
/* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
107+
* output of rand() to get less predictability, since rand() is
108+
* often poorly implemented.
109+
*/
110+
if (++calls == 1)
111+
{
112+
srand((unsigned)(time(NULL) ^ ZCR_SEED2));
113+
}
114+
init_keys(passwd, pkeys, pcrc_32_tab);
115+
for (n = 0; n < RAND_HEAD_LEN-2; n++)
116+
{
117+
c = (rand() >> 7) & 0xff;
118+
header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
119+
}
120+
/* Encrypt random header (last two bytes is high word of crc) */
121+
init_keys(passwd, pkeys, pcrc_32_tab);
122+
for (n = 0; n < RAND_HEAD_LEN-2; n++)
123+
{
124+
buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
125+
}
126+
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
127+
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
128+
return n;
129+
}
130+
131+
#endif

0 commit comments

Comments
 (0)