Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.util.zip.GZIPInputStream;
Import Java.util.zip.GZIPOutputStream;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;
Import Java.util.zip.ZipOutputStream;
public class Ziputils {
/**
* Compression with Gzip
*/
public static String gzip (string primstr) {
if (Primstr = = NULL | | primstr.length () = = 0) {
return primstr;
}
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Gzipoutputstream Gzip=null;
try {
gzip = new Gzipoutputstream (out);
Gzip.write (Primstr.getbytes ());
} catch (IOException e) {
E.printstacktrace ();
}finally{
if (gzip!=null) {
try {
Gzip.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
return new Sun.misc.BASE64Encoder (). Encode (Out.tobytearray ());
}
/**
*
* <p>description: Use gzip to decompress </p>
* @param compressedstr
* @return
*/
public static string Gunzip (String compressedstr) {
if (compressedstr==null) {
return null;
}
Bytearrayoutputstream out= new Bytearrayoutputstream ();
Bytearrayinputstream In=null;
Gzipinputstream Ginzip=null;
Byte[] Compressed=null;
String decompressed = null;
try {
compressed = new Sun.misc.BASE64Decoder (). Decodebuffer (COMPRESSEDSTR);
In=new Bytearrayinputstream (compressed);
Ginzip=new Gzipinputstream (in);
byte[] buffer = new byte[1024];
int offset =-1;
while (offset = ginzip.read (buffer))! =-1) {
Out.write (buffer, 0, offset);
}
Decompressed=out.tostring ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (ginzip! = null) {
try {
Ginzip.close ();
} catch (IOException e) {
}
}
if (in = null) {
try {
In.close ();
} catch (IOException e) {
}
}
if (out! = null) {
try {
Out.close ();
} catch (IOException e) {
}
}
}
return decompressed;
}
/**
* Compress with zip
* @param the text before str compression
* @return Return compressed text
*/
public static final String zip (String str) {if (str = = NULL)
return null;
Byte[] Compressed;
Bytearrayoutputstream out = null;
Zipoutputstream zout = null;
String compressedstr = null;
try {
out = new Bytearrayoutputstream ();
Zout = new Zipoutputstream (out);
Zout.putnextentry (New ZipEntry ("0"));
Zout.write (Str.getbytes ());
Zout.closeentry ();
Compressed = Out.tobytearray ();
Compressedstr = new Sun.misc.BASE64Encoder (). Encodebuffer (compressed);
} catch (IOException e) {
compressed = null;
} finally {
if (zout! = null) {
try {
Zout.close ();
} C Atch (IOException e) {
}
}
if (out! = null) {
try {
Out.close ();
} catch (IOException e) {
}
}
}
return compressedstr;
}
/**
* Unzip with zip
* @param compressed Compressed text
* @return extracted string
*/
public static final string unzip ( String Compressedstr {
if (compressedstr = = null) {
return null;
}
Bytearrayoutputstream out = null;
Bytearrayinputstream in = null;
Zipinputstream Zin = null;
String decompressed = null;
try {
byte[] Compressed = new Sun.misc.BASE64Decoder (). Decodebuffer (COMPRESSEDSTR);
out = new Bytearrayoutputstream ();
in = new Bytearrayinputstream (compressed);
Zin = new Zipinputstream (in);
Zin.getnextentry ();
byte[] buffer = new byte[1024];
int offset =-1;
while (offset = zin.read (buffer))! =-1) {
Out.write (buffer, 0, offset);
}
decompressed = Out.tostring ();
} catch (IOException e) {
decompressed = null;
} finally {
if (Zin! = null) {
try {
Zin.close ();
} catch (IOException e) {
}
}
if (in = null) {
try {
In.close ();
} catch (IOException e) {
}
}
if (out! = null) {
try {
Out.close ();
} catch (IOException e) {
}
}
}
return decompressed;
}
}
Java compression and decompression of strings