Zlib压缩在Java中使用Deflate和Inflate类 [英] Zlib compression Using Deflate and Inflate classes in Java

查看:2351
本文介绍了Zlib压缩在Java中使用Deflate和Inflate类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在zlib压缩中使用java.util.zip中的Deflate和Inflate类。

I want trying to use the Deflate and Inflate classes in java.util.zip for zlib compression.

我可以使用Deflate压缩代码,但解压缩时,我遇到此错误 -

I am able to compress the code using Deflate, but while decompressing, I am having this error -

Exception in thread "main" java.util.zip.DataFormatException: unknown compression method
    at java.util.zip.Inflater.inflateBytes(Native Method)
    at java.util.zip.Inflater.inflate(Inflater.java:238)
    at java.util.zip.Inflater.inflate(Inflater.java:256)
    at zlibCompression.main(zlibCompression.java:53)

这里是我的代码到目前为止 -

Here is my code so far -

import java.util.zip.*;
import java.io.*;

public class zlibCompression {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException, DataFormatException {
        // TODO Auto-generated method stub

        String fname = "book1";
        FileReader infile = new FileReader(fname);
        BufferedReader in = new BufferedReader(infile);

        FileOutputStream out = new FileOutputStream("book1out.dfl");
        //BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));

        Deflater compress = new Deflater();
        Inflater decompress = new Inflater();

        String readFile = in.readLine();
        byte[] bx = readFile.getBytes();

        while(readFile!=null){
            byte[] input = readFile.getBytes();
            byte[] compressedData = new byte[1024];
            compress.setInput(input);
            compress.finish();
            int compressLength = compress.deflate(compressedData, 0, compressedData.length);
            //System.out.println(compressedData);
            out.write(compressedData, 0, compressLength);
            readFile = in.readLine();
        }

        File abc = new File("book1out.dfl");
        InputStream is = new FileInputStream("book1out.dfl");

        InflaterInputStream infl = new InflaterInputStream(new FileInputStream("book1out.dfl"), new Inflater());
        FileOutputStream outFile = new FileOutputStream("decompressed.txt");

        byte[] b = new byte[1024];
        while(true){

            int a = infl.read(b,0,1024);
            if(a==0)
                break;

            decompress.setInput(b);
            byte[] fresult = new byte[1024];
            //decompress.in
            int resLength = decompress.inflate(fresult);
            //outFile.write(b,0,1);
            //String outt = new String(fresult, 0, resLength);
            //System.out.println(outt);
        }

        System.out.println("complete");

    }
}


推荐答案

你想在这里做什么?您使用InflaterInputStream解压缩您的数据,然后尝试将这个解压数据再次传递给Inflater?

What are you trying to do here? You use an InflaterInputStream, which decompresses your data, and then you try to pass this decompressed data again to an Inflater? Use either one of them, but not both.

这是导致您例外的原因。

This is what is causing your exception here.

除此之外,还有一些小错误,像bestsss提到的:

In addition to this, there are quite some minor errors, like these mentioned by bestsss:


  • 在完成后完成压缩,不能再添加任何数据。

  • 您不检查deflate过程产生的输出量。如果你有长的行,它可以超过1024字节。

  • 您设置输入到Inflater,而不设置长度 a

  • You finish the compression in the loop - after finishing, no more data can be added.
  • You don't check how much output the deflate process produces. If you have long lines, it could be more than 1024 bytes.
  • You set input to the Inflater without setting the length a, too.

我发现了更多:


  • 您不要在写入后(以及从同一个文件读取之前)关闭FileOutputStream。

  • 您使用 readLine()读取一行文本,但不再添加换行符,这意味着在解压缩文件中不会有任何换行符。

  • 将字节转换为


  • You don't close your FileOutputStream after writing (and before reading from the same file).
  • You use readLine() to read a line of text, but then you don't add the line break again, which means in your decompressed file won't be any line breaks.
  • You convert from bytes to string and to bytes again without any need.
  • You create variables which you don't use later on.

我不会尝试更正你的程序。这里是一个简单的一个,我做你想要的,使用DeflaterOutputStream和InflaterInputStream。 (您也可以使用JZlib的ZInputStream和ZOutputStream。)

I won't try to correct your program. Here is a simple one which does what I think you want, using DeflaterOutputStream and InflaterInputStream. (You could also use JZlib's ZInputStream and ZOutputStream instead.)

import java.util.zip.*;
import java.io.*;

/**
 * Example program to demonstrate how to use zlib compression with
 * Java.
 * Inspired by http://stackoverflow.com/q/6173920/600500.
 */
public class ZlibCompression {

    /**
     * Compresses a file with zlib compression.
     */
    public static void compressFile(File raw, File compressed)
        throws IOException
    {
        InputStream in = new FileInputStream(raw);
        OutputStream out =
            new DeflaterOutputStream(new FileOutputStream(compressed));
        shovelInToOut(in, out);
        in.close();
        out.close();
    }

    /**
     * Decompresses a zlib compressed file.
     */
    public static void decompressFile(File compressed, File raw)
        throws IOException
    {
        InputStream in =
            new InflaterInputStream(new FileInputStream(compressed));
        OutputStream out = new FileOutputStream(raw);
        shovelInToOut(in, out);
        in.close();
        out.close();
    }

    /**
     * Shovels all data from an input stream to an output stream.
     */
    private static void shovelInToOut(InputStream in, OutputStream out)
        throws IOException
    {
        byte[] buffer = new byte[1000];
        int len;
        while((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    }


    /**
     * Main method to test it all.
     */
    public static void main(String[] args) throws IOException, DataFormatException {
        File compressed = new File("book1out.dfl");
        compressFile(new File("book1"), compressed);
        decompressFile(compressed, new File("decompressed.txt"));
    }
}

为了提高效率,带有缓冲流的文件流。如果这是性能关键,测量它。

For more efficiency, it might be useful to wrap the file streams with buffered streams. If this is performance critical, measure it.

这篇关于Zlib压缩在Java中使用Deflate和Inflate类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆