Guest User

Untitled

a guest
Dec 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. import gzip
  2. import base64
  3. import StringIO
  4.  
  5. # python bgzip.py
  6. # H4sIAErhF1wC/8tIzcnJBwCGphA2BQAAAA==
  7. # hello
  8.  
  9. def gzip_and_base64(s):
  10. out = StringIO.StringIO()
  11.  
  12. with gzip.GzipFile(fileobj=out, mode='w') as fh:
  13. fh.write(s)
  14.  
  15. # return gzipped string base64 encoded
  16. return base64.b64encode(out.getvalue())
  17.  
  18. def base64_and_gunzip(s):
  19. decoded = base64.b64decode(s)
  20.  
  21. fo = StringIO.StringIO(decoded)
  22.  
  23. return gzip.GzipFile(fileobj=fo).read()
  24.  
  25. zipped = gzip_and_base64('hello')
  26. print(zipped)
  27. unzipped = base64_and_gunzip(zipped)
  28. print(unzipped)
Add Comment
Please, Sign In to add comment