native zlib inflate/deflate for swift3 on iOS

前端 未结 3 1051
耶瑟儿~
耶瑟儿~ 2020-12-21 04:11

I\'d like to be able to inflate/deflate Swift3 Data structs. I found GzipSwift, but it\'s not clear how I make that available to my iOS app. The naive things I\'ve tried inc

相关标签:
3条回答
  • 2020-12-21 04:40

    I maintain a small Swift 3+ wrapper around Apples native libcompression framework at:

    https://github.com/mw99/DataCompression

    Usage example for gzip:

    let data: Data! = "https://www.ietf.org/rfc/rfc1952.txt".data(using: .utf8)
    let gzipped: Data! = data.zip()
    let gunzipped: Data? = gzipped.unzip()
    assert(data == gunzipped)
    

    But if you are only interested in classic inflate and deflate you may use the .inflate() and .deflate() methods instead. That will save 18 bytes because the gzip header won't be added.

    0 讨论(0)
  • 2020-12-21 04:48

    I just recently had to add that exact library and file to my project, and after a lot of troubleshooting finally got it working, so let me walk you through the steps!

    Okay

    1) Go to the top level directory of your project in finder, and create a new folder called Swiftzlib or whatever you want the name of the module that you will be importing to be. (What we will do is add the zlib library as a module, so think of it as importing Foundation or some such other module). To clarify, this Swiftzlib directory will end up as a child directory of the same directory that contains your *.xcodeproj and *.xcworkspace files.

    2) Inside the folder you created, make two files.

    • include.h
    • module.modulemap

    3) In your include.h file, enter the following:

    #include<zlib.h>
    

    4) In your module.modulemap file, enter the following:

    module Swiftzlib [system] {
        header "include.h"
        export *
    }
    

    Where Swiftzlib is the same as the name of the folder that you created.

    5) Open your Xcode project, and select your target

    • 5a) In Build Phases -> Link Binary with Libraries, add libz.tbd
    • 5b) In Build Settings -> Swift Compiler - Search Paths, add $(PROJECT_DIR)/Swiftzlib non-recursively to the import paths
    • 5c) In Build Settings -> Other Linker Flags, add -lz as a flag

    6) Select your project in Xcode (may not be necessary, but I've done it in my project and it works)

    • 6a) In Build Settings -> Swift Compiler - Search Paths, add $(PROJECT_DIR)/Swiftzlib non-recursively to the import paths

    7) In Data+Gzip.swfit, add import Swiftzlib to the top of the file

    8) Clean, Build, and Run!

    0 讨论(0)
  • 2020-12-21 04:51

    Swift 5 implementation using Compression.

    Took me a few days to realise I had to drop the first 2 bytes of the compressed data. Hope it can help somebody else.

    import Foundation
    import Compression
    
    func decompress(_ data: Data) -> String {
        let size = 8_000_000
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
        let result = data.subdata(in: 2 ..< data.count).withUnsafeBytes ({
            let read = compression_decode_buffer(buffer, size, $0.baseAddress!.bindMemory(to: UInt8.self, capacity: 1),
                                                 data.count - 2, nil, COMPRESSION_ZLIB)
            return String(decoding: Data(bytes: buffer, count:read), as: UTF8.self)
        }) as String
        buffer.deallocate()
        return result
    }
    
    0 讨论(0)
提交回复
热议问题