Created
April 22, 2015 18:54
-
-
Save tilusnet/b0509afa624d11995875 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import zlib, os, sys | |
import argparse | |
def get_parsed_args(): | |
parser = argparse.ArgumentParser(description='Deflate a file from stdin. \n \ | |
Alternatively processes all files in a directory. \n \ | |
Decompressed output is directed to stdout.' | |
) | |
parser.add_argument('-d', '--input-dir', dest='input_dir', | |
help='All files in the folder will be processed. This is not recursive.\n \ | |
WARNING: Each individual file will be loaded entirely in memory!' | |
) | |
return parser.parse_args() | |
if __name__ == '__main__': | |
myargs = get_parsed_args() | |
inputdir = myargs.input_dir | |
if inputdir: | |
for root, _, files in os.walk(inputdir): | |
files.sort() | |
for ff in files: | |
fullpath = os.path.join(root, ff) | |
with open(fullpath, mode='rb') as f: | |
sys.stderr.write('\nDeflating "{}"...'.format(fullpath)) | |
try: | |
print(zlib.decompress(f.read()).decode()) | |
except: | |
sys.stderr.write(' FAILED; skipped.') | |
continue | |
sys.stderr.write(' DONE.') | |
else: | |
try: | |
print(zlib.decompress(sys.stdin.buffer.read()).decode()) | |
except: | |
sys.stderr.write('\n [!] Failed to decompress; skipped.\n') | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment