-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.test.py
More file actions
30 lines (25 loc) · 889 Bytes
/
file.test.py
File metadata and controls
30 lines (25 loc) · 889 Bytes
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
import classBased as cb
Huffboth = cb.Huffboth()
import os
file = "sample-file.txt"
def _to_Bytes(data):
b = bytearray()
for i in range(0, len(data), 8):
b.append(int(data[i:i+8], 2))
return bytes(b)
with open(file, 'rb') as r:
content = r.read()
encoded_text = Huffboth.Encode(str(content))
decoded_text = Huffboth.Decode(encoded_text)
if(str(content) == decoded_text):
print("File Compression and Decompression Sucessful!")
else:
print("Failed!")
fileOP = open("compressed_file.bin", "wb")
fileOP.write(_to_Bytes(encoded_text))
_o = os.path.getsize('sample-file.txt')
_c = os.path.getsize('compressed_file.bin')
print(f'Original file: {_o} bytes')
print(f'Compressed file: {_c} bytes')
print('Compressed file to about {}% of original'.format(round((((_o-_c)/_o)*100), 0)))
fileOP.close()