From 91cb18dd8f6686d064270a81aca036c702b3a23f Mon Sep 17 00:00:00 2001 From: 1024jp <1024jp@wolfrosch.com> Date: Mon, 18 Jul 2016 00:55:34 +0900 Subject: [PATCH] Add `isGzipped` proeprty --- CHANGELOG.md | 1 + Project/Tests/NSData_GZIPTests.swift | 4 ++++ README.md | 16 +++++++++++----- Sources/NSData+GZIP.swift | 18 ++++++++++++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d58932..a57ecf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Change Log - Migrate code to Swift 3.0 - Add `level` option to `gzipped()` method. +- Add `isGzipped` property (readonly). diff --git a/Project/Tests/NSData_GZIPTests.swift b/Project/Tests/NSData_GZIPTests.swift index 9a263c5..51321d8 100644 --- a/Project/Tests/NSData_GZIPTests.swift +++ b/Project/Tests/NSData_GZIPTests.swift @@ -44,6 +44,10 @@ class NSData_GZIPTests: XCTestCase XCTAssertNotEqual(gzipped, data) XCTAssertEqual(uncompressedSentence, testSentence) + + XCTAssertTrue(gzipped.isGzipped) + XCTAssertFalse(data.isGzipped) + XCTAssertFalse(uncompressed.isGzipped) } diff --git a/README.md b/README.md index 1199925..0beac06 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +NSData+GZIP.swift is distributed under the terms of the __MIT License__. See [LISENCE](LISENCE) for details. diff --git a/Sources/NSData+GZIP.swift b/Sources/NSData+GZIP.swift index 6c52f6d..d3c573b 100644 --- a/Sources/NSData+GZIP.swift +++ b/Sources/NSData+GZIP.swift @@ -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`