简体   繁体   English

如何在Java中的大文件上使用Deflater和Inflater?

[英]How to use Deflater and Inflater on large files in java?

I want to use Deflater and Inflater (NOT DeflaterOutputStream and InflaterInputStream ) to compress files. 我想使用DeflaterInflater (不是DeflaterOutputStreamInflaterInputStream )来压缩文件。 The problem is that deflater stops working after mentioned buffer size in this case is 1024. I am using the following code: 问题是在这种情况下,缓冲区大小为1024后,deflater停止工作。我正在使用以下代码:

public class CompressionUtils {

    static String deflateInput = "pic.jpg";
    static String deflateOutput = "picDeflate.raw";
    static String inflateOutput = "picInflate.jpg";

    public static void compress() throws IOException {
        Deflater deflater = new Deflater();
        byte[] data = new byte[1024];
        FileInputStream in = new FileInputStream(new File(deflateInput));
        FileOutputStream out = new FileOutputStream(new File(deflateOutput));

        long readBytes = 0;
        while ((readBytes = in.read(data, 0, 1024)) != -1) {
            deflater.setInput(data);
            deflater.finish();
            byte[] buffer = new byte[1024];
            while (!deflater.finished()) {
                int count = deflater.deflate(buffer); // returns the generated code... index  
                out.write(buffer, 0, count);
            }
        }
    }

    public static void decompress() throws IOException, DataFormatException {
        Inflater inflater = new Inflater();
        byte[] data = new byte[1024];
        FileInputStream in = new FileInputStream(new File(deflateOutput));
        FileOutputStream out = new FileOutputStream(new File(inflateOutput));
        long readBytesCount = 0;
        long readCompressedBytesCount = 0;
        long readBytes = 0;
        while ((readBytes = in.read(data, 0, 1024)) != -1) {
            readBytesCount = readBytesCount + readBytes;
            inflater.setInput(data);
            byte[] buffer = new byte[1024];
            while (!inflater.finished()) {
                int count = inflater.inflate(buffer);
                System.out.println("Remaining: " + inflater.getRemaining());
                out.write(buffer, 0, count);
            }
        }
        System.out.println("readBytesCount: " + readBytesCount);
    }

    public static void main(String[] args) {
        System.out.println("Operation started");
        try {
            compress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Operation ended");

    }
}

And this is output (in windows) of dir: 这是dir的输出(在Windows中):

01-04-2018  16:52           220,173 pic.jpg
28-04-2018  20:50               943 picDeflate.raw
28-04-2018  20:28             1,024 picInflate.jpg

Why does the compress code stops after reading 1024 bytes? 为什么读取1024个字节后压缩代码停止?

finish() is only for when you're finished. finish()仅适用于完成时。 It is the last thing called after you have provided all of the input data to the object. 将所有输入数据提供给对象后,这是最后一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM