List of usage examples for java.util.zip Deflater setLevel
public void setLevel(int level)
From source file:Main.java
License:asdf
public static void main(String[] argv) throws Exception { byte[] input = "asdf".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);//from www .j a v a2s .c om 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); } bos.close(); byte[] compressedData = bos.toByteArray(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] input = "www.java2s.com".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);/*from w ww. ja v a 2 s .co m*/ 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); } bos.close(); byte[] compressedData = bos.toByteArray(); System.out.println(Arrays.toString(compressedData)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] input = "this is a test".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);/* w w w . java 2 s .c om*/ 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); } bos.close(); byte[] compressedData = bos.toByteArray(); Inflater decompressor = new Inflater(); decompressor.setInput(compressedData); bos = new ByteArrayOutputStream(compressedData.length); buf = new byte[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] decompressedData = bos.toByteArray(); System.out.println(new String(decompressedData)); }
From source file:Main.java
public static String compressString(String str, String encoding) { String compressedString = ""; try {/*from w w w.ja v a 2 s . c om*/ byte[] input = str.getBytes(encoding); Deflater d = new Deflater(); d.setLevel(Deflater.BEST_COMPRESSION); ByteArrayOutputStream out = new ByteArrayOutputStream(); DeflaterOutputStream dout = new DeflaterOutputStream(out, d); dout.write(input); dout.close(); compressedString = new String(Hex.encodeHex(out.toByteArray())); } catch (Exception e) { e.printStackTrace(); } return compressedString; }
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 . j a v a2s.c om 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[] 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);//from www . j a v a 2 s . com compressor.finish(); // 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; }
From source file:Main.java
public static byte[] compress(byte[] input) { if (input == null || input.length == 0) return input; Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input);//from ww w . j a v a 2 s .c om compressor.finish(); // Create an expandable byte array to hold the compressed data. // It is not necessary 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); } // Get the compressed data return bos.toByteArray(); }
From source file:Main.java
public static byte[] zipCompress(byte[] input, int level) { Deflater compressor = new Deflater(); compressor.setLevel(level); compressor.setInput(input);/*from w w w. j a v a 2 s .co m*/ 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:Main.java
public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception { byte[] input = bytes; // Compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input);/* w w w .j av a2 s. c om*/ compressor.finish(); // Create an expandable byte array to hold the compressed data. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[32]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } // Get the compressed data byte[] compressedData = bos.toByteArray(); return new String(Base64.encode(compressedData), "UTF-8"); }
From source file:Main.java
/** * Compress the byte array passed//from w w w . j av a 2 s. c o m * <p> * @param input byte array * @param bufferLength buffer length * @return compressed byte array * @throws IOException thrown if we can't close the output stream */ public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException { // Compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); // Create an expandable byte array to hold the compressed data. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[bufferLength]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ ) compressor.end(); bos.close(); // Get the compressed data return bos.toByteArray(); }