-
Hello, how can I compress like I need exactly this compression algorithm to be compatible with external specification / programs. thx |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sorry for the long response time. Zlib (or actually Deflate) compression levels are not standardized, see e.g. Zlib Specification. This is mostly because compression, compared to decompression, is more of an art. You can employ many different techniques and heuristics to get different tradeoffs between compression ratio and time. This is even true for "simpler" algorithms such Deflate/Zlib, as you can choose how you search for subdata matches, do you use static Huffman trees or dynamic ones, how do you construct the dynamic trees, etc. As such, compression levels remain an implementation detail and different implementations may define the exact specifics of them in different ways. For example, I do not know what does level 1 of Note, however, that the output format does not depend on the level and that every specification-compliant decompressor must be able to decompress data with any specified level of compression. If you observe, that the output produced by SWCompression is rejected by other implementations as malformed, please report this as an issue, because this may warrant a further investigation. To answer your direct question, to perform Zlib compression, do the following: let data = ... // data that you want to compress
let compressedData = ZlibArchive.archive(data: data) As I mentioned above, this produces data that declares that it was formally compressed with level 3. If this is an issue, a hacky workaround is the following: let data = ... // data that you want to compress
var compressedData = ZlibArchive.archive(data: data)
compressedData[1] = 0x5E The last line manually changes the declared compression level to 1. P.S. A simpler explanation: a compression level is not observable from the compressed data alone, it can only be detected from a certain metadata field, which in fact can be set arbitrarily (from 0 to 3). |
Beta Was this translation helpful? Give feedback.
Sorry for the long response time.
Zlib (or actually Deflate) compression levels are not standardized, see e.g. Zlib Specification. This is mostly because compression, compared to decompression, is more of an art. You can employ many different techniques and heuristics to get different tradeoffs between compression ratio and time. This is even true for "simpler" algorithms such Deflate/Zlib, as you can choose how you search for subdata matches, do you use static Huffman trees or dynamic ones, how do you construct the dynamic trees, etc.
As such, compression levels remain an implementation detail and different implementations may define the exact specifics of them in different ways. For exa…