Add `isGzipped` proeprty

This commit is contained in:
1024jp 2016-07-18 00:55:34 +09:00
parent c90df5679c
commit 91cb18dd8f
4 changed files with 32 additions and 7 deletions

View File

@ -9,6 +9,7 @@ Change Log
- Migrate code to Swift 3.0
- Add `level` option to `gzipped()` method.
- Add `isGzipped` property (readonly).

View File

@ -44,6 +44,10 @@ class NSData_GZIPTests: XCTestCase
XCTAssertNotEqual(gzipped, data)
XCTAssertEqual(uncompressedSentence, testSentence)
XCTAssertTrue(gzipped.isGzipped)
XCTAssertFalse(data.isGzipped)
XCTAssertFalse(uncompressed.isGzipped)
}

View File

@ -14,11 +14,17 @@ __NSData+GZIP.swift__ is an extension of Data written in Swift 3.0. It enables c
```swift
// gzip
let compressedData : Data = try! data.gzipped()
let optimizedData : Data = try! data.gzipped(level: .bestCompression)
let compressedData: Data = try! data.gzipped()
let optimizedData: Data = try! data.gzipped(level: .bestCompression)
// gunzip
let decompressedData : Data = try! compressedData.gunzipped()
let decompressedData: Data
if data.isGzipped {
decompressedData = try! data.gunzipped()
} else {
decompressedData = data
}
```
@ -30,11 +36,11 @@ let decompressedData : Data = try! compressedData.gunzipped()
![screenshot](Documentation/binary_link@2x.png)
4. In *Build Settings* > *Swift Compiler - Search Paths*, Add path to `zlib/` to Import Paths (`SWIFT_INCLUDE_PATHS`).
![screenshot](Documentation/search_paths@2x.png)
4. Invoke from your Swift/ObjC files.
5. Invoke from your Swift/ObjC files.
## Lisence
© 2014-2016 1024jp
NSData+GZIP.swift is distributed under the terms of the __MIT License__. See [LISENCE](LISENCE) for details.
NSData+GZIP.swift is distributed under the terms of the __MIT License__. See [LISENCE](LISENCE) for details.

View File

@ -132,8 +132,22 @@ public enum GzipError: ErrorProtocol {
public extension Data
{
/**
Create a new `Data` object by compressing the reciver using zlib.
Check if the reciever is already gzipped.
- returns: Whether the data is compressed.
*/
public var isGzipped: Bool {
guard self.count >= 2 else { return false }
return self[0] == 0x1f && self[1] == 0x8b // check magic number
}
/**
Create a new `Data` object by compressing the receiver using zlib.
Throws an error if compression failed.
- parameters:
@ -184,7 +198,7 @@ public extension Data
/**
Create a new `Data` object by decompressing the reciver using zlib.
Create a new `Data` object by decompressing the receiver using zlib.
Throws an error if decompression failed.
- throws: `GzipError`