[Common Java class libraries]: StringCompress (string compression)

Source: Internet
Author: User

[Common Java class libraries]

StringCompress (string compression)

Method description:

1. compress (String): compress the String by ZIP, and returns the byte array.

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


Purpose:

Save to database BOLB.


[Java]
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)
Return 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 ();
} Catch (IOException localIOException6 ){
}
If (byteArrayOutputStream! = Null)
Try {
ByteArrayOutputStream. close ();
} 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 [2, 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 ();
} Catch (IOException localIOException8 ){
}
If (byteArrayInputStream! = Null)
Try {
ByteArrayInputStream. close ();
} Catch (IOException localIOException9 ){
}
If (byteArrayOutputStream! = Null)
Try {
ByteArrayOutputStream. close ();
} 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.