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:Main.java

public static boolean saveObjectToFile(String filePath, Object object) {
    if (!TextUtils.isEmpty(filePath)) {
        File cacheFile = null;//from   w  ww.ja  v  a  2s .  c o  m

        try {
            cacheFile = new File(filePath);
            if (cacheFile.exists()) {
                cacheFile.delete();
            }

            if (!cacheFile.getParentFile().exists()) {
                cacheFile.getParentFile().mkdirs();
            }

            cacheFile.createNewFile();
        } catch (Throwable var6) {
            var6.printStackTrace();
            cacheFile = null;
        }

        if (cacheFile != null) {
            try {
                FileOutputStream t = new FileOutputStream(cacheFile);
                GZIPOutputStream gzos = new GZIPOutputStream(t);
                ObjectOutputStream oos = new ObjectOutputStream(gzos);
                oos.writeObject(object);
                oos.flush();
                oos.close();
                return true;
            } catch (Throwable var7) {
                var7.printStackTrace();
            }
        }
    }

    return false;
}

From source file:Main.java

public static String compressGzipFile(String filename) {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
        String compressedFile = flockedFilesFolder.toString() + "/" + "gzip.zip";

        try {// w w  w  .  j  a  v  a 2 s  . c om
            FileInputStream fis = new FileInputStream(uncompressedFile);
            FileOutputStream fos = new FileOutputStream(compressedFile);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos) {
                {
                    def.setLevel(Deflater.BEST_COMPRESSION);
                }
            };

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, len);
            }
            //close resources
            gzipOS.close();
            fos.close();
            fis.close();
            return compressedFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

}

From source file:Main.java

public static final byte[] compress(byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }//from  ww w . jav  a 2 s .c o  m
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = bos.toByteArray();
        gzip.close();
        bos.close();
        gzip = null;
        bos = null;
        return fewerBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:Util.java

/**
 * Writes a serialized version of obj to a given file, compressing it using gzip.
 * @param f File to write to/*  w  w w . ja  v a  2s .  c  om*/
 * @param obj Object to serialize
 */
public static void writeGzippedObject(File f, Serializable obj) {
    try {
        ObjectOutputStream oos = new ObjectOutputStream(
                new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f))));
        oos.writeObject(obj);
        oos.close();
    } catch (IOException e) {
        System.err.println("Exception writing file " + f + ": " + e);
    }
}

From source file:bencoding.securely.Converters.java

public static String serializeObjectToString(Object object) throws Exception {

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);

    objectOutputStream.writeObject(object);

    objectOutputStream.flush();//w  ww .j  ava  2 s .c om
    objectOutputStream.close();
    gzipOutputStream.close();
    arrayOutputStream.close();

    String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT));

    return objectString;
}

From source file:com.amazonaws.services.logs.subscriptions.util.TestUtils.java

public static byte[] getCompressedTestFile(String filename) throws IOException {
    byte[] uncompressedData = FileUtils
            .readFileToByteArray(new File(TestUtils.class.getResource(filename).getFile()));

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);

    try {/*from w w w. ja  va2 s.  com*/
        gzipOutputStream.write(uncompressedData);
        gzipOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    } finally {
        byteArrayOutputStream.close();
    }
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.Compression.java

public static byte[] compress(byte[] plaintext) {
    try {/*from   w  w  w .  j  a v a  2 s . c o m*/
        ByteArrayOutputStream writer = new ByteArrayOutputStream();
        GZIPOutputStream gzipStream = new GZIPOutputStream(writer);
        gzipStream.write(plaintext);
        gzipStream.flush();
        gzipStream.close();
        writer.close();
        return writer.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static byte[] gzipCompress(byte[] input) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/* w ww  .j a  va 2s . com*/
        GZIPOutputStream os = new GZIPOutputStream(bos);
        os.write(input);
        os.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return bos.toByteArray();
}

From source file:Main.java

/**
 * GZip a given byte-array in-memory./*from  w  w w.  ja  v a  2 s  .com*/
 * 
 * @param bytes
 *            the uncompressed bytes
 * 
 * @return the GZipped byte stream
 * 
 * @throws IOException
 *             if something fails
 */
public static byte[] gzipByteArray(byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);

    GZIPOutputStream gzip = new GZIPOutputStream(baos);
    gzip.write(bytes);
    gzip.close();

    return baos.toByteArray();
}

From source file:Main.java

/**
 * Creates an output stream to write to a regular or compressed file.
 * // w ww. j  av a2 s  .c  om
 * @param fileName    a file name with an extension .gz or without it;
 *                    if the user specifies an extension .gz, we assume
 *                    that the output file should be compressed.
 * @return an output stream to write to a file.
 * @throws IOException
 */
public static OutputStream createOutputStream(String fileName) throws IOException {
    OutputStream foutp = new FileOutputStream(fileName);
    if (fileName.endsWith(".gz"))
        return new GZIPOutputStream(foutp);
    if (fileName.endsWith(".bz2")) {
        throw new IOException("bz2 is not supported for writing");
    }
    return foutp;
}