1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
PriyalChaudhari
  • 363
  • 1
  • 7
  • 23
  • Possible duplicate of [Is it possible to download just part of a ZIP archive (e.g. one file)?](https://stackoverflow.com/questions/8543214/is-it-possible-to-download-just-part-of-a-zip-archive-e-g-one-file) – Amin Etesamian Jul 16 '17 at 07:23
  • @Juggernaut nope. that is in unix context. I am asking if its possible using the python code. Nothing of sort is mentioned in that question. I already checked that question. And asked mine – PriyalChaudhari Jul 16 '17 at 07:34

2 Answers2

1

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?

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

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 the Range header.