From e99b2a26728e4ba276698c1574c8db3e997bdd60 Mon Sep 17 00:00:00 2001 From: Oskari Rauta Date: Wed, 27 Mar 2019 14:31:49 +0200 Subject: [PATCH] Update DataCompression.swift Add variables that can be used to identify data format as zip or gzip. --- Sources/DataCompression/DataCompression.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Sources/DataCompression/DataCompression.swift b/Sources/DataCompression/DataCompression.swift index 21320cb..7b3ad1c 100644 --- a/Sources/DataCompression/DataCompression.swift +++ b/Sources/DataCompression/DataCompression.swift @@ -32,6 +32,35 @@ import Compression public extension Data { + + /// Identifies if data is zipped. + /// - returns: boolean + var isZip: Bool { + get { + // 2 byte header + 4 byte adler32 checksum + let overhead = 6 + guard count > overhead else { return false } + + let header: UInt16 = withUnsafeBytes { (ptr: UnsafePointer) -> UInt16 in + return ptr.pointee.bigEndian + } + + // check for the deflate stream bit + guard header >> 8 & 0b1111 == 0b1000 else { return false } + // check the header checksum + guard header % 31 == 0 else { return false } + return true + } + } + + /// Identifies if data is gzipped. + /// - returns: boolean + var isGzip: Bool { + get { + return self.starts(with: [0x1f, 0x8b, 0x08]) // check magic number + } + } + /// Compresses the data. /// - parameter withAlgorithm: Compression algorithm to use. See the `CompressionAlgorithm` type /// - returns: compressed data