用c#实现哈夫曼压缩算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    /// <summary>
    /// hash压缩算法
    /// </summary>
    /// <param name="file"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public async Task<IActionResult> HashCompressAsync(IFormFile file, CancellationToken cancellationToken)
    {
        try
        {
            // 检查文件是否为空
            if (file == null || file.Length == 0)
            {
                return null;
            }
 
            // 读取传入文件的所有的数据
            byte[] fileData = new byte[file.Length];
 
            using (var stream = file.OpenReadStream())
            {
                await stream.ReadAsync(fileData, cancellationToken);
            }
 
            // 统计字符大小(字符频率)
            Dictionary<byte, int> dictionary = new Dictionary<byte, int>();
 
            foreach (var item in fileData)
            {
                // 判断字典中字符是否存在
                if (dictionary.ContainsKey(item))
                {
                    // 如果有就叠加
                    dictionary[item]++;
                }
                else
                {
                    dictionary[item] = 1;
                }
            }
 
            // 构建二叉树
            var towTree = BuildHuffmanTree(dictionary,cancellationToken);
 
            // 生成编码
            var codeTable = new Dictionary<byte,string>();
            GenerateCodeTable(towTree, "", codeTable);
 
            // 进行压缩
            var compressedData = CompressFileAsync(fileData,codeTable);
 
            // 将字节数组转换为十六进制字符串,并去除其中的"-"分隔符
            string compressedDataString = BitConverter.ToString(compressedData).Replace("-", "");
 
            Console.WriteLine(compressedDataString);
 
            var streamData = new MemoryStream();
 
            // 将压缩数据转换为字节数组,使用UTF-8编码
            byte[] compressedBytes = Encoding.UTF8.GetBytes(compressedDataString);
 
            // 将字节数组写入MemoryStream
            streamData.Write(compressedBytes, 0, compressedBytes.Length);
 
            await streamData.FlushAsync();
 
            // 将流的位置设置为0,以便从头开始读取数据
            streamData.Seek(0, SeekOrigin.Begin);
 
            // 返回一个文件
            var fileStreamResult = new FileStreamResult(streamData, "text/plain")
            {
                FileDownloadName = "compressedFile"
            };
            return fileStreamResult;
        }
        catch (Exception  ex)
        {
            throw ex;
        }
    }
 
    /// <summary>
    /// 构建二叉树
    /// </summary>
    /// <param name="dictionary"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    private  HuffmanNode BuildHuffmanTree(Dictionary<byte, int> dictionary, CancellationToken cancellationToken)
    {
        // 按照字符频率排序
        var hash = new PriorityQueue<HuffmanNode, int>(Comparer<int>.Create((x, y) => y - x));
 
        // 遍历字典,将每个字节及其频率作为节点加入优先队列
        foreach (var item in dictionary)
        {
            hash.Enqueue(new HuffmanNode { Value = item.Key, Frequency = item.Value }, item.Value);
        }
 
        // 构建树
        do
        {
            // 从优先队列中取出频率最小的两个节点作为左右子节点
            var Left = hash.Dequeue();
            var Right = hash.Dequeue();
 
            // 创建一个新的节点,其频率为左右子节点的频率之和,并将左右子节点分别设置为新节点的左右子节点
            var data = new HuffmanNode
            {
                Frequency = Left.Frequency + Right.Frequency,
                Left = Left,
                Right = Right
            };
 
            // 将新节点加入优先队列
            hash.Enqueue(data,data.Frequency);
 
            // 当优先队列中只剩下一个节点时,结束循环
        } while (hash.Count>1);
 
        return hash.Dequeue();
    }
 
    /// <summary>
    /// 生成编码表
    /// </summary>
    /// <param name="node"></param>
    /// <param name="code"></param>
    /// <param name="codeTable"></param>
    public void GenerateCodeTable(HuffmanNode node, string code, Dictionary<byte, string> codeTable)
    {
        // 如果节点的值存在
        if (node.Value.HasValue)
        {
            // 将节点的值作为键,编码作为值存储到编码表中
            codeTable[node.Value.Value] = code;
        }
        else
        {
            // 递归处理左右子树,编码加上"0","1"
            GenerateCodeTable(node.Left, code + "0", codeTable);
            GenerateCodeTable(node.Right, code + "1", codeTable);
        }
    }
 
    /// <summary>
    /// 用编码表对文件数据进行压缩
    /// </summary>
    /// <param name="data"></param>
    /// <param name="codeTable"></param>
    /// <returns></returns>
    public static byte[] CompressFileAsync(byte[] data, Dictionary<byte, string> codeTable)
    {
        var compressedBytes = new List<byte>();
 
        foreach (var item in data)
        {
            // 获取当前字节的编码
            var code = codeTable[item];
 
            // 将编码添加到 compressedBytes 中
            foreach (var c in code)
            {
                if (c == '0')
                {
                    compressedBytes.Add(0);
                }
                else if (c == '1')
                {
                    compressedBytes.Add(1);
                }
            }
        }
 
        // 返回压缩后的字节数组
        return compressedBytes.ToArray();
    }
 
}
 
/// <summary>
/// 哈夫曼树节点类
/// </summary>
public class HuffmanNode
{
    // 节点值
    public byte? Value { get; set; }
 
    // 节点频率
    public int Frequency { get; set; }
 
    // 左子节点
    public HuffmanNode Left { get; set; }
 
    // 右子节点
    public HuffmanNode Right { get; set; }
}

  

posted @   XiangdxDu  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示