Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #!/bin/python
  2. import base64
  3. import zlib
  4. import sys
  5.  
  6. """
  7. name: data_compressor.py
  8. description: Lossless data compression code snippet with the help of zlib DEFLATE (Lempel–Ziv 1977) compression algorithm.
  9. author: Bryan Angelo Pedrosa
  10. """
  11.  
  12. class data_compress:
  13. def __init__(self,location):
  14. self.location = location
  15. def mainsys(self):
  16. with open(self.location,"rb") as f:
  17. filedata = zlib.compress(base64.b64encode(f.read()))#read a file; encode with base64; compress (add a buffer if needed)
  18. return filedata
  19. class data_decompress:
  20. def __init__(self,location):
  21. self.location = location
  22. def mainsys(self):
  23. with open(self.location,"rb") as f:
  24. filedata = zlib.decompress(base64.b64encode(f.read()))#read a file; encode with base64; decompress (add a buffer if needed)
  25. return filedata
  26. if __name__ == "__main__":
  27. if len(sys.argv) == 3:
  28. if sys.argv[1] == "compress":
  29. print(data_compress(sys.argv[2]).mainsys())
  30. elif sys.argv[1] == "decompress":
  31. print(data_decompress(sys.argv[2]).mainsys())
  32. else:
  33. print("argument must be 3, but you entered %s" % len(sys.argvprogram
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement