简体   繁体   中英

Download zip file via FTP and extract files in memory in Python

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?

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

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.)

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 (ie read) them, zipfile will allow you to do this.

I couldn't find a possible way to extract the files directly, but here is an alternative:

First, download the file from the FTP

ftp =ftplib.FTP(FtpServer)          #connect to the ftp server
ftp.login(ServerUser,ServerPwd)     #using your credentials here

filedata = open(os.path.join(destination,DowloadedFileName),'wb')
ftp.retrbinary('RETR '+SourceFilename,filedata.write)      
filedata.close()
ftp.quit()

Second, extract the files from the zip:

path_to_zip_file = os.path.join(destination,DowloadedFileName)
directory_to_extract_to = destination
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
    zip_ref.extractall(directory_to_extract_to)
           

Lastly, remove the downloaded zip file

os.remove(path_to_zip_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM