brendan-duncan/archive

Compatibility with Java Deflater

Closed this issue · 1 comments

Gieted commented

It looks like data compressed using Java's built-in Deflater cannot be uncompressed using archive's Inflate.

In JVM/Kotlin:

fun deflate(input: ByteArray): ByteArray {
    val outputStream = ByteArrayOutputStream()
    DeflaterOutputStream(outputStream, Deflater(Deflater.BEST_COMPRESSION)).use {
        it.write(input)
        it.finish()
    }
    
    return outputStream.toByteArray()
}

fun main() {
    val compressed = deflate("test".toByteArray())
    val base64 = Base64.getUrlEncoder().encodeToString(compressed)
    println(base64)
}

Then in Dart:

void main() {
  const compressed = "eNorSS0uAQAEXQHB";
  final decoded = base64Url.decode(compressed);
  final decompressed = Inflate(decoded).getBytes(); // returns and empty list
  final decompressedString = utf8.decode(decompressed);
  print(decompressedString);
}

This seems weird as both are supposed to use the same "deflate" algorithm.

Gieted commented

Never mind, you just need to set nowrap parameter to true, and then it works:

fun deflate(input: ByteArray): ByteArray {
    val outputStream = ByteArrayOutputStream()
    DeflaterOutputStream(outputStream, Deflater(Deflater.BEST_COMPRESSION, true)).use {
        it.write(input)
        it.finish()
    }
    
    return outputStream.toByteArray()
}