[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;
}
}