2
我寫了一些Javascript代碼。 壓縮與Base64和放氣javascript deflate和java.util.zip.Deflater有什麼區別
function base64 (str) {
return new Buffer(str).toString("base64");
}
function deflate (str) {
return RawDeflate.deflate(str);
}
function encode (str) {
return base64(deflate(str));
}
var str = "hello, world";
console.log("Test Encode");
console.log(encode(str));
我改裝後的「你好,世界」到2f8d48710d6e4229b032397b2492f0c2
,我想在java中
我把海峽的文件進行解壓此字符串(2f8d48710d6e4229b032397b2492f0c2)然後:
public static String decompress1951(final String theFilePath) {
byte[] buffer = null;
try {
String ret = "";
System.out.println("can come to ret");
InputStream in = new InflaterInputStream(new Base64InputStream(new FileInputStream(theFilePath)), new Inflater(true));
System.out.println("can come to in");
while (in.available() != 0) {
buffer = new byte[20480];
*****line 64 excep happen int len = in.read(buffer, 0, 20480);
if (len <=0) {
break;
}
ret = ret + new String(buffer, 0, len);
}
in.close();
return ret;
} catch (IOException e) {
System.out.println("Has IOException");
System.out.println(e.getMessage());
e.printStackTrace();
}
return "";
}
但我有一個例外:
java.util.zip.ZipException: invalid stored block lengths
at java.util.zip.InflaterInputStream.read(Unknown Source)
at com.cnzz.mobile.datacollector.DecompressDeflate.decompress1951(DecompressDeflate.java:64)
at com.cnzz.mobile.datacollector.DecompressDeflate.main(DecompressDeflate.java:128)
代碼正確 關鍵是InflaterInputStream的第二個參數 新的Inflater(true)。如果沒有這個參數,代碼可以工作,但不是純粹的defalter。 也許與 RFC1950 zlib:[基本鏈接](http://www.ietf.org/rfc/rfc1950.txt)的區別。 (不含) RFC 1951 deflate:[基本鏈接](http://www.ietf.org/rfc/rfc1951.txt)。 (與新的Inflater(真)) 而「hello,world」的轉換字符串不是2f8d48710d6e4229b032397b2492f0c2。這就是爲什麼我無法解決它 – littletiger 2012-02-01 11:27:59