How to make Apache mod_deflate and Transfer-encoding : Chunked work together?

允我心安 提交于 2019-12-03 16:21:24

Actually I found the solution. I used to create a new object of GZipOutputStream each time to flush different chunks. Instead you should create one object only of GZipOutputStream and then used that object for compressing all the chunks of the response. Also I put a wrapper around GZipOutputStream. Here is the wrapper that I got from googling around.

public class GZIPFlushableOutputStream extends GZIPOutputStream {

    public GZIPFlushableOutputStream(final OutputStream out) throws IOException {
        // Using Deflater with nowrap == true will ommit headers and trailers
        super(out);
    }

    private static final byte[] EMPTYBYTEARRAY = new byte[0];

    /**
     * Insure all remaining data will be output.
     */
    public void flush() throws IOException {
        /**
         * Now this is tricky: We force the Deflater to flush its data by
         * switching compression level. As yet, a perplexingly simple workaround
         * for
         * 
         * http://developer.java.sun.com/developer/bugParade/bugs/42557 43.html
         */
        def.setInput(EMPTYBYTEARRAY, 0, 0);

        def.setLevel(Deflater.NO_COMPRESSION);
        deflate();

        def.setLevel(Deflater.DEFAULT_COMPRESSION);
        deflate();

        out.flush();
    }
}

My understanding is that you need the "whole" file in order to compress it. You can either send it out in chunks or send it compressed. The mod_gzip_dechunk option does not appear to exist any more - see mod_deflate documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!