repl.it
@tsimpson1379/

Compression Algorithm-1

Python

Class for binary data with compression and decompression methods

fork
loading
Files
  • main.py
  • compression.py
main.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
import random, replit
from compression import b

# Setup
replit.clear()
print("Welcome to this demonstration of @tsimpson1379's compression module.\nYou can of course enter your own binary data,\nbut this program will randomly generate some for you.")

dataLen = 4294967296
while True:
    try:
        dataLen = int(input('How many bits long do you want the file\'s data to be? This will be randomly generated. '))
        if (4294967295 >= dataLen >= 0): break
        else: print('Enter an integer between 0 and 4,294,967,295 inclusive!')
    except: print('Enter an integer between 0 and 4,294,967,295 inclusive!')

if dataLen > 0:
    percentZero = 101
    while True:
        try: 
            percentZero = int(input('What percentage of zeros do you want in your data? (compression only makes file smaller with approx >87 or <13) '))
            if (100 >= percentZero >= 0): break
            else: print('Enter an integer between 0 and 100 inclusive!')
        except: print('Enter an integer between 0 and 100 inclusive!')
    data = ''.join([str(int(not random.randint(1, 100) in list(range(0, percentZero+1)))) for n in range(dataLen)])  # generates a string with percentZero% zeros

metadataLen = 16384
while True:
    try:
        metadataLen = int(input('How many bits long do you want the file\'s metadata to be? This will be randomly generated with roughly equal ones and zeros, and will not be compressed by the algorithm. '))
        if (16383 >= metadataLen >= 0): break
        else: print('Enter an integer between 0 and 16,383 inclusive!')
    except: print('Enter an integer between 0 and 16,383 inclusive!')
if metadataLen == 0: metadata = ''
else: metadata = "{0:b}".format(random.randint(0, (2**metadataLen)-1))

replit.clear()

# File de/compression
a = b(data, metadata)

origLen = len(a.data)
print('Original:\n'+str(a)+'\nBytes: '+str(origLen))

a.compress()
newLen = len(a.data)
print('\nCompressed:\n'+str(a) + '\nBytes: '+str(newLen)+ '\n% of original size: '+str(round((newLen/origLen)*1000)/10))

a.decompress()
print('\nDecompressed:\n'+str(a)+'\nBytes: '+ str(len(a.data)))

input('\nContinue to explanation...')
replit.clear()

# Explanation
a.compress()
a.explain()
?