I am very new to Java.

I have some codes to compress like below:

public void compress(OutputStream out) throws IOException
{
    Deflater deflater = new Deflater(1);
    DeflaterOutputStream zOut = new DeflaterOutputStream(out, deflater, 1024);
    DataOutputStream stream = new DataOutputStream(zOut);

    stream.writeShort(200);
    stream.write("test".getBytes("utf-8"));

    zOut.close();
    deflater.end();
}

And I am using that function as below:

    compress c = new compress();
    FileOutputStream fis = new FileOutputStream("D:\\Temp\\file.bin");
    OutputStream out = fis;
    c.compress(out);
    fis.close(); 

Now, I need to decompress my file.bin file.

I have looked up several samples, but none of them shows me about the compression level.

The constructor of Deflater has 1 argument which is compression level.

Don't I have to mention that when decompressing it?

Anyway, please show me the proper way to decompress this.

Thanks in advance.

有帮助吗?

解决方案

This should give you an idea how to do it using InflaterInputStream:

public static void decompress(File compressed, File raw)
        throws IOException
    {
        InputStream in =
            new InflaterInputStream(new FileInputStream(compressed));
        OutputStream out = new FileOutputStream(raw);
        byte[] buffer = new byte[1000];
        int len;
        while((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top