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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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
check also Python in-memory zip library
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.
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, theStringIO
module provides similar functionality.)