swift-gzip解壓縮(一)-Data

解壓縮使用swift需要引用libz庫(kù)

  • 聲明Data類(lèi)的擴(kuò)展
/**解壓縮流大小**/
private let GZIP_STREAM_SIZE: Int32 = Int32(MemoryLayout<z_stream>.size)
/**解壓縮緩沖區(qū)大小**/
private let GZIP_BUF_LENGTH:Int = 512
/**默認(rèn)空數(shù)據(jù)**/
private let GZIP_NULL_DATA = Data()

  • 判斷是否zip壓縮后的數(shù)據(jù)。
   public var isGZipCompressed :Bool {
        return self.starts(with: [0x1f,0x8b])
    }
  • gzip壓縮。
public func gzipCompress() -> Data {
        
        guard self.count > 0 else {
            return self
        }
        
        var stream = z_stream()
        stream.avail_in = uInt(self.count)
        stream.total_out = 0
        
        self.withUnsafeBytes { (bytes:UnsafePointer<Bytef>) in
            stream.next_in = UnsafeMutablePointer<Bytef>(mutating:bytes)
        }

        var status = deflateInit2_(&stream,Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY, ZLIB_VERSION, GZIP_STREAM_SIZE)
        
        if  status != Z_OK {
            return  GZIP_NULL_DATA
        }

        var compressedData = Data()
        
        while stream.avail_out == 0 {
            
            if Int(stream.total_out) >= compressedData.count {
                compressedData.count += GZIP_BUF_LENGTH
            }
            
            stream.avail_out = uInt(GZIP_BUF_LENGTH)
            
            compressedData.withUnsafeMutableBytes { (bytes:UnsafeMutablePointer<Bytef>) -> Void in
                stream.next_out = bytes.advanced(by: Int(stream.total_out))
            }
            
            status = deflate(&stream, Z_FINISH)
            
            if status != Z_OK && status != Z_STREAM_END {
                return GZIP_NULL_DATA
            }
        }
        
        guard deflateEnd(&stream) == Z_OK else {
            return GZIP_NULL_DATA
        }
        
        compressedData.count = Int(stream.total_out)
        return compressedData
    }
  • gzip解壓。
    public func gzipUncompress() ->Data {
        guard self.count > 0  else {
            return GZIP_NULL_DATA
        }
        
        guard self.isGZipCompressed else {
            return self
        }
        
        var  stream = z_stream()
      
        self.withUnsafeBytes { (bytes:UnsafePointer<Bytef>) in
            stream.next_in =  UnsafeMutablePointer<Bytef>(mutating: bytes)
        }
        
        stream.avail_in = uInt(self.count)
        stream.total_out = 0
      
        
        var status: Int32 = inflateInit2_(&stream, MAX_WBITS + 16, ZLIB_VERSION,GZIP_STREAM_SIZE)
        
        guard status == Z_OK else {
            return GZIP_NULL_DATA
        }
        
        var decompressed = Data(capacity: self.count * 2)
        while stream.avail_out == 0 {

            stream.avail_out = uInt(GZIP_BUF_LENGTH)
            decompressed.count += GZIP_BUF_LENGTH

            decompressed.withUnsafeMutableBytes { (bytes:UnsafeMutablePointer<Bytef>)in
                 stream.next_out = bytes.advanced(by: Int(stream.total_out))
            }

            status = inflate(&stream, Z_SYNC_FLUSH)

            if status != Z_OK && status != Z_STREAM_END {
                break
            }
        }
        
        if inflateEnd(&stream) != Z_OK {
            return GZIP_NULL_DATA
        }
        
        decompressed.count = Int(stream.total_out)
        return decompressed
    }

  • 測(cè)試
        let string = "Hello world!"
        let helloData =  string.data(using: .utf8)
        let compressData =   helloData?.gzipCompress()
        let uncompressData =  compressData?.gzipUncompress()
        let resString =  String(data: uncompressData!, encoding: String.Encoding.utf8)
        print(resString)
        
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • WinRAR - 最新版本的更新 版本 5.50 1. WinRAR 和命令行 RAR 默認(rèn)使用 RAR ...
    王舒璇閱讀 2,509評(píng)論 0 2
  • 文 | 壹默了然 這是新滬族系列第6個(gè)故事 導(dǎo)讀:一對(duì)校園戀人,畢業(yè)后各奔前程,愛(ài)情被殘酷的生活打敗。7年后,女方...
    壹默了然閱讀 977評(píng)論 3 1
  • 經(jīng)過(guò)我的實(shí)踐 現(xiàn)實(shí)真的比想象要麻煩的多
    賈館長(zhǎng)閱讀 178評(píng)論 0 0
  • 我媽媽老王是一個(gè)做事雷厲風(fēng)行,心思敏感又粗獷的女王,家里一切她說(shuō)了算。當(dāng)然,她只能算上我爸,我和哥哥都只聽(tīng)不算。 ...
    懸崖上的小樹(shù)閱讀 751評(píng)論 21 23
  • 在剛進(jìn)大學(xué)的時(shí)候,聽(tīng)到班主任說(shuō)的最多的就是考研,考研這個(gè)想法就像在心里發(fā)了芽。 但是,進(jìn)入大學(xué)的...
    綠蘿囧了閱讀 117評(píng)論 0 1

友情鏈接更多精彩內(nèi)容