Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 22nd, 2012  |  syntax: Java  |  size: 1.29 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.ByteArrayOutputStream;
  2. import java.io.IOException;
  3. import java.util.zip.Deflater;
  4. import java.util.zip.DeflaterOutputStream;
  5. import java.util.zip.Inflater;
  6. import java.util.zip.InflaterOutputStream;
  7.  
  8. public class gzip {
  9.         public static final String encode(String text) throws IOException{
  10.                  byte[] input = text.getBytes();
  11.              ByteArrayOutputStream stream = new ByteArrayOutputStream();
  12.              Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
  13.              DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(stream, compresser);
  14.              deflaterOutputStream.write(input);
  15.              deflaterOutputStream.close();
  16.              byte[] output = stream.toByteArray();
  17.              String outputString = new String(output);
  18.              return outputString;
  19.         }
  20.        
  21.         public static final String decode(String text) throws IOException{
  22.                 byte[] in = text.getBytes();
  23.             ByteArrayOutputStream stream = new ByteArrayOutputStream();
  24.             Inflater decompresser = new Inflater(true);
  25.             InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(stream, decompresser);
  26.             inflaterOutputStream.write(in);
  27.             inflaterOutputStream.close();
  28.             byte[] output = stream.toByteArray();
  29.              String outputString = new String(output);
  30.              return outputString;          
  31.         }
  32. }
clone this paste RAW Paste Data