fork download
  1. #!/bin/bash
  2.  
  3. # ideone boilerplate - we can't write files in the home directory;
  4. # so create a temporary directory for our files instead
  5. t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit
  6. trap 'rm -rf "$t"' ERR EXIT
  7. cd "$t"
  8.  
  9. cat <<\____ >gz.py
  10. import struct
  11.  
  12.  
  13. def gzinfo(filename):
  14. # Copy+paste from gzip.py line 16
  15. FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
  16.  
  17. with open(filename, 'rb') as fp:
  18. # Basically copy+paste from GzipFile module line 429f
  19. magic = fp.read(2)
  20. if magic == b'':
  21. return False
  22.  
  23. if magic != b'\037\213':
  24. raise ValueError('Not a gzipped file (%r)' % magic)
  25.  
  26. method, flag, _last_mtime = struct.unpack("<BBIxx", fp.read(8))
  27.  
  28. if method != 8:
  29. raise ValueError('Unknown compression method')
  30.  
  31. if flag & FEXTRA:
  32. # Read & discard the extra field, if present
  33. extra_len, = struct.unpack("<H", fp.read(2))
  34. fp.read(extra_len)
  35. if flag & FNAME:
  36. fname = []
  37. while True:
  38. s = fp.read(1)
  39. if not s or s==b'\000':
  40. break
  41. fname.append(s.decode('latin-1'))
  42. return ''.join(fname)
  43.  
  44. def main():
  45. from sys import argv
  46. for filename in argv[1:]:
  47. print(filename, gzinfo(filename))
  48.  
  49. if __name__ == '__main__':
  50. main()
  51. ____
  52.  
  53. echo hello >hello.txt
  54. gzip -9 hello.txt
  55. mv hello.txt.gz test.gz
  56.  
  57. python3 ./gz.py ./test.gz /dev/null
Success #stdin #stdout 0.03s 9380KB
stdin
Standard input is empty
stdout
./test.gz hello.txt
/dev/null False