java.util.zip.DataFormatException: incorrect header check

C#保存的数据都是使用以下的压缩算法保存
        public static byte[] Zip2(byte[] content)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true))
            {
                stream.Write(content, 0, content.Length);
                stream.Close();
                return ms.ToArray();
            }
        }

问ChatGpt要了一段java解压的代码,结果提示:java.util.zip.DataFormatException: incorrect header check

怀疑base64解的byte[]有问题,因为两个IDE里,一个显示正数,一个显示负数。结果存文件比较是一致的。

ChatGpt一会说兼容一会说可能不兼容,建议我使用GZip。

查微软文档,说是行业标准

https://learn.microsoft.com/zh-cn/dotnet/api/system.io.compression.deflatestream?view=net-6.0

注解

此类表示 Deflate 算法,它是用于无损失文件压缩和解压缩的行业标准算法。 从 .NET Framework 4.5 开始,DeflateStream该类使用 zlib 库。 因此,它提供更好的压缩算法,在大多数情况下,压缩文件比早期版本的.NET Framework要小。

最后,从这文章得到一些灵感

https://www.cnblogs.com/yihuihui/p/9671131.html

原来java的Deflater的构造方法,有一个boolean值(nowrap)。尝试后发现,C#传过来的byte[],设置true就可正常解压了。以下是两段JAVA的方法都可正常解压的方法

public static byte[] decompress(byte[] compressedData) {
        Inflater inflater = new Inflater(true);
        inflater.setInput(compressedData);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length);
        byte[] buffer = new byte[1024];
        try {
            while (!inflater.finished()) {
                int count = inflater.inflate(buffer);
                outputStream.write(buffer, 0, count);
            }
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return outputStream.toByteArray();
    }
private static byte[] decompress2(byte[] bts) {       
        ByteArrayInputStream bin = new ByteArrayInputStream(bts);
        InflaterInputStream infIn = new InflaterInputStream(bin,new Inflater(true));
        ByteArrayOutputStream btOut = new ByteArrayOutputStream(128);
        try {
            int b = -1;
            while ((b = infIn.read()) != -1) {
                btOut.write(b);
            }
            bin.close();
            infIn.close();
            btOut.close();
            return btOut.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

也提供一段C#能解压缩的JAVA的压缩方法

    public static byte[] compress(byte[] data) {
        byte[] output=null;
        Deflater compress = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
        //compress.reset();
        compress.setInput(data);
        compress.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
        try{
            byte[] buf = new byte[1024];
            while (!compress.finished()) {
                int cnt = compress.deflate(buf);
                bos.write(buf, 0, cnt);
            }
            output = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output;
    }

我很少写java的代码,以下方法仅供参考。应用时需自行考虑可能发生的问题。

这还有一篇文章,原来node.js的行为与C#是一样的

https://blog.csdn.net/Liuying_AD/article/details/122186851

本文只发表在博客园,请勿转载!

 
posted @ 2024-01-18 11:43  生命体验之kevin-Y  阅读(89)  评论(0编辑  收藏  举报