java.util.zip.DataFormatException: incorrect header check 异常问题及解决方法

java.util.zip.DataFormatException: incorrect header check 异常问题及解决方法,第1张

java.util.zip.DataFormatException: incorrect header check 异常问题及解决方法 Java 解压 Node.js DeflaterRaw 压缩字符串 报无标头解决方案

前端 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);
    }

更改后就可以正常解压输出。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5682881.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存