List of usage examples for java.util.zip Deflater setInput
public void setInput(ByteBuffer input)
From source file:Main.java
public static byte[] compress(byte[] data, int level) throws IOException { if (data == null || data.length == 0) { return data; }// w ww . java2 s .c o m ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length); Deflater deflater = new Deflater(); deflater.setLevel(level); deflater.setInput(data); deflater.finish(); byte[] buf = new byte[BUFFER_SIZE]; while (!deflater.finished()) { int count = deflater.deflate(buf); bout.write(buf, 0, count); } deflater.end(); bout.close(); return bout.toByteArray(); }
From source file:Main.java
public static byte[] compress(byte[] data) throws IOException { Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION); compresser.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); compresser.finish();//w w w.j av a 2 s. c o m byte[] buffer = new byte[1024]; while (!compresser.finished()) { int count = compresser.deflate(buffer); outputStream.write(buffer, 0, count); } compresser.end(); outputStream.close(); return outputStream.toByteArray(); }
From source file:Main.java
private static byte[] compressBytesInflateDeflate(byte[] inBytes) { Deflater deflater = new Deflater(Deflater.BEST_SPEED); deflater.setInput(inBytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length); deflater.finish();/*from ww w . ja v a 2 s. co m*/ byte[] buffer = new byte[1024 * 8]; while (!deflater.finished()) { int count = deflater.deflate(buffer); bos.write(buffer, 0, count); } byte[] output = bos.toByteArray(); return output; }
From source file:Main.java
public static byte[] zipCompress(byte[] input, int level) { Deflater compressor = new Deflater(); compressor.setLevel(level);/*from www . j a v a 2 s . c o m*/ compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } return bos.toByteArray(); }
From source file:org.xdi.zip.CompressionHelper.java
public static byte[] deflate(byte[] data, boolean nowrap) throws IOException { Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, nowrap); deflater.setInput(data); deflater.finish();//from w ww . j a v a 2s .c om ByteArrayOutputStream os = new ByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); os.write(buffer, 0, count); } } finally { IOUtils.closeQuietly(os); } return os.toByteArray(); }
From source file:org.odk.collect.android.utilities.CompressionUtils.java
public static String compress(String data) throws IOException { if (data == null || data.length() == 0) { return data; }/*w w w . ja va 2 s . c o m*/ // Encode string into bytes byte[] input = data.getBytes("UTF-8"); Deflater deflater = new Deflater(); deflater.setInput(input); // Compress the bytes ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length()); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); // Encode to base64 String base64String = Base64.encodeBase64String(output); Timber.i("Original : %d", data.length()); Timber.i("Compressed : %d", base64String.length()); Timber.i("Compression ratio : %2f", ((data.length() * 1.0) / base64String.length()) * 100); return base64String; }
From source file:org.samlsnort.util.EncodingTool.java
public static String deflateToBase64(String inflated) { Deflater deflater = new Deflater(Deflater.DEFLATED, true); deflater.setInput(inflated.getBytes(CHARSET)); deflater.finish();//from w w w. j a v a2 s .c om byte[] deflatedBytes = new byte[2048]; int len = deflater.deflate(deflatedBytes); return new String(Base64.encodeBase64(Arrays.copyOf(deflatedBytes, len)), CHARSET); }
From source file:Main.java
public static byte[] Compress(String text) throws Exception { Deflater compressor = new Deflater(); byte[] bytes = text.getBytes("UTF-16LE"); compressor.setInput(bytes); // Create an expandable byte array to hold the compressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(); compressor.finish();//from w ww. j a v a2s .co m byte[] buffer = new byte[1024]; try { while (!compressor.finished()) { int count = compressor.deflate(buffer); bos.write(buffer, 0, count); } } finally { compressor.finish(); } bos.close(); return bos.toByteArray(); }
From source file:Main.java
/** * DEFLATEs the specified input data./* w ww . jav a 2 s .c o m*/ * * @param data the input data * @param dictionary the dictionary, or null if none * @return the compressed data */ public static byte[] deflate(byte[] data, byte[] dictionary) { Deflater deflater = new Deflater(8, true); if (dictionary != null) { deflater.setDictionary(dictionary); } deflater.setInput(data); deflater.finish(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; while (!deflater.finished()) { int n = deflater.deflate(buffer); byteArrayOutputStream.write(buffer, 0, n); } byte[] result = byteArrayOutputStream.toByteArray(); return result; }
From source file:Main.java
public static byte[] compressZLIB(byte[] input) throws IOException { // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_SPEED); // Give the compressor the data to compress compressor.setInput(input); compressor.finish();/* w w w . j a v a 2 s.c o m*/ // Create an expandable byte array to hold the compressed data. // You cannot use an array that's the same size as the orginal because // there is no guarantee that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); // Get the compressed data byte[] compressedData = bos.toByteArray(); return compressedData; }