Native Swift implementation of DEFLATE (unzip) algorithm [closed]

回眸只為那壹抹淺笑 提交于 2019-12-06 16:19:53

There is a native swift implementation of zlib on github called DeflateSwift

Take a look at the Compression framework introduced by Apple this summer.

I believe it can decompress any ZLIB compressed file (among other things) and should be pretty efficient at that.

Clean Swift 3 wrapper around Apples new libcompression: https://github.com/mw99/SwiftDataCompression

Usage example:

let s = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_0123456789"
var res: Data? = s.data(using: .utf8)

res = res?.deflate()
res = res?.inflate()

res = res?.zip()
res = res?.unzip()

res = res?.compress(withAlgorithm: .LZFSE)
res = res?.decompress(withAlgorithm: .LZFSE)

res = res?.compress(withAlgorithm: .LZ4)
res = res?.decompress(withAlgorithm: .LZ4)

res = res?.compress(withAlgorithm: .LZMA)
res = res?.decompress(withAlgorithm: .LZMA)

res = res?.compress(withAlgorithm: .ZLIB)
res = res?.decompress(withAlgorithm: .ZLIB)

assert(res != nil)
let t = String(data: res!, encoding: .utf8)
assert(s == t)

Trying this in the playground is quite interesting because by changing the input string you can see immediately see the compression ratio.

"zipped up" implies the .zip format, which is not the same as saying it has been compressed with deflate. The .zip format is an archive wrapper around compressed data formats that permits the compression and storage of files and directory structures, along with attributes, in a single .zip file. The compressed data formats include deflate (which is the most commmon).

So your deflate compressed data is likely not wrapped in the .zip file format.

Deflate compressed data can be found wrapped in gzip (.gz) files, PDF files, PNG files, and other formats.

In any case, you can use zlib to decompress the raw deflate format, however it may be wrapped, or not, in your data. zlib is already present in OS X and iOS as a shared library. As noted by @MirekE, you can access it from the new Compression Framework.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!