I trying to compress and decompress string using lzma
Decompress process goes without any errors, after that if I take compressed data and try to decompress it back, as it is, all good
But I need to save compressed date to file, so when app is running I can get string and decompress it at runtime, this is where problems begins. I get exactly string, that I got from compressed process before, but at decompress I got error that says “Decompression error: Error Domain=NSCocoaErrorDomain Code=5377 “(null)”
My compress process
var dataForCompress = try String(contentsOfFile: filepath)
let data = dataForCompress.data(using: .utf8)! as NSData
let compressedData = try data.compressed(using: .lzma)
After that I tried to decompress string by convert compressedData to string and decompress this string like that
var dataString = String(decoding: compressedData, as: UTF8.self)
let nsdata = dataString.data(using: .utf8)! as NSData
let uncompressedData = try nsdata.decompressed(using: .lzma) as Data
let theString = String(data: uncompressedData, encoding: .utf8)
At that point I got error
If I just do this
let uncompressedData = try compressedData.decompressed(using: .lzma) as Data
Uncompress compressed date straight forward, its decompressed without error
I think problem is somewhere at my process with get data from string, because size of the string I get when convert data to string is much more than size of compressed data itself
Buka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1