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 - Migrate code to Swift 3.0
- Add `level` option to `gzipped()` method. - Add `level` option to `gzipped()` method.
- Add `isGzipped` property (readonly).

View File

@ -44,6 +44,10 @@ class NSData_GZIPTests: XCTestCase
XCTAssertNotEqual(gzipped, data) XCTAssertNotEqual(gzipped, data)
XCTAssertEqual(uncompressedSentence, testSentence) 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 ```swift
// gzip // gzip
let compressedData : Data = try! data.gzipped() let compressedData: Data = try! data.gzipped()
let optimizedData : Data = try! data.gzipped(level: .bestCompression) let optimizedData: Data = try! data.gzipped(level: .bestCompression)
// gunzip // 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) ![screenshot](Documentation/binary_link@2x.png)
4. In *Build Settings* > *Swift Compiler - Search Paths*, Add path to `zlib/` to Import Paths (`SWIFT_INCLUDE_PATHS`). 4. In *Build Settings* > *Swift Compiler - Search Paths*, Add path to `zlib/` to Import Paths (`SWIFT_INCLUDE_PATHS`).
![screenshot](Documentation/search_paths@2x.png) ![screenshot](Documentation/search_paths@2x.png)
4. Invoke from your Swift/ObjC files. 5. Invoke from your Swift/ObjC files.
## Lisence ## Lisence
© 2014-2016 1024jp © 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 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. Throws an error if compression failed.
- parameters: - 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 an error if decompression failed.
- throws: `GzipError` - throws: `GzipError`