Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream GZIPOutputStream.

Prototype

public GZIPOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

From source file:net.orpiske.tcs.utils.compression.Compressor.java

/**
 * Compress a string in GZIP format/*from   ww  w  .j  a  v a2  s. com*/
 * @param text the string to compress
 * @return An array of compressed bytes
 * @throws IOException if unable to compress it
 */
public static byte[] compress(final String text) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = null;

    try {
        gzipOutputStream = new GZIPOutputStream(outputStream);
        gzipOutputStream.write(text.getBytes());
        gzipOutputStream.close();

        return outputStream.toByteArray();
    } finally {
        IOUtils.closeQuietly(gzipOutputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:fr.dutra.confluence2wordpress.util.CodecUtils.java

public static String compressAndEncode(String text) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(new Base64OutputStream(baos));
    try {//from ww w .  ja  v  a 2s. com
        gzos.write(text.getBytes(UTF_8));
    } finally {
        baos.close();
        gzos.close();
    }
    return new String(baos.toByteArray(), UTF_8);
}

From source file:Compress.java

/** Gzip the contents of the from file and save in the to file. */
public static void gzipFile(String from, String to) throws IOException {
    // Create stream to read from the from file
    FileInputStream in = new FileInputStream(from);
    // Create stream to compress data and write it to the to file.
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    // Copy bytes from one stream to the other
    byte[] buffer = new byte[4096];
    int bytes_read;
    while ((bytes_read = in.read(buffer)) != -1)
        out.write(buffer, 0, bytes_read);
    // And close the streams
    in.close();/* w  w  w . j  av  a  2 s .  c om*/
    out.close();
}

From source file:ByteUtils.java

public static byte[] packRaw(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    GZIPOutputStream zos = new GZIPOutputStream(baos);
    zos.write(b);//from   w  w  w.  j a v  a2 s  . c  om
    zos.close();

    return baos.toByteArray();
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

public static byte[] compressString(String value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(value.length());
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(Base64.encode(value.getBytes(StandardCharsets.UTF_8)));
    gos.close();/*from w  w  w .ja  va 2  s  . c om*/
    byte[] compressed = baos.toByteArray();
    baos.close();
    return compressed;
}

From source file:azkaban.utils.GZIPUtils.java

public static byte[] gzipBytes(byte[] bytes, int offset, int length) throws IOException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipStream = null;

    gzipStream = new GZIPOutputStream(byteOutputStream);

    gzipStream.write(bytes, offset, length);
    gzipStream.close();//from  w ww  .  ja v  a  2 s.com
    return byteOutputStream.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] source) throws IOException {
    if (source == null || source.length == 0) {
        return source;
    }/*www.j  a va 2  s.c  o m*/
    ByteArrayInputStream sourceStream = new ByteArrayInputStream(source);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(source.length / 2);
    try (OutputStream compressor = new GZIPOutputStream(outputStream)) {
        ByteStreams.copy(sourceStream, compressor);
        compressor.close();
    }
    try {
        return outputStream.toByteArray();
    } finally {
        sourceStream.close();
        outputStream.close();
    }
}

From source file:com.cenrise.test.azkaban.GZIPUtils.java

public static byte[] gzipBytes(final byte[] bytes, final int offset, final int length) throws IOException {
    final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipStream = null;

    gzipStream = new GZIPOutputStream(byteOutputStream);

    gzipStream.write(bytes, offset, length);
    gzipStream.close();//  w  w  w.  ja va2 s. c  o  m
    return byteOutputStream.toByteArray();
}

From source file:ca.uhn.fhir.jpa.dao.GZipUtil.java

public static byte[] compress(String theEncoded) {
    try {/*from  www.j  a  va 2 s  . co  m*/
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(os);
        IOUtils.write(theEncoded, gos, "UTF-8");
        gos.close();
        os.close();
        byte[] retVal = os.toByteArray();
        return retVal;
    } catch (IOException e) {
        throw new DataFormatException("Compress contents", e);
    }
}

From source file:it.restrung.rest.utils.IOUtils.java

/**
 * Serializes ans saves a Serializable object to a file
 *
 * @param object the source object//from   w ww .java 2 s .com
 * @param file   the target file
 */
static public void saveSerializableObjectToDisk(Object object, File file) {
    try {
        file.delete();
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        ObjectOutputStream out = new ObjectOutputStream(gzos);
        out.writeObject(object);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.d(IOUtils.class.getName(), "Error, file not found for save serializable object to disk");
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.d(IOUtils.class.getName(), "Error on save serializable object");
        throw new RuntimeException(e);
    }
}