Download zip file via FTP and extract files in mem

2019-06-05 15:26发布

I'm trying to download a zip file via ftp, but then extract the files inside without ever actually saving the zip. Any idea how I do this?

标签: python ftp zip
3条回答
在下西门庆
2楼-- · 2019-06-05 15:46

use zipfile.open

it opens a member from the archive into memory. Since ZipFile accepts any file-like object as parameter, you may get it from many sources, like HTTP/FTP servers

import urllib
import io
from zipfile import ZipFile

mysock = urllib.urlopen('ftp://ftp.yourhost.com/spam.zip')  // check urllib for parameters
memfile = io.BytesIO(mysock.read())
with ZipFile(memfile, 'r') as myzip:
    f = myzip.open('eggs.txt')
    content = f.read()  // or other file-like commands

check also Python in-memory zip library

查看更多
放荡不羁爱自由
3楼-- · 2019-06-05 15:49

The zipfile module can be used to extract things from a zip file; the ftplib would be used to access the zipfile. Unfortunately, ftplib doesn't provide a file-like object for zipfile to use to access the contents of the file. I suppose you could read the zip & store it in memory, for example in a string, which could then be wrapped up in a file-like object (StringIO), although you're still getting the whole zip, just not saving it to disk.

If you don't need to save the individual files, but just access (i.e. read) them, zipfile will allow you to do this.

查看更多
Explosion°爆炸
4楼-- · 2019-06-05 15:53

The ftplib module allows downloading files via FTP.

The zipfile module allows extracting files from a zip file.

Here's the key, the io.BytesIO class allows you to pass in-memory bytes to anything that expects a file. (In Python 2.x, the StringIO module provides similar functionality.)

查看更多
登录 后发表回答