Created
November 28, 2013 05:54
-
-
Save dualbus/7687811 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 python | |
| from zipfile import ZipFile | |
| from os import walk | |
| from os.path import isfile, isdir, join | |
| class Zipper(object): | |
| _class = ZipFile | |
| def __init__(self, zip_file, recurse=False): | |
| self._zip = self._class(zip_file, 'w') | |
| self._recurse = recurse | |
| def add_file(self, file_): | |
| self._zip.write(file_) | |
| def add_dir(self, dir_): | |
| if not self._recurse: | |
| return | |
| for dirpath, dirnames, filenames in walk(dir_): | |
| for file_ in filenames: | |
| self.add_file(join(dirpath, file_)) | |
| if __name__ == '__main__': | |
| import sys | |
| from argparse import ArgumentParser | |
| p = ArgumentParser(description='unzip this') | |
| p.add_argument('-r', dest='recurse', action='store_true', default=False) | |
| p.add_argument('archive', metavar='ARCHIVE') | |
| p.add_argument('sources', metavar='SOURCES', nargs='+') | |
| args = p.parse_args() | |
| z = Zipper(args.archive, args.recurse) | |
| for source in args.sources: | |
| if isfile(source): | |
| z.add_file(source) | |
| elif isdir(source): | |
| z.add_dir(source) | |
| else: | |
| raise Exception() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment