Recursively gunzip directory or gunzip if gzipped

Simple python2 script to recursively gunzip a directory or gunzip a file after checking if a file is gzipped.

/usr/local/bin/ginr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/local/bin/python
import os
from os import path
import gzip as _gzip
import zipfile
import sys

def gunzip(gzp):
if not path.exists(gzp):
return 'No such file'

with _gzip.open(gzp, 'rb') as rf:
content = rf.read()

fp = gzp[:-3] if gzp.endswith('.gz') else gzp

with open(fp, 'wb') as wf:
wf.write(content)

return fp


def unzip(zipfn):
z = zipfile.ZipFile(zipfn, 'r')
z.extractall(zipfn.replace('.zip', ''))
z.close()


def _is_gzipped(fp):
"""
Checks if a file is gzipped or not by checking the signature in file
See http://stackoverflow.com/a/13044946
"""
with open(fp) as rf:
sig = rf.read(3)
return sig == "\x1f\x8b\x08"


def extract(fn):
if _is_gzipped(fn):
print 'Extracting: %s...' % fn
gunzip(fn)
elif zipfile.is_zipfile(fn):
unzip(fn)


def dir_iter(p):
for root, dirs, files in os.walk(p):
for f in files:
yield path.join(root, f)


if __name__ == "__main__":
argv = sys.argv[1:]

if not len(argv):
print 'Usage: ginr <path1> <path2> ...'
sys.exit(1)

for p in argv:
if not path.exists(p):
print '%s does not exist!' % p
sys.exit(1)

for p in argv:
if path.isfile(p):
extract(p)
elif path.isdir(p):
for f in dir_iter(p):
extract(f)