-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlz77.py
138 lines (127 loc) · 5.09 KB
/
lz77.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def longestSubstring(searchString, lookAheadString):
maxi = 0
index = 0
for i in range(0, len(searchString)):
longest_substring = 0
if (i== len(searchString) - len(lookAheadString) - 1 ):
break
for j in range(0, len(lookAheadString)):
if (i+j < len(searchString)):
if searchString[i+j] == lookAheadString[j]:
longest_substring = longest_substring + 1
if (maxi < longest_substring):
maxi = longest_substring
index = i
else:
break
else:
break
return maxi, index
def encode_lz77(data, searchBufferSize, lookAheadBufferSize):
encodedNums = []
encodedLengths = []
encodedLetters = []
i = 0
while i < len(data):
if i < 2:
if i == 0:
encodedNums.append(0)
encodedLengths.append(0)
encodedLetters.append(data[i])
i=i+1
continue
else:
if (i > 0 and data[i] != data[0]):
encodedNums.append(0)
encodedLengths.append(0)
encodedLetters.append(data[i])
i=i+1
continue
else:
encodedNums.append(1)
encodedLengths.append(1)
encodedLetters.append(data[i+1])
i = i + 2
continue
else:
lookAheadString = data[i:i+lookAheadBufferSize]
searchBufferindex = 0
if (i < searchBufferSize):
searchBufferindex = i
else:
searchBufferindex = searchBufferSize
searchString = data[i - searchBufferindex:i]
result = longestSubstring(searchString + lookAheadString, lookAheadString)
encodedLetter = ''
if (result[0] == len(lookAheadString)):
if (i + result[0] == len(data)):
encodedLetter = ''
else:
encodedLetter = data[i+lookAheadBufferSize]
else:
encodedLetter = lookAheadString[result[0]]
if (result[0] == 0):
encodedNums.append(0)
encodedLengths.append(0)
encodedLetters.append(encodedLetter)
else:
encodedNums.append(searchBufferindex - result[1])
encodedLengths.append(result[0])
encodedLetters.append(encodedLetter)
i = i + result[0] + 1
return encodedNums, encodedLengths, encodedLetters
def decode_lz77(encodedNums, encodedLengths, encodedLetters):
i = 0
decodedString = []
while i < len(encodedNums):
if (i == 1 and encodedLetters[i] == encodedLetters[0]):
decodedString.append(encodedLetters[i])
i = i+1
else:
if (encodedNums[i] == 0):
decodedString.append(encodedLetters[i])
else:
currentSize = len(decodedString)
for j in range(0, encodedLengths[i]):
decodedString.append(
decodedString[currentSize-encodedNums[i]+j])
decodedString.append(encodedLetters[i])
i = i+1
return decodedString
number=input ('If you have compress "1" and decompress "0" : ' )
if number=='1' :
file = input("Enter the filename you want to compress: ")
with open(file, 'r') as f:
stringToEncode = f.read()
restrictions = int(input("Do you want restrictions on searchwindow or look ahead window enter '1' if yes '0' if no: "))
if (restrictions == 0):
[encodedNums, encodedLengths, encodedLetters] = encode_lz77(stringToEncode,len(stringToEncode),len(stringToEncode))
else:
searchBufferSize = int(input("Enter the Search Buffer Size: "))
lookAheadBufferSize = int(input("Enter the look ahead buffer Size: "))
[encodedNums, encodedLengths, encodedLetters] = encode_lz77(stringToEncode, searchBufferSize, lookAheadBufferSize)
a = [encodedNums, encodedLengths, encodedLetters]
output = open("encoded.txt","w+")
size = len(a[0])
for i in range(size):
word = "<" + str(encodedNums[i]) + ":" + str(encodedLengths[i]) + ":" + encodedLetters[i] + ">"
output.write(word)
print("Compressed file generated as encoded.txt")
else:
encodedNums_d = []
encodedLengths_d = []
encodedLetters_d = []
file = input("Enter the filename you want to decompress:")
f = open(file, "r").readline()
newList = f.split(">")
for word in newList:
if word == '':
break
newword = word[1:].split(":")
encodedNums_d.append(int(newword[0]))
encodedLengths_d.append(int(newword[1]))
encodedLetters_d.append(newword[2])
decodedString = decode_lz77(encodedNums_d, encodedLengths_d, encodedLetters_d)
output = open("decoded.txt","w+")
output.write("".join(decodedString))
print("Decoded file generated as decoded.txt")