Skip to content

Instantly share code, notes, and snippets.

@adl32x
Last active September 23, 2015 08:13
Show Gist options
  • Save adl32x/d477bdb264d84045ed8c to your computer and use it in GitHub Desktop.
Save adl32x/d477bdb264d84045ed8c to your computer and use it in GitHub Desktop.
Read a zip file and print its contents. Try to decompress .gz files before printing them. Useful for eg. looking at the contents of a Elastic Beanstalk log zip.
#!/usr/bin/env python
import sys
from zipfile import ZipFile
import gzip
import io
"""
Reads a zip file and prints it out so that you can easily grep it.
If the zip file contains .gz files, try to decompress them before
printing them out.
Installation: copy to your path.
Usage: readzip [filename]
"""
class colors:
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
ENDC = '\033[0m'
def print_zip(input_zip):
input_zip=ZipFile(input_zip)
for name in input_zip.namelist():
output = None
if name.endswith(".gz"):
compressed = io.BytesIO(input_zip.read(name))
decompressed = gzip.GzipFile(fileobj=compressed)
output = decompressed.read()
else:
output = input_zip.read(name)
lines = output.split("\n")
for line in lines:
print "%s%s%s: %s" % (colors.GREEN, name, colors.ENDC, line)
if len(sys.argv) != 2:
print "Usage: readzip [filename]"
sys.exit()
print_zip(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment