List of usage examples for java.util.zip Deflater Deflater
public Deflater()
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 ww w. j a v a 2s.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[] args) throws IOException { Deflater def = new Deflater(); byte[] input = new byte[1024]; byte[] output = new byte[1024]; FileInputStream fin = new FileInputStream("a.dat"); FileOutputStream fout = new FileOutputStream("b.dat"); int numRead = fin.read(input); def.setInput(input, 0, numRead);/*from w w w. j a v a 2 s .c om*/ while (!def.needsInput()) { int numCompressedBytes = def.deflate(output, 0, output.length); if (numCompressedBytes > 0) { fout.write(output, 0, numCompressedBytes); } } def.finish(); fin.close(); fout.flush(); fout.close(); def.reset(); }
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);/* w w w . jav a 2 s . c o 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 ww . j av a 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 void main(String[] args) throws Exception { // main method // Encode a String into bytes String inputString = "this is a test"; byte[] input = inputString.getBytes("UTF-8"); // Compress the bytes byte[] output1 = new byte[input.length]; Deflater compresser = new Deflater(); compresser.setInput(input);//from w w w. j a va 2 s .c o m compresser.finish(); int compressedDataLength = compresser.deflate(output1); compresser.end(); String str = new String(Base64.getEncoder().encode(output1)); System.out.println("Deflated String:" + str); byte[] output2 = Base64.getDecoder().decode(str); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output2); byte[] result = str.getBytes(); int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String String outputString = new String(result, 0, resultLength, "UTF-8"); System.out.println("Deflated String:" + outputString); }
From source file:Main.java
public static byte[] compress(byte[] uncompressedBuffer) { Deflater deflater = new Deflater(); deflater.setInput(uncompressedBuffer); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(uncompressedBuffer.length); try {/*from w ww . j a v a 2 s . c o m*/ deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } byte[] output = outputStream.toByteArray(); return output; } finally { try { deflater.end(); outputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:Main.java
private static byte[] compress(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data);/*from w ww .j av a2 s . c o m*/ 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(); deflater.end(); // System.out.println("Original: " + data.length + " bytes."); // System.out.println("Compressed: " + output.length + " bytes."); return output; }
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 ava2 s . c o m 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[] compress(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data);/* w w w . ja va 2 s . c o m*/ 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(); System.out.println("Original: " + data.length / 1024 + " Kb"); System.out.println("Compressed: " + output.length / 1024 + " Kb"); return output; }
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 w ww .j a v a2s. co m*/ 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; }