接收InputStream并将GZIP压缩到给定文件中。
//package com.nowjava; import java.io.*;/* from N o w J a v a . c o m*/ import java.util.zip.GZIPOutputStream; public class Main { public static final int BUFF_SIZE = 2048; /** * Takes in InputStream and compresses it into a given file. This method will not overwrite an existing file. * * @param istream the stream to compress and write out * @param destinationFile the file you want to put the compressed stream into. * @throws IOException if there */ public static void streamToCompressedFile(final InputStream istream, final File destinationFile) throws IOException { if (destinationFile.exists()) { throw new IOException("Refusing to overwrite an existing file."); } OutputStream gzStream = null; InputStream biStream = null; try {//from 时代Java公众号 - nowjava.com gzStream = new GZIPOutputStream(new BufferedOutputStream( new FileOutputStream(destinationFile)), BUFF_SIZE); biStream = new BufferedInputStream(istream, BUFF_SIZE); transferStream(biStream, gzStream); } finally { if (gzStream != null) { gzStream.close(); } if (biStream != null) { biStream.close(); } } } /** * Transfers an InputStream to an OutputStream it is up to the job of the caller to close the streams. * * @param istream * @param ostream * @throws IOException */ protected static void transferStream(final InputStream istream, final