Python: Use zipfile to unzip from standard input

If there is standard input, unzip argv [1] from there.

Specify the unzip destination with --dst. The default ./_unzip directory. If you add --setup, it will be put in $ VIRTUAL_ENV / bin / uz.py so that the path will pass, but I'm not sure about setup () so it's appropriate:

    from zipfile import ZipFile
    import os
    import sys
    from StringIO import StringIO
    
    
    def unzip(src,outdir='.'):
        _unzip = lambda z,out : map(lambda n:  z.extract(n, out),
                                    z.namelist())
    
        if not os.path.exists(outdir):
            os.makedirs(outdir)
        
        if not sys.stdin.isatty() :
            _unzip(ZipFile(StringIO(sys.stdin.read())), outdir)
        
        elif src:
            with open(src) as stream:
                _unzip(ZipFile(stream), outdir)
        
    
    def main():
        from argparse import ArgumentParser
        parser = ArgumentParser(description='Unzip stdin or specifile file.')
        parser.add_argument('zipfile', metavar='ZIPFILE', type=str, nargs='?',
                       help='File to be unziped.')
    
        parser.add_argument('--dst', dest='destination',
                            default='_unzip', required=False, )
    
        parser.add_argument('--setup', dest='setup', action='store_true',
                            required=False, )
    
    
        args = parser.parse_args()
    
        if args.setup:
            sys.argv = [__file__,'install']
            setup_me() 
        else:
            unzip(args.zipfile, args.destination)
    
    def setup_me():
        from setuptools import setup
    
        setup(
            name = 'uz.py',
            version = "1",
            license = 'Simplfied BSD License',
            author = 'Hideki Nara of LaFoaglia,Inc.',
            author_email = 'gmail [at] hdknr.com',
            maintainer = 'LaFoglia,Inc.',
            maintainer_email = 'gmail [at] hdknr.com',
            description = "uz : Unzip",
            long_description = "unzip script",
            platforms=['any'],
            classifiers = [
                'Development Status :: 4 - Beta',
                'Environment :: Library',
                'Intended Audience :: Developers',
                'License :: OSI Approved :: Simplifed BSD License',
                'Natural Language :: English',
                'Operating System :: OS Independent',
                'Programming Language :: Python',
            ],
            scripts= [os.path.abspath(__file__)]
        )
    
    if __name__ == '__main__':
        main()

Recommended Posts

Python: Use zipfile to unzip from standard input
[Python] How to use input ()
[Python] Change standard input from keyboard to text file
[Python] Standard input
Python3 standard input I tried to summarize
I want to use jar from python
[Python] Add comments to standard input files
Use thingsspeak from python
Use fluentd from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Study from Python Hour7: How to use classes
Use MySQL from Python
[Bash] Use here-documents to get python power from bash
Use MySQL from Python
I want to use ceres solver from python
[Python] About standard input
Use Tor to connect from urllib2 [Python] [Mac]
Use mecab-ipadic-neologd from python
Have python parse the json entered from the standard input
I wanted to use the Python library from MATLAB
Post from Python to Slack
python3: How to use bottle (2)
[Python3] Standard input [Cheat sheet]
[Python] How to use list 1
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Python3 standard input (competition pro)
Standard input / summary / python, ruby
Receiving standard input tips @ python
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
Connect to sqlite from python
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Use e-Stat API from Python
How to use Python bytes
Allow Python to select strings in input files from folders
Let's use Watson from Python! --How to use Developer Cloud Python SDK
Call Matlab from Python to optimize
Python3 standard input for competitive programming
Python: How to use async with
Use Stanford Core NLP from Python
Matrix representation with Python standard input
How to use SWIG from waf
[Python] How to use Pandas Series
Create folders from '01' to '12' with python
Post from python to facebook timeline
How to use Requests (Python Library)
How to use SQLite in Python
[Lambda] [Python] Post to Twitter from Lambda!
Read and use Python files from Python
Forcibly use Google Translate from python
[Introduction to Python] Let's use pandas
Python (from first time to execution)
Use kabu StationĀ® API from Python
Post images from Python to Tumblr
[Python] How to use list 3 Added
How to use Mysql in python