[Java Common Class Library]:stringcompress (string compression)

Source: Internet
Author: User

[Java Common class library ]

Stringcompress (String compression)


Method Description:

1.compress (String): Zip-Compress a string to return an array of bytes

2.decompress (byte[]): Restores a compressed byte array to a string


Use:

Saved to the database Bolb.


Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;

Import Java.util.zip.ZipOutputStream; public class Stringcompress {public static final byte[] Compress (String paramstring) {if (paramstring = null) ret
		Urn null;
		Bytearrayoutputstream bytearrayoutputstream = null;
		Zipoutputstream zipoutputstream = null;
		Byte[] Arrayofbyte;
			try {bytearrayoutputstream = new Bytearrayoutputstream ();
			Zipoutputstream = new Zipoutputstream (bytearrayoutputstream);
			Zipoutputstream.putnextentry (New ZipEntry ("0"));
			Zipoutputstream.write (Paramstring.getbytes ());
			Zipoutputstream.closeentry ();
		Arrayofbyte = Bytearrayoutputstream.tobytearray ();
		catch (IOException localIOException5) {arrayofbyte = null;
				finally {if (zipoutputstream!= null) try {zipoutputstream.close (); The catch (IOException localIOException6) {} if (bytearrayouTputstream!= Null) try {bytearrayoutputstream.close ();
	The catch (IOException localIOException7) {}} return arrayofbyte; @SuppressWarnings ("unused") public static final String decompress (byte[] paramarrayofbyte) {if (Paramarrayofbyte =
		= null) return null;
		Bytearrayoutputstream bytearrayoutputstream = null;
		Bytearrayinputstream bytearrayinputstream = null;
		Zipinputstream zipinputstream = null;
		String str;
			try {bytearrayoutputstream = new Bytearrayoutputstream ();
			Bytearrayinputstream = new Bytearrayinputstream (paramarrayofbyte);
			Zipinputstream = new Zipinputstream (bytearrayinputstream);
			ZipEntry localzipentry = Zipinputstream.getnextentry ();
			byte[] Arrayofbyte = new byte[1024];
			int i =-1;
			while ((i = Zipinputstream.read (arrayofbyte))!=-1) bytearrayoutputstream.write (arrayofbyte, 0, I);
		str = bytearrayoutputstream.tostring ();
		catch (IOException localIOException7) {str = null; finally {if (zipinputsTream!= Null) try {zipinputstream.close (); The catch (IOException LocalIOException8) {} if (Bytearrayinputstream!= null) try {BYTEARRAYINPUTSTREAM.C
				Lose (); The catch (IOException LocalIOException9) {} if (Bytearrayoutputstream!= null) try {Bytearrayoutputstream
				. Close ();
	The catch (IOException localIOException10) {}} return str; }
}




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.