简体   繁体   中英

Is it possible to download just part of a ZIP file using python zipfile library

I was wondering is there any way by which I can download only a part of a.rar or.zip file without downloading the whole file? There is a zip file containing files A,B,C and D. I only need A. Can I somehow, use zipfile module so that i can only download 1 file?

i am trying below code:

 r = c.get(file)
    z = ZipFile.ZipFile(BytesIO(r.content)) 
    for file1 in z.namelist():
        if 'time' not in file1:
            print("hi")
            z.extractall(file1,download_path + filename)

This code is downloading whole zip file and only extracting specific one. Can i somehow download only the file i Need. There is similar question here but it shows only approch by command line in linux. That question dosent address how it can be done using python liabraries.

The question @Juggernaut mentioned in a comment is actually very helpful, as it points you in the direction of the solution.

You need to create a replacement for Bytes.IO that returns the necessary information to ZipFile . You will need to get the length of the file, and then get whatever sections ZipFile asks for.

How large are those file? Is it really worth the trouble?

Use remotezip: https://github.com/gtsystem/python-remotezip . You can install it using pip:

pip install remotezip

Usage example:

from remotezip import RemoteZip


with RemoteZip("https://path/to/zip/file.zip") as zip_file:
    for file in zip_file.namelist():
        if 'time' not in file:
            print("hi")
            zip_file.extract(file, path="/path/to/extract")

Note that to use this approach, the web server from which you receive the file needs to support theRange header .

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