Java compression and decompression of strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.