/** * Copyright (c) 2011 Mark S. Kolich * http://mark.koli.ch * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ package com.kolich.util.compression; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class GzipCompressor { private static final String UTF_8 = "UTF-8"; private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; /** * Given an uncompressed InputStream, compress it and return the * result as new byte array. * * @throws IOException */ public static final byte[] compress(final InputStream is) throws IOException { GZIPOutputStream gzos = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); gzos = new GZIPOutputStream(baos); copy(is, gzos); gzos.finish(); return baos.toByteArray(); } finally { closeQuietly(gzos); } } /** * Given a GZIP'ed compressed InputStream, uncompresses it and returns * the result as new byte array. Does NOT close the InputStream; it's * up to the caller to close the InputStream when necessary. * * @throws IOException */ public static final byte[] uncompress(final InputStream is) throws IOException { GZIPInputStream gzis = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); gzis = new GZIPInputStream(is); copy(gzis, baos); return baos.toByteArray(); } finally { closeQuietly(gzis); } } /** * Given an uncompressed byte-array, compress it using GZIP * compression and return the compressed array. * * @throws IOException */ public static final byte[] compress(final byte[] input) throws IOException { return compress(new ByteArrayInputStream(input)); } /** * Given a GZIP'ed compressed byte array, uncompresses it and * returns the result as new byte array. * * @throws IOException */ public static final byte[] uncompress(final byte[] input) throws IOException { return uncompress(new ByteArrayInputStream(input)); } private static long copy(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; // 4K buffer long count = 0; int n = 0; while(-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; } private static void closeQuietly(final OutputStream output) { try { if(output != null) { output.close(); } } catch(IOException i) { // ignore any IOException's, this is closeQuietly } } private static void closeQuietly(final InputStream input) { try { if(input != null) { input.close(); } } catch(IOException i) { // ignore any IOException's, this is closeQuietly } } public static void main(String[] args) throws Exception { // Note that this HTML is too tiny, probably won't compress well at all. // In fact, the "compressed" result may actually be larger in size than // the uncompressed original String. final String html = "