SHARE
TWEET

Untitled

a guest Dec 22nd, 2012 49 Never
  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. }
RAW Paste Data
Top