Is Android InflaterInputStream the same as the popular Windows ZLIB library?

I am trying to unpack data that was compressed using the ZLIB library, written by Jean-Luir Gaylli back in the 1990s. I think this is a popular library (I see a lot of programs that send the zlib32.dll file that it uses), so I hope someone will be familiar enough with it to help me. I directly use the compress () function, which from what I'm reading uses the rfc-1951 DEFLATE format.

Here is a code snippet that I use to read compressed data from a stream and unzip it:

InputStream is = new ByteArrayInputStream(buf); //GZIPInputStream gzis = new GZIPInputStream(is); InflaterInputStream iis = new InflaterInputStream(is); byte[] buf2 = new byte[uncompressedDataLength]; iis.read(buf2); 

The iis.read (buf2) function throws an internal data format error exception. I also tried using GZIPInputStream, but this also throws the same exception.

The buf variable is a byte type [], and I confirmed with debugging that it is the same as my C program returned from the ZLIB compress () function (the actual data comes from the server via TCP). "uncompressedDataLength" is the known size of uncompressed data, which was also provided by the C program (server).

Has anyone tried to read / write data using this library and then read and write the same data on Android using Java?

I found the “pure Java ZLIB port” mentioned in several places, and if I need, I can try this, but I would prefer to use the built-in / OS functions if possible.

+4
source share
1 answer

The deflate, zlib, and gzip data formats in the game are related here.

  • The base is the deflate compressed data format defined in RFC 1951 . Since it is often completely useless in its pure form, we usually use the packaging format around it.

  • The gzip compressed data format ( RFC 1952 ) is designed to compress files. It consists of a header, in which there is a place for the file name and some attributes, a stream with data deflation and a CRC-32 checksum (4 bytes) at the end. (There is also support for several of these files in a single stream in the specification, but I think this is not used that often.)

  • The zlib compressed data format defined in RFC 1950 : it consists of a smaller header (2 or 6 bytes), a stream with deflate transmission and an Adler-32 checksum (4 bytes) at the end. (The Adler-32 checksum is designed to calculate faster than the CRC-32 checksum used in gzip.) Designed for compressed data transfer inside some other protocols or for storing compressed data in other file formats. For example, it is used in PNG file format.

The zlib library supports all of these formats. Java java.util.zip is built on zlib (as part of the implementation / internal calls of VM) and provides access to them with several classes:

  • The Deflater and Inflater classes implement, depending on the argument of the nowrap constructor, either zlib or deflate data formats.

  • DeflaterOutputStream / DeflaterInputStream / InflaterInputStream / InflaterOutputStream are built on Deflater / Inflater. The documentation does not indicate whether Inflater / Deflater really implements zlib or deflate by default, but the source shows that it uses the default Deflater or Inflater that implements zlib.

  • GZipOutputStream / GZipInputStream implements, as the name implies, the gzip format.

I looked at the source code of the zlib compress function and it seems to use the zlib format. Therefore, your code must do the right thing. Make sure that there is no missing data or additional data that is not part of the compressed data block before or after it.

Disclaimer: This is a condition for Java SE, I believe it is similar to Android, but I can not guarantee it.

The jzlib library you found (I suppose), which is an implementation of Java zlib, also implements all of these data formats (gzip was added in the latest update). For interactive use (on the compression side), this is preferable, since it allows for some cleaning actions that are not possible for the java.util classes (other than using some workaround, such as changing the compression level), and can also be faster, since it avoids (which always have some overhead).

PS: The zip (or pkzip) file format is also related: it uses internal deflation for each file inside the archive.

+7
source

Source: https://habr.com/ru/post/1380863/


All Articles