Groovy web console

subscribe to the feed Subscribe
to this
site

zip

Published 2 months ago by Ram with tags zip compression
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.io.*;
import java.util.zip.*;

byte[] input = "compression string".getBytes();   
  // 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[1024];
  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();