Example usage for java.util.zip GZIPOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:org.mitre.opensextant.util.FileUtility.java

/**
 *
 * @param text//from www .j  a  va 2s.c o  m
 * @param fname
 * @return
 * @throws IOException
 */
public static boolean writeGzipFile(String text, String fname) throws IOException {
    if (fname == null || text == null) {
        return false;
    }

    FileOutputStream outstream = new FileOutputStream(fname);
    GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(outstream), default_buffer);

    gzout.write(text.getBytes(default_encoding));

    gzout.flush();
    gzout.finish();

    gzout.close();
    outstream.close();
    return true;

}

From source file:com.beligum.core.utils.AssetPacker.java

private static byte[] encodeGzip(String content) throws IOException {
    byte[] retVal = null;

    ByteArrayOutputStream out = null;
    GZIPOutputStream gzip = null;
    try {/*from  www  .  j  a  va2  s .  c  o  m*/
        out = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(out);
        gzip.write(content.getBytes("UTF-8"));
        gzip.close();
        out.close();

        retVal = out.toByteArray();
    } catch (Exception e) {
        if (gzip != null) {
            gzip.close();
        }
        if (out != null) {
            out.close();
        }
    }

    return retVal;
}

From source file:com.evolveum.midpoint.repo.sql.util.RUtil.java

public static byte[] getByteArrayFromXml(String xml, boolean compress) {
    byte[] array;

    GZIPOutputStream gzip = null;
    try {/*from   w w w. ja  va  2s. c  om*/
        if (compress) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(xml.getBytes("utf-8"));
            gzip.close();
            out.close();

            array = out.toByteArray();
        } else {
            array = xml.getBytes("utf-8");
        }
    } catch (Exception ex) {
        throw new SystemException("Couldn't save full xml object, reason: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(gzip);
    }

    return array;
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * GZIP encode./*w w w  .  j  a  v  a 2  s  . c o m*/
 * 
 * @param str
 * @return
 * @throws IOException
 */
public synchronized static byte[] gzipEncode(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return null;
    }
    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);
    gzip.write(str.getBytes("UTF-8"));
    gzip.close();
    return obj.toByteArray();
}

From source file:com.comcast.cqs.util.Util.java

public static String compress(String decompressed) throws IOException {
    if (decompressed == null || decompressed.equals("")) {
        return decompressed;
    }/*from  w  w  w.  j  a  v  a  2 s .  co  m*/
    ByteArrayOutputStream out = new ByteArrayOutputStream(decompressed.length());
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(decompressed.getBytes());
    gzip.close();
    out.close();
    String compressed = Base64.encodeBase64String(out.toByteArray());
    logger.debug("event=compressed from=" + decompressed.length() + " to=" + compressed.length());
    return compressed;
}

From source file:updater.builder.SoftwarePatchBuilder.java

public static void catalog(CommandLine line, Options options) throws ParseException, Exception {
    if (!line.hasOption("key")) {
        throw new Exception("Please specify the key file to use using --key");
    }//  w  w w  .java2s .c o m
    if (!line.hasOption("output")) {
        throw new Exception("Please specify the path to output the XML file using --output");
    }

    String[] catalogArgs = line.getOptionValues("catalog");
    String keyArg = line.getOptionValue("key");
    String outputArg = line.getOptionValue("output");

    if (catalogArgs.length != 2) {
        throw new ParseException("Wrong arguments for 'catalog', expecting 2 arguments");
    }
    if (!catalogArgs[0].equals("e") && !catalogArgs[0].equals("d")) {
        throw new ParseException("Catalog mode should be either 'e' or 'd' but not " + catalogArgs[0]);
    }

    RSAKey rsaKey = RSAKey.read(Util.readFile(new File(keyArg)));

    System.out.println("Mode: " + (catalogArgs[0].equals("e") ? "encrypt" : "decrypt"));
    System.out.println("Catalog file: " + catalogArgs[1]);
    System.out.println("Key file: " + keyArg);
    System.out.println("Output file: " + outputArg);
    System.out.println();

    File in = new File(catalogArgs[1]);
    File out = new File(outputArg);
    BigInteger mod = new BigInteger(rsaKey.getModulus());

    if (catalogArgs[0].equals("e")) {
        BigInteger privateExp = new BigInteger(rsaKey.getPrivateExponent());

        RSAPrivateKey privateKey = CommonUtil.getPrivateKey(mod, privateExp);

        // compress
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(bout);
        gout.write(Util.readFile(in));
        gout.finish();
        byte[] compressedData = bout.toByteArray();

        // encrypt
        int blockSize = mod.bitLength() / 8;
        byte[] encrypted = Util.rsaEncrypt(privateKey, blockSize, blockSize - 11, compressedData);

        // write to file
        Util.writeFile(out, encrypted);
    } else {
        BigInteger publicExp = new BigInteger(rsaKey.getPublicExponent());
        RSAPublicKey publicKey = CommonUtil.getPublicKey(mod, publicExp);

        // decrypt
        int blockSize = mod.bitLength() / 8;
        byte[] decrypted = Util.rsaDecrypt(publicKey, blockSize, Util.readFile(in));

        // decompress
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ByteArrayInputStream bin = new ByteArrayInputStream(decrypted);
        GZIPInputStream gin = new GZIPInputStream(bin);

        int byteRead;
        byte[] b = new byte[1024];
        while ((byteRead = gin.read(b)) != -1) {
            bout.write(b, 0, byteRead);
        }
        byte[] decompressedData = bout.toByteArray();

        // write to file
        Util.writeFile(out, decompressedData);
    }

    System.out.println("Manipulation succeed.");
}

From source file:com.urs.triptracks.TripUploader.java

public static byte[] compress(String string) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes());
    gos.close();//from   w w w .  j  a v a  2 s .co m
    byte[] compressed = os.toByteArray();
    os.close();
    return compressed;
}

From source file:backtype.storm.utils.Utils.java

public static byte[] gzip(byte[] data) {
    try {// w ww  . ja  v  a  2s  . c o  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream out = new GZIPOutputStream(bos);
        out.write(data);
        out.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.broad.igv.cbio.GeneNetwork.java

public static int writeEncodedString(String string, OutputStream outputStream, boolean gzip,
        boolean base64encode) throws IOException {

    byte[] byteData;

    if (gzip) {// w w  w  .j  av  a  2s  .  c o  m
        ByteArrayOutputStreamChild gmlByteStream = new ByteArrayOutputStreamChild(string.length() / 20);
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(gmlByteStream);
        gzipOutputStream.write(string.getBytes());
        gzipOutputStream.finish();
        byteData = gmlByteStream.getBuf();
        gmlByteStream.close();
    } else {
        byteData = string.getBytes();
    }

    int count = 0;
    if (base64encode) {
        char[] gmlData = Base64Coder.encode(byteData);
        for (char c : gmlData) {
            outputStream.write(c);
            count++;
        }
    } else {
        outputStream.write(byteData);
        outputStream.flush();
        count += byteData.length;
    }
    outputStream.flush();
    return count;
}

From source file:com.krawler.common.util.ByteUtil.java

/**
 * compress the supplied data using GZIPOutputStream and return the
 * compressed data./* w  ww.  j  a  v a2s . co  m*/
 * 
 * @param data
 *            data to compress
 * @return compressesd data
 */
public static byte[] compress(byte[] data) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;
    try {
        baos = new ByteArrayOutputStream(data.length); // data.length
        // overkill
        gos = new GZIPOutputStream(baos);
        gos.write(data);
        gos.finish();
        return baos.toByteArray();
    } finally {
        if (gos != null) {
            gos.close();
        } else if (baos != null)
            baos.close();
    }
}