Downloading and unzipping a .zip file without writing to disk

前端 未结 9 1588
囚心锁ツ
囚心锁ツ 2020-12-02 04:42

I have managed to get my first python script to work which downloads a list of .ZIP files from a URL and then proceeds to extract the ZIP files and writes them to disk.

9条回答
  •  不知归路
    2020-12-02 05:34

    My suggestion would be to use a StringIO object. They emulate files, but reside in memory. So you could do something like this:

    # get_zip_data() gets a zip archive containing 'foo.txt', reading 'hey, foo'
    
    import zipfile
    from StringIO import StringIO
    
    zipdata = StringIO()
    zipdata.write(get_zip_data())
    myzipfile = zipfile.ZipFile(zipdata)
    foofile = myzipfile.open('foo.txt')
    print foofile.read()
    
    # output: "hey, foo"
    

    Or more simply (apologies to Vishal):

    myzipfile = zipfile.ZipFile(StringIO(get_zip_data()))
    for name in myzipfile.namelist():
        [ ... ]
    

    In Python 3 use BytesIO instead of StringIO:

    import zipfile
    from io import BytesIO
    
    filebytes = BytesIO(get_zip_data())
    myzipfile = zipfile.ZipFile(filebytes)
    for name in myzipfile.namelist():
        [ ... ]
    

提交回复
热议问题