Advertisement
CSenshi

Information Theory - HW2 (Lasha)

Nov 16th, 2019
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # LZcompress
  2. from os.path import getsize as sz
  3. import sys
  4. from math import ceil, log
  5. # Args
  6. src, dst = open(sys.argv[1], 'rb'), open(sys.argv[2], 'wb+')
  7. buff_text = src.read()
  8. buff_binary = left = ''
  9. lem = {"0": 0, "1": 1}
  10. cur_index = 2
  11. # Gamma
  12. original_size = sz(sys.argv[1])
  13. arr = ''.join(["{0:{f}8b}".format(byte, f='0') for byte in buff_text])
  14. buff_binary += '0'*int(log(original_size, 2)) + \
  15.     "{0:{f}8b}".format(original_size, f='0')
  16. # Write to buffer
  17. for bit in arr:
  18.     if (left+bit) not in lem:
  19.         inbin = "{0:b}".format(lem[left])
  20.         buff_binary += (ceil(log(cur_index, 2)) - len(inbin)) * '0' + inbin
  21.         lem[left + '0'], lem[left + '1'] = lem[left], cur_index
  22.         cur_index += 1
  23.         left = bit
  24.     else:
  25.         left += bit
  26. # Padding
  27. while left not in lem:
  28.     left += '0'
  29. inbin = "{0:b}".format(lem[left])
  30. buff_binary += (ceil(log(cur_index, 2)) - len(inbin)) * '0' + inbin
  31. buff_binary += '1{}'.format('0' * (7 - len(buff_binary) % 8))
  32. # Write to file
  33. for i in range(int(len(buff_binary) / 8)):
  34.     num = int(buff_binary[i * 8:(i + 1) * 8], 2)
  35.     dst.write(num.to_bytes(1, byteorder='big'))
  36.  
  37.  
  38. # LZDecompress
  39. from math import ceil, log
  40. import os
  41. import sys
  42.  
  43. sf, df = open(sys.argv[1], 'rb'), open(sys.argv[2], 'wb+')
  44. lem = {0: "0", 1: "1"}
  45. ind = 2
  46. # read Gamma code
  47. buf = sf.read(4096)
  48. BUFTOREAD = ''.join(["{0:{f}8b}".format(byte, f='0') for byte in buf])
  49. index = BUFTOREAD.index('1')
  50. file_size = int('1{}'.format(''.join(BUFTOREAD[index + 1:2 * index + 1])), 2)
  51. # Read Lempel-Ziv coding
  52. BUFTOWRITE, BUFTOREAD = '', BUFTOREAD[2 * index + 1:]
  53. while True:
  54.     while ceil(log(len(lem), 2)) <= len(BUFTOREAD):
  55.         i = int(BUFTOREAD[:ceil(log(len(lem), 2))], 2)
  56.         BUFTOWRITE += lem[i]
  57.         BUFTOREAD = BUFTOREAD[ceil(log(len(lem), 2)):]
  58.         lem[ind], lem[i] = lem[i] + '1', lem[i] + '0'
  59.         ind += 1
  60.     buf = sf.read(4096)
  61.     if not buf:
  62.         break
  63.     BUFTOREAD += ''.join(["{0:{f}8b}".format(byte, f='0') for byte in buf])
  64. # Write to file
  65. for i in range(int(len(BUFTOWRITE) / 8)):
  66.     if file_size == 0:
  67.         break
  68.     num = int(BUFTOWRITE[i * 8:(i + 1) * 8], 2)
  69.     df.write(num.to_bytes(1, byteorder='big'))
  70.     file_size -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement