Created
July 5, 2018 16:46
-
-
Save ehrmann/ca4454924e4fcf88c0a76c0b3752194b to your computer and use it in GitHub Desktop.
Java pngtastic zopfli compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public byte[] gzipData(byte[] blob) { | |
Buffer compressedData = zopfli.compress(new Options(Options.BlockSplitting.FIRST, 15), blob); | |
ByteBuffer gzipBuffer = ByteBuffer.allocate(compressedData.getSize() - 6 + 18); | |
gzipBuffer.order(ByteOrder.LITTLE_ENDIAN); | |
gzipBuffer.put((byte) 0x1f); | |
gzipBuffer.put((byte) 0x8b); | |
gzipBuffer.put((byte) 8); | |
gzipBuffer.put((byte) 0); | |
gzipBuffer.putInt(0); | |
gzipBuffer.put((byte) 2); | |
gzipBuffer.put((byte) 0xff); | |
gzipBuffer.put(compressedData.getData(), 2, compressedData.getSize() - 6); | |
CRC32 crc32 = new CRC32(); | |
crc32.update(blob); | |
gzipBuffer.putInt((int) crc32.getValue()); | |
gzipBuffer.putInt((int) (((long) blob.length) % (1L << 32))); | |
return gzipBuffer.array(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment