6

I have a .zip file and would like to know the names of the files within it. Here's the code:

zip_path = glob.glob(path + '/*.zip')[0] 
file = open(zip_path, 'r') # opens without error
if zipfile.is_zipfile(file):
    print str(file) # prints to console
my_zipfile = zipfile.ZipFile(zip_path) # throws IOError

Here is the traceback:

<open file u'/Users/me/Documents/project/uploads/assets/peter/offline_message/offline_imgs.zip', mode 'r' at 0x107b2a150>
Traceback (most recent call last):
  File "/Users/me/Documents/project/admin_dev/proj_name/views.py", line 1680, in get_dps_app_builder_assets
    link_to_assets_zip = zip_dps_app_builder_assets(server_url, app_slug, button_slugs)
  File "/Users/me/Documents/project/admin_dev/proj_name/views.py", line 1724, in zip_dps_app_builder_assets
    my_zipfile = zipfile.ZipFile(zip_path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 712, in __init__
    self._GetContents()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 746, in _GetContents
    self._RealGetContents()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 779, in _RealGetContents
    fp.seek(self.start_dir, 0)
IOError: [Errno 22] Invalid argument

I am very confused as to why this is happening since the file is clearly there and is a valid .zip file. The documentation clearly states that you can pass it either the path to the file or a file-like object, neither of which work in my case:

http://docs.python.org/2/library/zipfile#zipfile-objects

Dharman
  • 30,962
  • 25
  • 85
  • 135
pcaisse
  • 754
  • 2
  • 11
  • 19
  • `file` is a built-in. Try not to use as a variable name. – mechanical_meat Jan 17 '13 at 16:40
  • I don't actually use that as the variable name, I just changed it to make things as simple as possible. Thanks, though. – pcaisse Jan 17 '13 at 16:42
  • 1
    The `is_zipfile()` function just checks for a magic number and might be fooled. It looks to me like `zipfile` is attempting to seek to somewhere before the start of the file, which would typically be due to an invalid zip file. Are you sure the same file works with the conventional zip utility? – Cartroo Jan 17 '13 at 16:51
  • I'm able to unzip it in the terminal and examine its contents without a problem, if that's what you mean. – pcaisse Jan 17 '13 at 16:59
  • 1
    can you upload the zipfile somewhere? – mgrandi Jan 17 '13 at 17:10
  • Also, you appear to be using an Apple-supplied OS X system Python which is, at best, Python 2.7.2. There have been a number of changes to Python's zipfile module since 2.7.2. You *could* try downloading the top of trunk version of it from here: http://hg.python.org/cpython/file/2.7/Lib/zipfile.py – Ned Deily Jan 17 '13 at 17:34
  • @mgrandi, I'm pretty sure the zip file is fine. I created a different one on my Windows VM and also created others through the OS X GUI, none of which work. – pcaisse Jan 17 '13 at 18:13
  • @stank345 well if its not working in python but working in other programs then it could be a bug in python or something else, i would just try uploading it here! – mgrandi Jan 18 '13 at 19:00

1 Answers1

-2

I was not able to figure this issue out and ended up doing it a different way entirely.

EDIT: In the Django app I work with, users needed to be able to upload assets in the form of .zip files and later download everything they had uploaded (plus other content we generate dynamically) in another zip with a different structure. So, I wanted to unzip a previously uploaded file and zip up the contents of that file up in another zip, which I couldn't do because of the error. Instead of reading the zip file when the user requested the download, I ended up unzipping it from a Django InMemoryUploadedFile (whose contents I was able to successfully read) and just leaving the unzipped files on the file system to work with later. The contents of the zip are only two smallish image files, so this workaround of unzipping the zip ahead of time to be used later worked OK for my purposes.

pcaisse
  • 754
  • 2
  • 11
  • 19
  • For the benefit of future visitors, could you elaborate on the different approach you took? As it stands, this isn't a particularly helpful answer. – Brad Larson Jan 20 '13 at 21:56