★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ ) ➤GitHub地址:https://github.com/strengthen/LeetCode ➤原文地址:https://www.cnblogs.com/strengthen/p/10844466.html ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。 ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创! ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
热烈欢迎,请直接点击!!!
进入博主App Store主页,下载使用各个作品!!!
注:博主将坚持每月上线一个新app!!!
一、DataCompression
定义:Swift libcompression包装器作为数据类型的扩展(GZIP,ZLIB,LZFSE,LZMA,LZ4,deflate,RFC-1950,RFC-1951,RFC-1952)
该Data 类型的libcompression包装器扩展
支持的压缩算法包括:
测试所有算法并比较压缩比
1 let raw: Data! = String(repeating: "There is no place like 127.0.0.1", count: 25).data(using: .utf8)
2
3 print("raw => \(raw.count) bytes")
4
5 for algo: Data.CompressionAlgorithm in [.zlib, .lzfse, .lz4, .lzma] {
6 let compressedData: Data! = raw.compress(withAlgorithm: algo)
7
8 let ratio = Double(raw.count) / Double(compressedData.count)
9 print("\(algo) => \(compressedData.count) bytes, ratio: \(ratio)")
10
11 assert(compressedData.decompress(withAlgorithm: algo)! == raw)
12 }
输出打印:
1 raw => 800 bytes
2 zlib => 40 bytes, ratio: 20.00
3 lzfse => 69 bytes, ratio: 11.59
4 lz4 => 181 bytes, ratio: 4.42
5 lzma => 100 bytes, ratio: 8.00
RFC-1951)也可以用快捷键使用.deflate() 和.inflate()
1 let data: Data! = "https://www.ietf.org/rfc/rfc1951.txt".data(using: .utf8)
2 let deflated: Data! = data.deflate()
3 let inflated: Data? = deflated?.inflate()
4 assert(data == inflated)
RFC-1952)可以用.gzip() 和处理.gunzip()
1 let data: Data! = "https://www.ietf.org/rfc/rfc1952.txt".data(using: .utf8)
2 let gzipped: Data! = data.gzip()
3 let gunzipped: Data? = gzipped.gunzip()
4 assert(data == gunzipped)
注意:gzip格式的压缩数据总是比原始放气数据大18个字节,并将附加/执行基于crc32校验和的数据完整性测试。
RFC-1950)可以用.zip() 和处理.unzip()
1 let data: Data! = "https://www.ietf.org/rfc/rfc1950.txt".data(using: .utf8)
2 let zipped: Data! = data.zip()
3 let unzipped: Data? = zipped.unzip()
4 assert(data == unzipped)
注意:压缩格式的压缩数据总是比原始缩减数据大6个字节,并将附加/执行基于adler32校验和的数据完整性测试。
在命令行上压缩文件并在Swift中解压缩
最简单的方法是使用已安装的gzip命令行工具。假设你有一个名为file.txt的文件,在调用之后
该文件应该已经被压缩到file.txt.gz。您现在可以使用以下命令加载和解压缩文件的内容:
1 let compressedData = try? Data(contentsOf: URL(fileURLWithPath: "/path/to/your/file.txt.gz"))
2
3 if let uncompressedData = compressedData?.gunzip() {
4 print(String(data: uncompressedData, encoding: .utf8) ?? "Can't decode UTF-8")
5 }
校验和扩展
与压缩无关但为方便起见,Crc32和Adler32方法也暴露在Data 可能派上用场的类型上。
1 let classicExample = "The quick brown fox jumps over the lazy dog".data(using: .utf8)!
2 let crc32 = classicExample.crc32()
3 let adler32 = classicExample.adler32()
4 print("crc32: \(crc32), adler32: \(adler32)")
将打印:
crc32: 414fa339, adler32: 5bdc0fda
安装:
使用示例:
1 import Gzip
2
3 // gzip
4 let compressedData: Data = try! data.gzipped()
5 let optimizedData: Data = try! data.gzipped(level: .bestCompression)
6
7 // gunzip
8 let decompressedData: Data
9 if data.isGzipped {
10 decompressedData = try! data.gunzipped()
11 } else {
12 decompressedData = data
13 }
GZip字符串压缩和解压缩:
1 //MARK:字符串GZip压缩
2 func compress(_ str:String) -> String
3 {
4 //使用base64编码,转换成Data类型
5 if let data:Data = str.data(using: String.Encoding.utf8)
6 {
7 //GZip压缩
8 let gzipped:Data! = data.gzip()
9 //Data转换为String
10 return gzipped.base64EncodedString()
11 }
12 return String()
13 }
14
15 //MARK:字符串GZip解压缩
16 func decompress(_ str:String) -> String
17 {
18 //使用base64编码,转换成Data类型,GZip解压缩
19 if let data:Data = Data(base64Encoded: str),let gunzipped = data.gunzip()
20 {
21 //Data类型转换为String
22 return String(data: gunzipped , encoding: String.Encoding.utf8) ?? String()
23 }
24 return String()
25 }
二、GzipSwift
定义:GzipSwift是一个带有Swift编写的Data扩展的框架。它使用zlib实现压缩/解压缩gzip。
安装:
用法:
1 import Gzip
2
3 // gzip
4 let compressedData: Data = try! data.gzipped()
5 let optimizedData: Data = try! data.gzipped(level: .bestCompression)
6
7 // gunzip
8 let decompressedData: Data
9 if data.isGzipped {
10 decompressedData = try! data.gunzipped()
11 } else {
12 decompressedData = data
13 }
GZip字符串压缩和解压缩:
1 //MARK:字符串GZip压缩
2 func compress(_ str:String) -> String
3 {
4 //使用base64编码,转换成Data类型
5 if let data:Data = str.data(using: String.Encoding.utf8)
6 {
7 //GZip压缩
8 let optimizedData:Data = try! data.gzipped(level: .bestCompression)
9 //Data转换为String
10 return optimizedData.base64EncodedString()
11 }
12 return String()
13 }
14
15 //MARK:字符串GZip解压缩
16 func decompress(_ str:String) -> String
17 {
18 //使用base64编码,转换成Data类型
19 let data:Data = Data(base64Encoded: str) ?? Data()
20 //判断是否为GZip压缩的Data
21 if data.isGzipped {
22 //GZip解压缩
23 let decompressedData: Data = try! data.gunzipped()
24 //Data类型转换为String
25 return String(data: decompressedData , encoding: String.Encoding.utf8) ?? String()
26 }
27 return String()
28 }
|
请发表评论