应用Huffman编码技术实现对src.txt文件完成压缩和解压
时间: 2024-06-14 20:06:33 浏览: 179
利用huffman编码对文件进行压缩和解压
5星 · 资源好评率100%
以下是使用Huffman编码技术实现对src.txt文件完成压缩和解压的示例:
压缩:
```python
import heapq
import os
class HuffmanNode:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
def __lt__(self, other):
return self.freq < other.freq
def build_frequency_table(data):
frequency_table = {}
for char in data:
if char in frequency_table:
frequency_table[char] += 1
else:
frequency_table[char] = 1
return frequency_table
def build_huffman_tree(frequency_table):
heap = []
for char, freq in frequency_table.items():
node = HuffmanNode(char, freq)
heapq.heappush(heap, node)
while len(heap) > 1:
node1 = heapq.heappop(heap)
node2 = heapq.heappop(heap)
merged = HuffmanNode(None, node1.freq + node2.freq)
merged.left = node1
merged.right = node2
heapq.heappush(heap, merged)
return heap[0]
def build_encoding_table(root):
encoding_table = {}
def traverse(node, code):
if node.char:
encoding_table[node.char] = code
else:
traverse(node.left, code + "0")
traverse(node.right, code + "1")
traverse(root, "")
return encoding_table
def compress(src_file, dest_file):
with open(src_file, 'r') as file:
data = file.read().rstrip()
frequency_table = build_frequency_table(data)
huffman_tree = build_huffman_tree(frequency_table)
encoding_table = build_encoding_table(huffman_tree)
encoded_data = ""
for char in data:
encoded_data += encoding_table[char]
padding = 8 - len(encoded_data) % 8
encoded_data += "0" * padding
byte_array = bytearray()
for i in range(0, len(encoded_data), 8):
byte = encoded_data[i:i+8]
byte_array.append(int(byte, 2))
with open(dest_file, 'wb') as file:
file.write(bytes([padding]))
file.write(byte_array)
compress("src.txt", "compressed.bin")
```
解压:
```python
def decompress(src_file, dest_file):
with open(src_file, 'rb') as file:
padding = ord(file.read(1))
encoded_data = ""
byte = file.read(1)
while byte:
byte = ord(byte)
bits = bin(byte)[2:].rjust(8, '0')
encoded_data += bits
byte = file.read(1)
encoded_data = encoded_data[:-padding]
decoding_table = {v: k for k, v in encoding_table.items()}
decoded_data = ""
current_code = ""
for bit in encoded_data:
current_code += bit
if current_code in decoding_table:
char = decoding_table[current_code]
decoded_data += char
current_code = ""
with open(dest_file, 'w') as file:
file.write(decoded_data)
decompress("compressed.bin", "decompressed.txt")
```
阅读全文