Skip to content

Commit

Permalink
Add isGzipped proeprty
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 17, 2016
1 parent c90df56 commit 91cb18d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Change Log

- Migrate code to Swift 3.0
- Add `level` option to `gzipped()` method.
- Add `isGzipped` property (readonly).



Expand Down
4 changes: 4 additions & 0 deletions Project/Tests/NSData_GZIPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class NSData_GZIPTests: XCTestCase

XCTAssertNotEqual(gzipped, data)
XCTAssertEqual(uncompressedSentence, testSentence)

XCTAssertTrue(gzipped.isGzipped)
XCTAssertFalse(data.isGzipped)
XCTAssertFalse(uncompressed.isGzipped)
}


Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

```


Expand All @@ -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.
18 changes: 16 additions & 2 deletions Sources/NSData+GZIP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,22 @@ public enum GzipError: ErrorProtocol {

public extension Data
{

/**
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 reciver using zlib.
Create a new `Data` object by compressing the receiver using zlib.
Throws an error if compression failed.

- parameters:
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 91cb18d

Please sign in to comment.