compression and décompression
/**
* Compress a string and add the header "zip:"
*
* @param strToCompress the string to compress.
* @return the string compressed, NULL if an error occurs.
*/
public static String compress(String strToCompress) {
String out = null;
ByteArrayOutputStream bos = null;
Deflater compressor = null;
try {
byte[] input = strToCompress.getBytes("UTF-8");
// Set the highest level of compression
compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
bos = new ByteArrayOutputStream(input.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
// Get the compressed data
byte[] bytesCompressed = bos.toByteArray();
out = new String(Base64.encodeBase64(bytesCompressed), "UTF-8");
out = "zip:" + out;
} catch (UnsupportedEncodingException e) {
LOGGER.error("UnsupportedEncodingException in compress method. null is returned: " + e);
return null;
} finally {
if (compressor != null) {
compressor.end();
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
LOGGER.error("IOException in compress method. null is returned: " + e);
return null;
}
}
}
return out;
}
/**
* Uncompress a string if the input string has the header "zip:".
*
* @param strToUncompress the string to be uncompressed.
* @return return the uncompressed string if the input string has the header "zip:", return the input string otherwise, return NULL if an error occurs.
*/
public static String uncompress(String strToUncompress) {
// Return the input string if the input string doesn't have the header "zip:"
if (!strToUncompress.startsWith("zip:")) {
return strToUncompress;
}
// Remove the header "zip:"
strToUncompress = strToUncompress.substring(4);
String out = null;
ByteArrayOutputStream bos = null;
Inflater decompressor = null;
try {
byte[] compressedData = Base64.decodeBase64(strToUncompress.getBytes("UTF-8"));
// Create the decompressor and give it the data to compress
decompressor = new Inflater();
decompressor.setInput(compressedData);
// Create an expandable byte array to hold the decompressed data
bos = new ByteArrayOutputStream(compressedData.length);
// Uncompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
// Get the decompressed data
out = new String(bos.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
LOGGER.error("UnsupportedEncodingException in compress method. null is returned: " + e);
return null;
} catch (DataFormatException e) {
LOGGER.error("DataFormatException in compress method. null is returned: " + e);
return null;
} finally {
if (decompressor != null) {
decompressor.end();
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
LOGGER.error("IOException in compress method. null is returned: " + e);
return null;
}
}
}
return out;
}
浙公网安备 33010602011771号