
前端 Node.js DeflaterRaw 压缩zipJSON字符串,是没有标头的!
所以使用Java inflater 去解压时会报,提示无标头异常! incorrect header check!
java.util.zip.DataFormatException: incorrect header check at java.util.zip.Inflater.inflateBytes(Native Method) at java.util.zip.Inflater.inflate(Inflater.java:259) at java.util.zip.Inflater.inflate(Inflater.java:280) at com.example.my_qr_smart.ExampleUnitTest.decompressStrToBytes(ExampleUnitTest.java:68)
此时我们要使用Java inflater 去解压字符串时需要将Inflater 的 nowrap 设置为 true 去忽略标头进行解压即可。
public static String compressStrToBytes(byte[] str) throws IOException {
byte[] outinput = new byte[0];
Inflater decompresser = new Inflater(true); //设置为Ture
decompresser.reset();
decompresser.setInput(str);
ByteArrayOutputStream out = new ByteArrayOutputStream(str.length);
try {
byte[] buf = new byte[1024];
while (!decompresser.finished()) {
int i = decompresser.inflate(buf);
out.write(buf, 0, i);
}
outinput = out.toByteArray();
} catch (Exception e) {
outinput = str;
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
decompresser.end();
return new String(outinput);
}
更改后就可以正常解压输出。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)