-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_io.py
109 lines (83 loc) · 2.85 KB
/
my_io.py
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
import utils
import bitstring
# Get input file name
def getFilename():
print("Insira o arquivo que sofrerá compressão (com extensão):")
filename = input()
return filename
# Get a list of bytes from a file
def readBinaryFile(filename):
# Opening input file
file = open(filename, "rb")
content = []
# Reading byte by byte
while True:
byte = file.read(1)
if not byte:
break
else:
#print(byte)
content.append(byte)
return content
# Save the compressed file
def compressFile(filename, translateDict):
# Generating name of compressed file
cfilename = filename[:-4] + ".du"
# Opening files to translation
input = open(filename, "rb")
output = open(cfilename, "wb")
# Getting Huffman Tree
tree = utils.getTree(translateDict)
# Calculate total overhead size
overhead = 0
for i in range(0, len(tree)):
# Counts all bits as a single byte each
if (i % 3) == 0 and i != 0:
overhead += len(tree[i])
# A single byte
else:
overhead += 1
# Saving the Huffman Tree in the file's header
for item in tree:
output.write(item)
# Start translation of the original file
bitArray = ""
# Write into the output file using dictionary, byte by byte
while True:
byte = input.read(1)
if not byte:
break
else:
bitArray += translateDict[byte].decode("utf-8")
# Counting useless bits
artificialBits = bytes([0])
# Checking if will be necessary to add artificial bits
if (len(bitArray) % 8):
quo = int(int(len(bitArray) / 8) + 1)
artificialBits = bytes([8 * quo - len(bitArray)])
# Creating bitstream from string
deployArray = bitstring.BitArray("0b" + bitArray)
# Writing how many of the final bits are useless
output.write(artificialBits)
# Writing bitstream
output.write(deployArray.tobytes())
return [cfilename, overhead]
# Show size information
def showFileSize(option, size, overhead):
if option == 1:
print("Original File Size (bytes): " + str(size))
elif option == 2:
print("Compressed File Size (bytes): " + str(size))
print("Compressed File Size (bits) with overhead: " + str(size * 8))
print("Compressed File Size (bits) without overhead: " + str((size * 8) - (overhead * 8)))
else:
print("Decompressed File Size (bytes): " + str(size))
# Show compression ratio
def showCompression(original, compressed):
print("Compression: " + "{:.2f}".format(100 - ((compressed * 100) / original)) + "%")
# Show entropy on screen
def showEntropy(entropy):
print("Entropy: " + "{:.2f}".format(entropy))
# Show average length on screen
def showAverageLength(average):
print("Average Length: " + "{:.2f}".format(average))