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?
问题:
回答1:
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.)
回答2:
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:
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.