gzip With Len - Java File Path IO

Java examples for File Path IO:GZIP

Description

gzip With Len

Demo Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Main{
    public static byte[] gzipWithLen(byte[] bytes) throws IOException {
        ByteArrayOutputStream baOut = null;
        GZIPOutputStream gzOut = null;
        try {//from  ww w . j  ava2  s . com
            baOut = new ByteArrayOutputStream();
            baOut.write(EncodeUtils.int2ByteArray(bytes.length), 0, 4);
            gzOut = new GZIPOutputStream(baOut);
            gzOut.write(bytes);
            gzOut.finish();
            gzOut.flush();
            return baOut.toByteArray();
        } finally {
            closeQuietly(gzOut);
            closeQuietly(baOut);
        }
    }
    public static void closeQuietly(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related Tutorials