reading data compressed by NSMutableData using the lz4 library in python

100 Views Asked by At

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?

0

There are 0 best solutions below