List of usage examples for java.util.zip GZIPOutputStream close
public void close() throws IOException
From source file:com.ery.ertc.estorm.util.GZIPUtils.java
public static final byte[] zip(byte[] in, int offset, int length) { try {/* w w w. j av a 2 s. co m*/ // compress using GZIPOutputStream ByteArrayOutputStream byteOut = new ByteArrayOutputStream(length / EXPECTED_COMPRESSION_RATIO); GZIPOutputStream outStream = new GZIPOutputStream(byteOut); try { outStream.write(in, offset, length); } catch (Exception e) { LOG.error("Failed to get outStream.write input", e); } try { outStream.close(); } catch (IOException e) { LOG.error("Failed to implement outStream.close", e); } return byteOut.toByteArray(); } catch (IOException e) { LOG.error("Failed with IOException", e); return null; } }
From source file:cn.sharesdk.analysis.net.NetworkHelper.java
public static String Base64Gzip(String str) { ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); String result = null;//w ww . j a v a2 s .c om // gzip GZIPOutputStream gos; try { gos = new GZIPOutputStream(baos); int count; byte data[] = new byte[1024]; while ((count = bais.read(data, 0, 1024)) != -1) { gos.write(data, 0, count); } gos.finish(); gos.close(); byte[] output = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); result = Base64.encodeToString(output, Base64.NO_WRAP); } catch (IOException e) { e.printStackTrace(); Ln.e("NetworkHelper", "Base64Gzip == >>", e); } //Ln.i("after base64gizp", result); return result; }
From source file:org.dd4t.core.util.CompressionUtils.java
/** * Compresses a given object to a GZipped byte array. * * @param object the object to encode//from w w w . j a v a 2s .c o m * @return byte[] representing the compressed object bytes * @throws SerializationException if something goes wrong with the streams */ public static <T> byte[] compressGZipGeneric(T object) throws SerializationException { ByteArrayOutputStream baos = null; GZIPOutputStream gos = null; ObjectOutputStream oos = null; try { baos = new ByteArrayOutputStream(); gos = new GZIPOutputStream(baos); oos = new ObjectOutputStream(gos); oos.writeObject(object); gos.close(); return baos.toByteArray(); } catch (IOException ioe) { LOG.error("Compression failed.", ioe); throw new SerializationException("Failed to compres object", ioe); } finally { IOUtils.closeQuietly(oos); IOUtils.closeQuietly(gos); IOUtils.closeQuietly(baos); } }
From source file:de.kapsi.net.daap.DaapUtil.java
/** * Serializes the <code>chunk</code> and compresses it optionally. * The serialized data is returned as a byte-Array. *///from ww w .ja v a2 s. c o m public static final byte[] serialize(Chunk chunk, boolean compress) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(chunk.getSize()); if (compress) { GZIPOutputStream gzip = new GZIPOutputStream(buffer); chunk.serialize(gzip); gzip.finish(); gzip.close(); } else { chunk.serialize(buffer); buffer.flush(); buffer.close(); } return buffer.toByteArray(); }
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 {/*from w ww . j a v a2 s . c o m*/ 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: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();/*from w w w .j a v a 2 s . c o m*/ objectOutputStream.close(); gzipOutputStream.close(); arrayOutputStream.close(); String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT)); return objectString; }
From source file:com.taobao.tair.etc.TranscoderUtil.java
public static byte[] compress(byte[] in) { if (in == null) { throw new NullPointerException("Can't compress null"); }//from w w w . j av a 2 s.c o m ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gz = null; try { gz = new GZIPOutputStream(bos); gz.write(in); } catch (IOException e) { throw new RuntimeException("IO exception compressing data", e); } finally { try { gz.close(); bos.close(); } catch (Exception e) { // should not happen } } byte[] rv = bos.toByteArray(); if (log.isInfoEnabled()) { log.info("compressed value, size from [" + in.length + "] to [" + rv.length + "]"); } return rv; }
From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java
public static File compressFile(File source) throws IOException { String tmpName = source.getName() + ".tmp"; File tempFile = new File(source.getParentFile(), tmpName); FileUtils.copyFile(source, tempFile); String targetName = source.getName() + ".gz"; File file = new File(source.getParentFile(), targetName); if (file.exists()) file.delete();/*w w w . j av a 2 s .com*/ file.createNewFile(); byte[] buffer = new byte[1024]; try { GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(file)); FileInputStream in = new FileInputStream(tempFile); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); tempFile.delete(); } catch (IOException ex) { ex.printStackTrace(); } Bukkit.getLogger().info("Compressed file " + source.getName() + " to " + file.getName() + "."); return file; }
From source file:org.hawkular.listener.cache.InventoryHelperTest.java
private static byte[] gzip(String data) { ByteArrayOutputStream obj = new ByteArrayOutputStream(); try {/*from w w w. j a va 2 s . c o m*/ GZIPOutputStream gzip = new GZIPOutputStream(obj); gzip.write(data.getBytes("UTF-8")); gzip.close(); return obj.toByteArray(); } catch (IOException e) { throw Throwables.propagate(e); } }
From source file:cn.sharesdk.net.NetworkHelper.java
public static String Base64Gzip(String str) { ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); String result = null;/*from w w w .j a v a 2 s . co m*/ // gzip GZIPOutputStream gos; try { gos = new GZIPOutputStream(baos); int count; byte data[] = new byte[1024]; while ((count = bais.read(data, 0, 1024)) != -1) { gos.write(data, 0, count); } gos.finish(); gos.close(); byte[] output = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); result = Base64.encodeToString(output, Base64.NO_WRAP); } catch (IOException e) { e.printStackTrace(); Ln.i("NetworkHelper", "Base64Gzip == >>", e); } return result; }