-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
51 lines (41 loc) · 1.29 KB
/
decoder.py
File metadata and controls
51 lines (41 loc) · 1.29 KB
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
# import sys
# import struct
# from sys import argv
import shutil
from struct import *
def Decompression(ipt, fileName):
input_file = ipt
n = 64
maximum_table_size = pow(2,int(n))
file = open(input_file, "rb")
compressed_data = []
next_code = 256
decompressed_data = ""
string = ""
while True:
rec = file.read(2)
if len(rec) != 2:
break
(data, ) = unpack('>H', rec)
compressed_data.append(data)
dictionary_size = 256
dictionary = dict([(x, chr(x)) for x in range(dictionary_size)])
for code in compressed_data:
if not (code in dictionary):
dictionary[code] = string + (string[0])
decompressed_data += dictionary[code]
if not(len(string) == 0):
dictionary[next_code] = string + (dictionary[code][0])
next_code += 1
string = dictionary[code]
fileName = fileName.split(".")[0]
output_file = open(fileName + "_decoded.txt", "w")
for data in decompressed_data:
output_file.write(data)
output_file.close()
file.close()
# -- move to directory
current_path = "./" + fileName + "_decoded.txt"
new_path = "./decompressed/" + fileName + "_decoded.txt"
shutil.move(current_path, new_path)
return new_path