您可以通过将 SwiftCompressor 添加为包依赖项,将其添加到 Xcode 项目中。
https://github.com/sochalewski/SwiftCompressor
输入到包存储库 URL 文本字段中。要使用 CocoaPods 将 SwiftCompressor 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
pod 'SwiftCompressor'
然后,运行以下命令
$ pod install
SwiftCompression 是一个 Data
扩展。 它允许您轻松地压缩/解压缩 Data
对象,如下所示
// Create NSData from file
let path = URL(fileURLWithPath: Bundle.main.path(forResource: "lorem", ofType: "txt")!)
let loremData = try? Data(contentsOf: path)
// Compress and then decompress it!
let compressedData = try? loremData?.compress()
let decompressedData = try? compressedData?.decompress()
// You can also choose one of four algorithms and set a buffer size if you want.
// Available algorithms are LZFSE, LZMA, ZLIB and LZ4.
// Compression without parameters uses LZFSE algorithm. Default buffer size is 4096 bytes.
let compressWithLZ4 = try? loremData?.compress(algorithm: .lz4)
let compressWithLZMAReallyBigBuffer = try? loremData?.compress(algorithm: .lzma, bufferSize: 65_536)
Piotr Sochalewski, sochalewski.github.io
SwiftCompressor 在 MIT 许可证下可用。 有关更多信息,请参阅 LICENSE 文件。