惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
MyScale Blog
MyScale Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
罗磊的独立博客
G
Google Developers Blog
Y
Y Combinator Blog
博客园 - 【当耐特】
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
美团技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
小众软件
小众软件
T
Tailwind CSS Blog
A
About on SuperTechFans

博客园 - 生命体验之kevin-Y

windbg内存占用分析常用命令 如何让Trae IDE调用windbg来分析dump文件 Avalonia.Controls.DataGrid自动合并列 asp.net core如何实现Controller热更新 windbg无法分析dotnetframework3.5的dmp asp.net core通过配置决定AddSingleton的实现类 vscode指定python文件的执行程序 golang离线开发-windows-vscode go语言学习 - caddy配置的一些发现 go语言学习 - caddy添加nacos-gateway 学习jsp-使用IDEA2024社区版 ravendb源代码学习一:服务启动 修整程序集需要 .NET Core 3.0 或更高版本。 打开电脑后一直弹出“交互式检测服务”窗口 Idea2024-java-Maven开发配置 C#获取事件绑定的方法 Build Secure Web Services With SOAP Headers and Extensions “System.Net.Http.HttpContent”不包含“ReadAsAsync”的定义 C#入门:如何合理制定方法参数-下 C#入门:如何合理制定方法参数-上 拷贝对象的开源工具类-FastMapper-TinyMapper-Mapster
java.util.zip.DataFormatException: incorrect header check
生命体验之kevin-Y · 2024-01-18 · via 博客园 - 生命体验之kevin-Y

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

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