早上在論壇中看到有人提問, 感覺有點興趣, 於是就看了一下. 首先想到的是java 內建的zip 包. 看了一下API文檔, 發現原來有完整的例子, 包括壓縮和解壓縮. 以下的內容來自java.util.zip.Inflater的javadoc, 只是稍作了一些修改.
public static void compressString() {
System.out.println("//////////////////////////////////////START//////////////////////////////////////");
try {
// Encode a String into bytes
String inputString = "LLLLLLLLLLLLLLLLLLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
System.out.println("Input string:" + inputString);
System.out.println("Input length:" + input.length);
System.out.println("Compressed length:" + compressedDataLength);
System.out.println("Compressed string:" + new String(output));
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("Decompressed string:" + outputString);
} catch (java.io.UnsupportedEncodingException ex) {
// handle
} catch (java.util.zip.DataFormatException ex) {
// handle
}
}
找到了現成的代碼, 感覺還有些不過癮, 於是又藏式用stream方式來實現.
public static void compressStringWithStream() throws Exception {
System.out.println("//////////////////////////////////////START//////////////////////////////////////");
String inputString = "LLLLLLLLLLLLLLLLLLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG";
byte[] input = inputString.getBytes();
// compress
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream zos = new DeflaterOutputStream(baos);
zos.write(input);
zos.finish();
zos.flush();
byte[] compressedData = baos.toByteArray();
System.out.println("Input string:" + inputString);
System.out.println("Input length:" + input.length);
System.out.println("Compressed length:" + compressedData.length);
System.out.println("Compressed string:" + new String(compressedData));
// decompress
ByteArrayInputStream bais = new ByteArrayInputStream(compressedData);
// Inflater inflater = new Inflater();
// inflater.setInput(compressedData);
// InflaterInputStream gis = new InflaterInputStream(bais, new Inflater());
InflaterInputStream gis = new InflaterInputStream(bais);
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
int i;
while ((i = gis.read()) != -1) {
baos2.write(i);
}
byte[] decompressedData = baos2.toByteArray();
gis.read(decompressedData);
System.out.println("Decompressed length:" + decompressedData.length);
System.out.println("Decompressed string:" + new String(decompressedData));
}
其實碰到問題需要解決時, 思路很重要, 要試著去找最直接簡單的方法. 壓縮 > zip > java.util.zip 想到這個, 問題解決一半了. 這個也需要一定的經驗, 就是說你至少要知道java已經提供了zip包. 說道這裡, 不得不說的是, 很多東西(工具/類庫), 其實當你看第一眼的時候, 你只需要去瞭解他能夠做什麼, 至於怎麼用如果時間有限的話可以暫時不看, 到了真正要使用的時候再去看也不遲. 只要知道有這麼個東西, 當你碰到相應的問題的時候能夠想到可以用它來幫你解決問題就好了. 這個也是需要積累的, 但是只看一個功能概要比起仔細研究來說, 所花的時間少多了. 只有到了真正使用的時候才去瞭解. 另外, 在互連網發達的現在, 應該更好的去利用搜尋引擎. 就算java本身沒有提供zip包, 你也應該且十分必要去嘗試搜尋是否已經有人實現了這個功能, open source這麼多, 很多時候總有一款能夠滿足你的需求, 應該盡量避免重新造輪子.