I try to decompress Data with zlib
.
I cut a slice from a array of UInt8
, which is compressed with zlib and try to decompress it.
Unfortunately it fails and I get a Error Domain=NSCocoaErrorDomain Code=5377 "(null)"
. I have no clue how to debug this.
I checked the header for compatibility and it is: 0x789c
which seems right as statet from mwfearnley on stackoverflow.
if isCompressed {
let origDataArray: [UInt8] = binary.data
let arraySlice: [UInt8] = Array(origDataArray[offset..<(offset + length)])
let newData: Data = Data(arraySlice)
let nsData: NSData = NSData(data: newData)
print(nsData)
// prints {length = 77, bytes = 0x789c6360 66fcc538 81819581 81a98b29 ... 00421e00 2d700cf5 }
do {
let dataDecompressed = try nsData.decompressed(using: .zlib)
} catch {
print("failed: ", error)
}
}
I’ve done a compression test like this:
do {
let dataTest = NSData(data: Data([0x8b, 0xaf, 0x19, 0x9b, 0xf1, 0xaa]))
let compressed = try dataTest.compressed(using: .zlib)
let decompressed = try compressed.decompressed(using: .zlib)
print(compressed, decompressed)
} catch {
print(error)
}
And I got the following:
{length = 8, bytes = 0xeb5e2f39fbe32a00} {length = 6, bytes = 0x8baf199bf1aa}
In the compressed print (the first) there is no such header.
I think I miss something very fundamental..
EDIT:
Its working with SWCompression Library though.. weird
EDIT2:
Got it working.
Had to cut the first two header bytes away.
// Instead of
let arraySlice: [UInt8] = Array(origDataArray[offset..<(offset + length)])
// do
let arraySlice: [UInt8] = Array(origDataArray[offset+2..<(offset + length)])