如何在Swift 3中压缩数据? [英] How to compress data in swift 3?

查看:120
本文介绍了如何在Swift 3中压缩数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift 3的文件管理器中有一些文件.我想上传它们,但是当我将它们转换为base 64时,它们的大小将很大!所以我想先压缩数据,然后再将其转换为base 64.

I have some files in file manager in Swift 3. I want to upload them, but when I will convert them into base 64, their size will be huge! so I want to compress the data before converting it into base 64.

这是我的转换代码:

for i in 0...(rows?.count)! - 1 {

   let filePath = filesurl[fileManagerViewController.selectedFileIndex[i]]
        do {
            let fileData = try Data.init(contentsOf: filePath)

            let fileStream:String = fileData.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))

            fileManagerViewController.upupload.append(fileStream)


        } catch {
            print(error.localizedDescription)
        }


        }

我用过

let compressedData = fileData(UF_COMPRESSED)

但这对我来说不起作用,因此请帮助我压缩文件,然后再将其转换为base 64以进行上传.

But that didn't work for me, so please help me compressing files before converting them into base 64 for uploading.

推荐答案

这是用Swift 3编写的libcompression包装器. https://github.com/mw99/SwiftDataCompression

Here's libcompression wrapper written in Swift 3. https://github.com/mw99/SwiftDataCompression

快速libcompression包装器作为数据类型(ZLIB,LZFSE,LZMA,LZ4,deflate,RFC-1950,RFC-1951)的扩展

Swift libcompression wrapper as an extension for the Data type (ZLIB, LZFSE, LZMA, LZ4, deflate, RFC-1950, RFC-1951)

因此您可以像这样压缩数据:

So you can compress your data like that:

let fileData = try Data.init(contentsOf: filePath)
let compressedData = fileData.compress(withAlgorithm: .LZFSE)

这篇关于如何在Swift 3中压缩数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆