RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1526139
Accepted
Rootware
Rootware
Asked:2023-06-17 15:09:09 +0000 UTC2023-06-17 15:09:09 +0000 UTC 2023-06-17 15:09:09 +0000 UTC

用于 C# 的 Java DeflaterInputStream 和 DeflaterOutputStream

  • 772

告诉我,对于 .NET Framework 4.6.1 及以下版本,在 C# 中是否有正常的zlib实现?面临文本压缩的问题。该算法的GitHub上的算法的zlib实现的任何测试版本都正常将字节数组解包为文本,但打包后与原始版本相比,前2个字节的字节数组不同,最后还有大约 6 个字节,这最终使它在后续解包时被破坏。同时,在 Java 版本中,一切正常。打包后的文字与拆包和打包前的原件完全一致。换句话说,以具有打包文本的字节数组为例,在解包和打包操作后,我无法使用 .NET 工具在输出中获得相同的字节数组。

在此处输入图像描述

测试表单代码:

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Test_Compression.utils;
using zlib;

namespace Test_Compression
{
    public partial class Form1 : Form
    {
        // Source code of ZLIB
        // http://www.componentace.com/zlib_.NET.htm

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            byte[] source = File.ReadAllBytes("original.bin");

            string sourceHex = HexUtil.printData(source);
            this.richTextBox1.Text = sourceHex;

            byte[] TextBlock;
            DecompressData(source, out TextBlock);

            string text = Encoding.Default.GetString(TextBlock);
            this.richTextBox3.Text = text;

            byte[] CompressedBlock;
            CompressData(TextBlock, out CompressedBlock);

            string compressedHex = HexUtil.printData(CompressedBlock);
            this.richTextBox2.Text = compressedHex;
        }

        public static void CompressData(byte[] inData, out byte[] outData)
        {
            using (MemoryStream outMemoryStream = new MemoryStream())
            using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
            using (Stream inMemoryStream = new MemoryStream(inData))
            {
                CopyStream(inMemoryStream, outZStream);
                outZStream.finish();
                outData = outMemoryStream.ToArray();
            }
        }

        public static void DecompressData(byte[] inData, out byte[] outData)
        {
            using (MemoryStream outMemoryStream = new MemoryStream())
            using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream))
            using (Stream inMemoryStream = new MemoryStream(inData))
            {
                CopyStream(inMemoryStream, outZStream);
                outZStream.finish();
                outData = outMemoryStream.ToArray();
            }
        }

        public static void CopyStream(Stream input, Stream output)
        {
            byte[] buffer = new byte[2000];

            int len;
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                output.Write(buffer, 0, len);

            output.Flush();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Clear();
            this.richTextBox2.Clear();
            this.richTextBox3.Clear();
        }
    }
}

我附上一个项目来检查开箱和打包。

https://disk.yandex.ru/d/HpBzcz4QBWRUkA

c#
  • 1 1 个回答
  • 74 Views

1 个回答

  • Voted
  1. Best Answer
    aepot
    2023-06-17T18:10:27Z2023-06-17T18:10:27Z

    我阅读了zlib 说明并意识到您根本没有Z_SYNC_FLUSH.

    也就是这里是解决方法,需要在压缩的时候指定

    outZStream.FlushMode = zlibConst.Z_SYNC_FLUSH;
    

    结果是:

    private void buttonRun_Click(object sender, EventArgs e)
    {
        byte[] source = File.ReadAllBytes("original.bin");
    
        string sourceHex = HexUtil.printData(source);
        this.richTextBox1.Text = sourceHex;
    
        byte[] TextBlock = DecompressData(source);
    
        string text = Encoding.UTF8.GetString(TextBlock);
        this.richTextBox3.Text = text;
    
        byte[] CompressedBlock = CompressData(TextBlock);
    
        string compressedHex = HexUtil.printData(CompressedBlock);
        this.richTextBox2.Text = compressedHex;
    }
    
    public static byte[] CompressData(byte[] inData)
    {
        using (Stream inMemoryStream = new MemoryStream(inData))
        using (MemoryStream outMemoryStream = new MemoryStream())
        using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
        {
            outZStream.FlushMode = zlibConst.Z_SYNC_FLUSH;
            CopyStream(inMemoryStream, outZStream);
            outZStream.finish();
            return outMemoryStream.ToArray();
        }
    }
    
    public static byte[] DecompressData(byte[] inData)
    {
        using (Stream inMemoryStream = new MemoryStream(inData))
        using (MemoryStream outMemoryStream = new MemoryStream())
        using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream)) 
        {
            CopyStream(inMemoryStream, outZStream);
            outZStream.finish();
            return outMemoryStream.ToArray();
        }
    }
    
    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[8192];
    
        int len;
        while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
            output.Write(buffer, 0, len);
    }
    

    但是当然这个图书馆很奇怪。顺便说一下,您可以不通过手动连接,而是将其安装为 NuGet 包,它在那里称为zlib.net。

    在此处输入图像描述


    顺便说一句,这就是我们在完全没有第三方库的情况下为 .NET 7 设法实现的目标。ZLibStream自 BCL 中的 .NET 6 以来,该类就可用了。

    static void Main(string[] args)
    {
        byte[] source = File.ReadAllBytes("original.bin");
    
        string sourceHex = HexUtil.printData(source);
        Console.WriteLine(sourceHex);
    
        byte[] bytes = DecompressData(source);
    
        string text = Encoding.UTF8.GetString(bytes);
        Console.WriteLine(/* text */);
    
        byte[] compressed = CompressData(bytes);
    
        string compressedHex = HexUtil.printData(compressed);
        Console.WriteLine(compressedHex);
    }
    
    public static byte[] CompressData(byte[] bytes)
    {
        MemoryStream result = new();
        using (MemoryStream input = new(bytes))
        using (ZLibStream zs = new(result, CompressionLevel.Optimal))
        {
            input.CopyTo(zs);
            zs.Flush();
        }
        return result.ToArray();
    }
    
    public static byte[] DecompressData(byte[] bytes)
    {
        MemoryStream result = new();
        using (MemoryStream input = new(bytes))
        using (ZLibStream zs = new(input, CompressionMode.Decompress))
        {
            zs.CopyTo(result);
        }
        return result.ToArray();
    }
    

    结论是这样的

    0000: 78 9c 8a 0e 49 2d 2e 89 e5 e5 02 51 01 89 45 89    x...I-.....Q..E.
    0010: b9 86 b6 7e f9 79 a9 20 01 08 3f b5 a8 28 b5 bc    ...~.y. ..?..(..
    0020: 28 15 21 8c a1 0e 4d 80 97 2b 1a b7 99 c5 25 40    (.!...M..+....%@
    0030: 7e 21 10 60 d7 8f cb bc 8c 74 3c 26 02 05 8a d3    ~!.`.....t<&....
    0040: 70 39 08 ca 07 59 49 82 23 03 12 2b ca 92 2b ca    p9...YI.#..+..+.
    0050: 20 a2 70 41 b8 a2 4a 98 aa f2 72 60 d0 00 31 ba     .pA..J...r`..1.
    0060: 73 b3 f0 3b 97 68 01 a8 43 d3 89 31 0d 43 41 25    s..;.h..C..1.CA%
    0070: ae c8 c1 6b 1c 86 f1 e9 59 19 84 62 3b 03 c5 44    ...k....Y..b;..D
    0080: 42 9e 4b cf c8 42 12 a3 82 0b 29 70 5e 49 69 65    B.K..B....)p^Iie
    0090: 49 29 7e 23 20 fc bc dc e4 b2 a4 e4 d1 68 1e 8d    I)~# ........h..
    00A0: 66 fa 44 b3 89 09 66 44 9b 9a 99 62 08 82 84 08    f.D...fD...b....
    00B0: 9b 6a 6a 36 f8 e3 3b 1d 9f 7b 48 70 a0 99 19 45    .jj6..;..{Hp...E
    00C0: 71 0e 00 00 00 ff ff 03 00 ec 30 50 6c             q.........0Pl
    
    0000: 78 9c 8a 0e 49 2d 2e 89 e5 e5 02 51 01 89 45 89    x...I-.....Q..E.
    0010: b9 86 b6 7e f9 79 a9 20 01 08 3f b5 a8 28 b5 bc    ...~.y. ..?..(..
    0020: 28 15 21 8c a1 0e 4d 80 97 2b 1a b7 99 c5 25 40    (.!...M..+....%@
    0030: 7e 21 10 60 d7 8f cb bc 8c 74 3c 26 02 05 8a d3    ~!.`.....t<&....
    0040: 70 39 08 ca 07 59 49 82 23 03 12 2b ca 92 2b ca    p9...YI.#..+..+.
    0050: 20 a2 70 41 b8 a2 4a 98 aa f2 72 60 d0 00 31 ba     .pA..J...r`..1.
    0060: 73 b3 f0 3b 97 68 01 a8 43 d3 89 31 0d 43 01 dc    s..;.h..C..1.C..
    0070: 95 e8 fe c6 6b 1c 86 f1 e9 59 19 84 62 3b 03 c5    ....k....Y..b;..
    0080: 44 42 9e 4b cf c8 42 12 a3 82 0b 29 70 5e 49 69    DB.K..B....)p^Ii
    0090: 65 49 29 7e 23 20 fc bc dc e4 b2 a4 e4 d1 68 1e    eI)~# ........h.
    00A0: 8d 66 fa e4 66 13 13 cc fc 6c 6a 66 8a 21 08 12    .f..f....ljf.!..
    00B0: 22 6c aa a9 d9 e0 cf d6 e9 f8 dc 43 82 03 cd cc    "l.........C....
    00C0: 28 ca da 00 00 00 00 ff ff 03 00 ec 30 50 6c       (...........0Pl
    

    可以看出是不一样的,但是打包解包都妥妥的。据我了解,这是由于 zlib 中 deflate 算法的实现版本不同所致。这有点不被认为是一个问题,因为解包器的工作在任何地方都是一样的,唯一的区别在于打包器。

    • 3

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5