Skip to content

Instantly share code, notes, and snippets.

@angeloped
Created October 2, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angeloped/01461c6f7f87cdd36b62f89ca101109a to your computer and use it in GitHub Desktop.
Save angeloped/01461c6f7f87cdd36b62f89ca101109a to your computer and use it in GitHub Desktop.
Lossless data compression code snippet with the help of zlib DEFLATE (Lempel–Ziv 1977) compression algorithm.
#!/bin/python
import base64
import zlib
import sys
"""
name: data_compressor.py
description: Lossless data compression code snippet with the help of zlib DEFLATE (Lempel–Ziv 1977) compression algorithm.
author: Bryan Angelo Pedrosa
"""
class data_compress:
def __init__(self,location):
self.location = location
def mainsys(self):
with open(self.location,"rb") as f:
filedata = zlib.compress(base64.b64encode(f.read()))#read a file; encode with base64; compress (add a buffer if needed)
return filedata
class data_decompress:
def __init__(self,location):
self.location = location
def mainsys(self):
with open(self.location,"rb") as f:
filedata = zlib.decompress(base64.b64encode(f.read()))#read a file; encode with base64; decompress (add a buffer if needed)
return filedata
if __name__ == "__main__":
if len(sys.argv) == 3:
if sys.argv[1] == "compress":
print(data_compress(sys.argv[2]).mainsys())
elif sys.argv[1] == "decompress":
print(data_decompress(sys.argv[2]).mainsys())
else:
print("argument must be 3, but you entered %s" % len(sys.argvprogram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment