1

I'm trying to unzip the file from an URL, and stuck with the below Error. I would get the URL's dynamic so the path may keep vary.

[Errno 22] invalid mode ('rb') or filename: 'http://example.com/media/example.zip'

    url = r"http://" + request.get_host() + uploaded_file_url
    with zipfile.ZipFile(url, "r") as zip_ref:
        zip_ref.extractall("c:/tmp")
        zip_ref.namelist()

The above is the piece of code that i use for unzip. As per my understanding expected is to pass the escape character so that it can read url "IOError: [Errno 22] invalid mode ('r') or filename: 'c:\\Python27\test.txt'"

How we can do this for URL.

Community
  • 1
  • 1
arasub
  • 447
  • 5
  • 10
  • 1
    `ZipFile` expects file on local disk or data in memory , not url. First you have to download this file from internet. – furas Jan 07 '17 at 09:38
  • I think i might need to download the file locally and then pass the same, – arasub Jan 07 '17 at 09:39
  • you have to download file and (a) save in local file and then use `ZipFile` (b) or create file-like object in memory (using `io.StringIO` or `io.BytesIO`), fill with data from internet and then use this file-like object with `ZipFile` – furas Jan 07 '17 at 09:42

1 Answers1

0

Insteated of reading from the URL, i invoked the os.getcwd function to get the current working dir and passed the same to the zip function to unzip, this worked. However if you have blank space in the filename you need to tweek the code a bit.

    fullpath = os.getcwd()+ uploaded_file_url
    fullpath = url.replace('/' , '\\')
    with zipfile.ZipFile(fullpath , "r") as zip_ref:
        zip_ref.extractall("c:/tmp")
        zip_ref.namelist()
arasub
  • 447
  • 5
  • 10