-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_coding.py
executable file
·52 lines (38 loc) · 1.56 KB
/
arithmetic_coding.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
#!/usr/bin/env python
import os
import argparse
from coding.encoder import ArithmeticEncoder
from coding.decoder import ArithmeticalDecoder
from file_operations.reader import FileReader
from file_operations.writer import FileWriter
from termcolor import cprint
def compress(file_in, file_out):
cprint(f'Input file size: {os.stat(file_in).st_size}', 'magenta')
with open(file_in, 'rb') as f:
content = f.read()
ae = ArithmeticEncoder(content)
content_fraction, length, symbols_dict = ae.encode()
fw = FileWriter(file_out)
fw.write(content_fraction, length, symbols_dict)
cprint(f'Output file size: {os.stat(file_out).st_size}', 'green')
def decompress(file_in, file_out):
cprint(f'Input file size: {os.stat(file_in).st_size}', 'cyan')
fr = FileReader(file_in)
content_fraction, length, symbols_dict = fr.read()
ad = ArithmeticalDecoder(content_fraction, length, symbols_dict)
decoded_content = ad.decode()
with open(file_out, 'wb') as f:
f.write(decoded_content)
cprint(f'Output file size: {os.stat(file_out).st_size}', 'blue')
def main():
action_dict = dict(compress=compress, decompress=decompress)
parser = argparse.ArgumentParser(description='Compress/Decompress files using arithmetic coding algorithm.')
parser.add_argument('action', choices=action_dict.keys())
parser.add_argument('input')
parser.add_argument('output')
args = parser.parse_args()
action = args.action
params = [args.input, args.output]
action_dict[action](*params)
if __name__ == '__main__':
main()