-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.py
56 lines (49 loc) · 1.43 KB
/
decoder.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
# LZW Decoder
# Name: Aditya Gupta
# ID: 800966229
# ITCS 6114
import sys
from sys import argv
import struct
from struct import *
# taking the compressed file input and the number of bits from command line
# defining the maximum table size
# opening the compressed file
# defining variables
#input_file, n = argv[1:]
n = 32
input_file = "freq14_320ver.lzw"
maximum_table_size = pow(2,int(n))
file = open("my_text.lzw", "rb")
compressed_data = []
next_code = 256
decompressed_data = ""
string = ""
# Reading the compressed file.
while True:
rec = file.read(2)
if len(rec) != 2:
break
(data, ) = unpack('>H', rec)
compressed_data.append(data)
# Building and initializing the dictionary.
dictionary_size = 256
dictionary = dict([(x, chr(x)) for x in range(dictionary_size)])
# iterating through the codes.
# LZW Decompression algorithm
print(compressed_data)
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]
# storing the decompressed string into a file.
out = input_file.split(".")[0]
output_file = open(out + "_decoded.txt", "w")
for data in decompressed_data:
output_file.write(data)
output_file.close()
file.close()