Sté*_*uca 5 compression lz4 swift4
我正在尝试使用 Swift 压缩包解压 lz4 压缩的 png 图像,但代码以零大小退出。\n我的代码如下,解压后的文件预计为 240Kb。
\n\n[更新2] \n我在 Apple 的文档中偶然发现了这一点:
\n\n\n\n\n此处记录了该帧,以便您可以轻松包装另一个 LZ4\n 编码器/解码器以在必要时生成/使用相同的数据流。\n LZ4 编码缓冲区是一系列块,每个块\n 以标头开头。共有三种可能的标头:
\n\n压缩块头由八位位组 0x62、0x76、0x34 和 0x31 组成,后跟块表示的解码(明文)数据的字节大小以及编码数据的大小(以字节为单位)。 n 存储在块中。两个大小字段都存储为(可能\n 未对齐的)32 位小端值。压缩块头后面紧跟着实际的 LZ4 编码数据流。
\n\n流标头结束由八位字节 0x62、0x76、0x34 和 0x24 组成,标记 LZ4 帧的结束。除此标头之外,不得写入或读取更多数据。
\n
所以我相应地添加了页眉和页脚并且它有效。但是 \xe2\x80\x94 总是有一个 \'but\'\xe2\x80\x94,在解压缩之前我如何知道“解码(明文)数据的字节大小”?
\n\n[更新1]
\n\n我提供了一个 LZ4 文件供您测试,以及从主包中解压该文件的代码用法。
\n\n使用 sw“LZ4 命令行界面 64 位 v1.8.0,由 Yann Collet”压缩的 LZ4 文件在互联网上广泛使用
\n\nhttps://drive.google.com/file/d/1eQ204LJs_xsHRwJ_jUcl1Up9NFo8monO/view?usp=sharing
\n\nLZ4减压使用
\n\n if let url = Bundle.main.url(forResource: "TestImage300.png", withExtension: "lz4") {\n if let compressed = try? Data(contentsOf: url) {\n if let decompressed = compressed.lz4Decompress() {\n if let image = UIImage(data: decompressed) {\n self.sourceImages.append(image)\n print("Unittest: Decompressed image as \\(image)")\n }\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\nLZ4解压数据扩展代码:
\n\nimport Foundation\nimport Compression\n\nextension Data {\n\n func lz4Decompress() -> Data? {\n let destinationBufferSize: size_t = 480000\n let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destinationBufferSize)\n\n let sourceBufferSize = count\n\n let decompressed = withUnsafeBytes { (sourceBuffer: UnsafePointer<UInt8>) -> Data? in\n\n let size = compression_decode_buffer(\n destinationBuffer, destinationBufferSize,\n sourceBuffer, sourceBufferSize,\n nil, // scratch buffer automatically handled\n COMPRESSION_LZ4\n )\n\n if size == 0 {\n print("Error ")\n return nil\n }\n\n print("Original compressed size: \\(sourceBufferSize) | Decompressed size: \\(size)")\n\n return Data(bytesNoCopy: destinationBuffer, count: size, deallocator: .free)\n }\n return decompressed\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
小智 1
let intArray: [Int8] = [-16, 1, 1, 39, 0, 19, 11, -30, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1, 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, -51, 6, 0, 0, 2, 0, 0, 43, 0, 16, 2, 9, 0, -1, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, -105, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0, 0, 0]
let uintArray = intArray.map { UInt8(bitPattern: $0) }
Run Code Online (Sandbox Code Playgroud)
// 对于可见性,无符号 uintArray 为 [240, 1, 1, 39, 0, 19, 11, 226, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1 , 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, 205, 6, 0, 0, 2, 0, 0, 43 , 0, 16, 2, 9, 0, 255, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0 , 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, 151, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0 , 0, 0]
var encodedData = Data.init(bytes:uintArray)
let decodedCapacity = 205
let decodedDestinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: decodedCapacity)
let decodedData = encodedData.withUnsafeBytes {
(encodedSourceBuffer: UnsafePointer<UInt8>) -> Data? in
let decodedCharCount = compression_decode_buffer(decodedDestinationBuffer,
decodedCapacity,
encodedSourceBuffer,
encodedData.count,
nil,
COMPRESSION_LZ4_RAW)
if decodedCharCount == 0 {
fatalError("Decoding failed.")
}
print("Before: \(encodedSourceBuffer) | After: \(decodedCharCount)")
return Data(bytesNoCopy: decodedDestinationBuffer, count: decodedCharCount, deallocator: .free)
}
Run Code Online (Sandbox Code Playgroud)
如果您没有在压缩中定义标头,则应该使用 COMPRESSION_LZ4_RAW
| 归档时间: |
|
| 查看次数: |
843 次 |
| 最近记录: |