2

I'm compressing random data using NSMutableData (in Swift) as follows:

import Foundation

let n = 1024
let numbers = (0..<n).map { _ in UInt8.random(in: 0...1) }

print("generated random data: \(numbers)")

let data = Data(numbers)
let mutable = NSMutableData(data: data)
try! mutable.compress(using: .lz4)

print("compressed size: \(mutable.count)")'

try! mutable.write(toFile: "compressed.dat", options: [])

And reading into python using:

import os
import lz4.frame

fh = open('compressed.dat', 'rb')
ba = bytearray(fh.read())

print("read %d bytes" % len(ba))

decompressed = lz4.frame.decompress(ba)

print("decompressed to %d bytes" % len(decompressed))

When calling lz4.frame.decompress, I get:

RuntimeError: LZ4F_getFrameInfo failed with code: ERROR_frameType_unknown

trying lz4.block.decompress, I get:

_block.LZ4BlockError: Decompression failed: corrupt input or insufficient space in destination buffer. Error code: 4

What am I doing wrong?

Taylor
  • 5,871
  • 2
  • 30
  • 64
  • 1
    I had a look into this starting with confirming I could decompress the stream back in to a Data in Swift. I don't think you're doing anything wrong, I think the Apple implementation of LZ4 differs from Python. The Apple docs https://developer.apple.com/documentation/compression/algorithm/lz4 for their LZ4 implementation mentions the addition of 'a very simple frame to the raw stream to allow some additional validation and functionality', additionally the LZ4 homepage lists interoperable ports that include Python, but not Apple https://lz4.github.io/lz4/ Hope you find a simple solution to this – Toby May 31 '23 at 00:02
  • Forget about using Apple data compression. There is many pods out there that would probably work. Try this one https://github.com/marmelroy/Zip – Leo Dabus May 31 '23 at 03:22

0 Answers0