Skip to content
Snippets Groups Projects
Commit e7f9e7ed authored by Jacob Theisen's avatar Jacob Theisen
Browse files

Cleaning

parent 0f15f6e2
Branches
No related tags found
No related merge requests found
File added
with open('./compressed_text','rb') as file: with open('./LZ_compressed_text','rb') as file:
tekst = file.read() tekst = file.read()
class Table_object(object): class Table_object(object):
...@@ -117,19 +117,19 @@ for i in range(0 ,len(bit_string), 8): ...@@ -117,19 +117,19 @@ for i in range(0 ,len(bit_string), 8):
num_of_bytes = len(bytes) num_of_bytes = len(bytes)
with open('./trash/huffmann_test_file_comp', 'w') as file: with open('./HM_compressed_text', 'w') as file:
for i in rep_tree_arr: for i in rep_tree_arr:
file.write(f'{i.symb}{i.val}\n') file.write(f'{i.symb}{i.val}\n')
file.write(f'{num_of_bytes} {rest} END TABLE \n') file.write(f'{num_of_bytes} {rest} END TABLE \n')
with open('./trash/huffmann_test_file_comp', 'ab') as file: with open('./HM_compressed_text', 'ab') as file:
for i in bytes: for i in bytes:
file.write(i) file.write(i)
with open('./trash/huffmann_test_file_comp', 'ab') as file: with open('./HM_compressed_text', 'ab') as file:
for i in all_lz_bytes: for i in all_lz_bytes:
file.write(i) file.write(i)
File moved
import string import string
with open('./trash/huffmann_test_file_comp', 'rb') as file: with open('./HM_compressed_text', 'rb') as file:
text = file.read() text = file.read()
...@@ -15,19 +15,15 @@ while True: ...@@ -15,19 +15,15 @@ while True:
try: try:
meta_data += text[i:i+1].decode('UTF-8') meta_data += text[i:i+1].decode('UTF-8')
i += 1 i += 1
print('ascii')
except: except:
try: try:
meta_data += text[i:i+2].decode('UTF-8') meta_data += text[i:i+2].decode('UTF-8')
i += 2 i += 2
print('extended ascii')
except: except:
meta_data += text[i:i+3].decode('UTF-8') meta_data += text[i:i+3].decode('UTF-8')
i += 3 i += 3
print('nor')
text = text[i+2:] text = text[i+2:]
print(meta_data)
meta_data = meta_data.split('\n') meta_data = meta_data.split('\n')
rep_tree_arr = [] rep_tree_arr = []
...@@ -86,13 +82,13 @@ for k in range(len(bit_string)): ...@@ -86,13 +82,13 @@ for k in range(len(bit_string)):
full_code = b'' full_code = b''
next_chars = 0 next_chars = 0
with open('./hm_uncomp', 'w') as file: with open('./HM_uncompressed_text', 'w') as file:
file.write('') file.write('')
for i in range(0,len(lz_code),6): for i in range(0,len(lz_code),6):
counter = 0 counter = 0
value = lz_code[i:i+2] value = lz_code[i:i+2]
value = int.from_bytes(value, 'little') value = int.from_bytes(value, 'little')
with open('./hm_uncomp', 'ab') as file: with open('./HM_uncompressed_text', 'ab') as file:
file.write(value.to_bytes(2, 'little')) file.write(value.to_bytes(2, 'little'))
j = 0 j = 0
a = len(huff_string[:value]) a = len(huff_string[:value])
...@@ -106,16 +102,13 @@ for i in range(0,len(lz_code),6): ...@@ -106,16 +102,13 @@ for i in range(0,len(lz_code),6):
a -=1 a -=1
counter += 1 counter += 1
j += 1 j += 1
#print(huff_string[:value-counter], counter) with open('./HM_uncompressed_text', 'a') as file:
with open('./hm_uncomp', 'a') as file:
file.write(huff_string[:value-counter]) file.write(huff_string[:value-counter])
huff_string = huff_string[value-counter:] huff_string = huff_string[value-counter:]
if i + 4 < len(lz_code): if i + 4 < len(lz_code):
with open('./hm_uncomp', 'ab') as file: with open('./HM_uncompressed_text', 'ab') as file:
file.write(lz_code[i+2:i+4]) file.write(lz_code[i+2:i+4])
file.write(lz_code[i+4:i+6]) file.write(lz_code[i+4:i+6])
# with open('./hm_uncomp', 'rb') as file:
# print(file.read())
\ No newline at end of file
File moved
import string import string
with open('./test_files/fil.txt') as file: with open('./fil.txt') as file:
tekst = file.read() tekst = file.read()
...@@ -72,7 +72,7 @@ def run_comp(): ...@@ -72,7 +72,7 @@ def run_comp():
#run func and print result #run func and print result
a = run_comp() a = run_comp()
with open('./compressed_text', 'w') as file: with open('./LZ_compressed_text', 'w') as file:
file.write('') file.write('')
#print(a) #print(a)
...@@ -89,10 +89,10 @@ while True: ...@@ -89,10 +89,10 @@ while True:
if a[i+len_of_int] == ']': if a[i+len_of_int] == ']':
break break
len_of_int +=1 len_of_int +=1
with open('./compressed_text', 'ab') as file: with open('./LZ_compressed_text', 'ab') as file:
int1 = int(a[i:i+len_of_int]) int1 = int(a[i:i+len_of_int])
file.write(int1.to_bytes(2,'little')) file.write(int1.to_bytes(2,'little'))
with open('./compressed_text', 'a') as file: with open('./LZ_compressed_text', 'a') as file:
#print(a[i + len_of_int+1:i + len_of_int+1+int1]) #print(a[i + len_of_int+1:i + len_of_int+1+int1])
for j in a[i + len_of_int+1:i + len_of_int+1+int1]: for j in a[i + len_of_int+1:i + len_of_int+1+int1]:
if j not in string.printable and j not in extended_string: if j not in string.printable and j not in extended_string:
...@@ -121,7 +121,7 @@ while True: ...@@ -121,7 +121,7 @@ while True:
break break
len_of_backtrack +=1 len_of_backtrack +=1
len_of_backtrack_value = int(a[i:len_of_backtrack]) len_of_backtrack_value = int(a[i:len_of_backtrack])
with open('./compressed_text', 'ab') as file: with open('./LZ_compressed_text', 'ab') as file:
file.write(backtrack_value.to_bytes(2,'little')) file.write(backtrack_value.to_bytes(2,'little'))
file.write(len_of_backtrack_value.to_bytes(2,'little')) file.write(len_of_backtrack_value.to_bytes(2,'little'))
togle_append_clear_text = True togle_append_clear_text = True
......
File moved
with open('./hm_uncomp', 'rb') as file: with open('./HM_uncompressed_text', 'rb') as file:
tekst = file.read() tekst = file.read()
...@@ -34,7 +34,7 @@ def dcomp(): ...@@ -34,7 +34,7 @@ def dcomp():
string = dcomp() string = dcomp()
with open('./uncompressed_text', 'w') as file: with open('./LZ_uncompressed_text', 'w') as file:
file.write(string) file.write(string)
......
Lempel Ziv og Huffman komprimering
Komprimeringen blir gjort på filen './fil.txt' for å endre dette kan du endre på 3. linje i LZ_compression.py
Komprimeringen foregår i 2 steg:
1. LZ_compression.py komprimerer innholdet i fil.txt med lempel ziv til filen LZ_compressed_text
2. HM_compression.py komprimerer innhodet i LZ_compressed_text til filen HM_compressed_text
Når komprimeringen er ferdig har man fått en fil med sansynligvis vesentlig mindre størelse
Dekomprimeringen foregår i motsatte 2 steg:
1. HM_uncompression.py dekomprimerer inholdet fra HM_compressed_text til HM_uncompressed_text
2. LZ_uncompression.py dekomprimerer innholdet fra HM_uncompressed_text til LZ_uncompressed_text
Når dekomprimeringen er ferdig sitter man igjen med LZ_uncompressed_text som skal være identisk med fil.txt
This diff is collapsed.
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment