-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZAP.py
206 lines (192 loc) · 5.37 KB
/
LZAP.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#encode
def encode(array):
last=256
i=0
array_len=len(array)
prev_match=b''
match=b''
out=[]
while(i<array_len):
match=bytes([array[i]])
while(match in dictionary):
i=i+1
try:
match=match+bytes([array[i]])
except:
match=match+bytes([array[i-1]])
break
match=match[:-1]
out.append(dictionary[match])
if(prev_match+match) not in dictionary:
x=match
for j in range(len(x)-1,0,-1):
dictionary[prev_match+match[:-j]]=last
last+=1
dictionary[prev_match+match]=last
last+=1
prev_match=match
return out
#preparing output (int) to chunks of bytes
def output_to_bytes(array):
i=0
bits=0
bit_chunks=0
#max length in decimal
mldec=max(array)
#max length in binary
mlbin=1
x=mldec
out=[]
while x>1:
x=x>>1
mlbin+=1
out.append(mlbin)
while i<len(array):
while bits<=mlbin:
j=array[i]
i=i+1
x=j
bit_chunks=(bit_chunks<<mlbin)|j
bits=bits+mlbin
if i>=len(array):
break
while bits>=8:
bits=bits-8
temp=bit_chunks>>(bits)
out.append(temp)
bit_chunks=bit_chunks&((2**(bits))-1)
if i>=len(array):
if(bits!=0):
padding=8-bits
bit_chunks=(bit_chunks<<(padding))|((2**padding)-1)
out.append(bit_chunks)
out.append(padding)
else:
out.append(0)
return bytes(out)
#get key of item (idx) from dictionary
def get_string(idx):
a=b''
for i in dictionary:
if dictionary[i]==idx:
a=i
return a
#decode
def decode(array):
out=b''
match=b''
prev_match=b''
last=256
c=0
try:
for i in array:
match=get_string(i)
out=out+match
x=b''
x=(prev_match)+match
if x not in dictionary:
for j in range(len(match)-1,0,-1):
dictionary[prev_match+match[:-j]]=last
last+=1
dictionary[x]=last
last+=1
prev_match=match
except:
out=out+(get_string(i))
return out
#preparing input from chunks of bytes into integer
def bytes_to_int(array):
out=[]
byte_chunk=0
bits=0
length=array[0]
padding=array[-1]
array=array[1:-1]
size=len(array)
i=0
while i<size-1:
while (bits<length) & (i<size-1):
#print(array[i])
#sleep(1)
#print(type(byte_chunk))
byte_chunk=(byte_chunk<<8)+array[i]
bits=bits+8
i+=1
# print('1',byte_chunk,bits)
while bits>=length:
bits=bits-length
temp=byte_chunk>>(bits)
out.append(temp)
byte_chunk=byte_chunk&((2**bits)-1)
byte_chunk=(byte_chunk<<8)+array[i]
byte_chunk=byte_chunk>>padding
out.append(byte_chunk)
return out
#compression proccess (inputting file, make dictionary, encoding, writing into files)
def compress():
print('You choose to compress file. Place file to compress in the same directory as this program.')
input_file=str(input("Input file (ex :file.txt): "))
f=open(file=input_file,mode='r+b')
input_=f.read()
file_name=''
for i in input_file:
if i == '.':
break
file_name=file_name+i
print("making dictionary...")
#make dictionary
dictionary={bytes([i]):i for i in range(256)}
print("\tinput size\t: ",len(input_),'bytes')
print("compressing...")
output=encode(input_)
print("converting to bytes...")
#convert output list into bytes
output=output_to_bytes(output)
print("saving file....")
print("\toutput size:\t",len(output),"bytes")
#WRITE FILE OUTPUT
f=open(file=file_name+'.LZAP',mode='w+b')
f.write(output)
f.close()
print("done!!")
#compression proccess (inputting file, make dictionary, decoding, writing into files)
def decompress():
print('You choose to decompress file. Place file to decompress in the same directory as this program.')
input_file=input(str("Input file (ex :file.LZMW): "))
file_name=''
for i in input_file:
if i == '.':
break
file_name=file_name+i
f=open(file=input_file,mode='r+b')
input_=f.read()
print("\tfile size: ",len(input_),'bytes')
print("preparing files...")
input_=bytes_to_int(input_)
print("creating dictionary...")
dictionary={bytes([i]):i for i in range(256)}
print("decompressing...")
output=decode(input_)
print("done!")
print("\toutput size:\t",len(output),'\n')
f=open(file=file_name+'_LZAP.txt',mode='w+b')
f.write(output)
f.close()
#main loop
while True:
dictionary={bytes([i]):i for i in range(256)}
try:
choice=int(input("1. compress\n2. decompress\n0. exit\n\tyour choice:"))
try:
if choice==1:
compress()
elif choice==2:
decompress()
elif choice==0:
break
else:
print("invalid input")
except:
print('cannot find file...')
except:
print("invalid input (not integer)")