#!/bin/bash
# ideone boilerplate - we can't write files in the home directory;
# so create a temporary directory for our files instead
t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit
trap 'rm -rf "$t"' ERR EXIT
cd "$t"
cat <<\____ >gz.py
import struct
def gzinfo(filename):
# Copy+paste from gzip.py line 16
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
with open(filename, 'rb') as fp:
# Basically copy+paste from GzipFile module line 429f
magic = fp.read(2)
if magic == b'':
return False
if magic != b'\037\213':
raise ValueError('Not a gzipped file (%r)' % magic)
method, flag, _last_mtime = struct.unpack("<BBIxx", fp.read(8))
if method != 8:
raise ValueError('Unknown compression method')
if flag & FEXTRA:
# Read & discard the extra field, if present
extra_len, = struct.unpack("<H", fp.read(2))
fp.read(extra_len)
if flag & FNAME:
fname = []
while True:
s = fp.read(1)
if not s or s==b'\000':
break
fname.append(s.decode('latin-1'))
return ''.join(fname)
def main():
from sys import argv
for filename in argv[1:]:
print(filename, gzinfo(filename))
if __name__ == '__main__':
main()
____
echo hello >hello.txt
gzip -9 hello.txt
mv hello.txt.gz test.gz
python3 ./gz.py ./test.gz /dev/null
IyEvYmluL2Jhc2gKCiMgaWRlb25lIGJvaWxlcnBsYXRlIC0gd2UgY2FuJ3Qgd3JpdGUgZmlsZXMgaW4gdGhlIGhvbWUgZGlyZWN0b3J5OwojIHNvIGNyZWF0ZSBhIHRlbXBvcmFyeSBkaXJlY3RvcnkgZm9yIG91ciBmaWxlcyBpbnN0ZWFkCnQ9JChta3RlbXAgLWQgLXQgaWRlb25lLlhYWFhYWFhYWFhYWCkgfHwgZXhpdAp0cmFwICdybSAtcmYgIiR0IicgRVJSIEVYSVQKY2QgIiR0IgoKY2F0IDw8XF9fX18gPmd6LnB5CmltcG9ydCBzdHJ1Y3QKCgpkZWYgZ3ppbmZvKGZpbGVuYW1lKToKICAgICMgQ29weStwYXN0ZSBmcm9tIGd6aXAucHkgbGluZSAxNgogICAgRlRFWFQsIEZIQ1JDLCBGRVhUUkEsIEZOQU1FLCBGQ09NTUVOVCA9IDEsIDIsIDQsIDgsIDE2CiAgICAKICAgIHdpdGggb3BlbihmaWxlbmFtZSwgJ3JiJykgYXMgZnA6CiAgICAgICAgIyBCYXNpY2FsbHkgY29weStwYXN0ZSBmcm9tIEd6aXBGaWxlIG1vZHVsZSBsaW5lIDQyOWYKICAgICAgICBtYWdpYyA9IGZwLnJlYWQoMikKICAgICAgICBpZiBtYWdpYyA9PSBiJyc6CiAgICAgICAgICAgIHJldHVybiBGYWxzZQoKICAgICAgICBpZiBtYWdpYyAhPSBiJ1wwMzdcMjEzJzoKICAgICAgICAgICAgcmFpc2UgVmFsdWVFcnJvcignTm90IGEgZ3ppcHBlZCBmaWxlICglciknICUgbWFnaWMpCgogICAgICAgIG1ldGhvZCwgZmxhZywgX2xhc3RfbXRpbWUgPSBzdHJ1Y3QudW5wYWNrKCI8QkJJeHgiLCBmcC5yZWFkKDgpKQoKICAgICAgICBpZiBtZXRob2QgIT0gODoKICAgICAgICAgICAgcmFpc2UgVmFsdWVFcnJvcignVW5rbm93biBjb21wcmVzc2lvbiBtZXRob2QnKQoKICAgICAgICBpZiBmbGFnICYgRkVYVFJBOgogICAgICAgICAgICAjIFJlYWQgJiBkaXNjYXJkIHRoZSBleHRyYSBmaWVsZCwgaWYgcHJlc2VudAogICAgICAgICAgICBleHRyYV9sZW4sID0gc3RydWN0LnVucGFjaygiPEgiLCBmcC5yZWFkKDIpKQogICAgICAgICAgICBmcC5yZWFkKGV4dHJhX2xlbikKICAgICAgICBpZiBmbGFnICYgRk5BTUU6CiAgICAgICAgICAgIGZuYW1lID0gW10KICAgICAgICAgICAgd2hpbGUgVHJ1ZToKICAgICAgICAgICAgICAgIHMgPSBmcC5yZWFkKDEpCiAgICAgICAgICAgICAgICBpZiBub3QgcyBvciBzPT1iJ1wwMDAnOgogICAgICAgICAgICAgICAgICAgIGJyZWFrCiAgICAgICAgICAgICAgICBmbmFtZS5hcHBlbmQocy5kZWNvZGUoJ2xhdGluLTEnKSkKICAgICAgICAgICAgcmV0dXJuICcnLmpvaW4oZm5hbWUpCiAgICAgICAgCmRlZiBtYWluKCk6CiAgICBmcm9tIHN5cyBpbXBvcnQgYXJndgogICAgZm9yIGZpbGVuYW1lIGluIGFyZ3ZbMTpdOgogICAgICAgIHByaW50KGZpbGVuYW1lLCBnemluZm8oZmlsZW5hbWUpKQoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIG1haW4oKQpfX19fCgplY2hvIGhlbGxvID5oZWxsby50eHQKZ3ppcCAtOSBoZWxsby50eHQKbXYgaGVsbG8udHh0Lmd6IHRlc3QuZ3oKCnB5dGhvbjMgLi9nei5weSAuL3Rlc3QuZ3ogL2Rldi9udWxs