1

How do I traverse the contents of a zip file in python without having to extract them?

If extracting is the only way, is it efficient if I create a unique temp folder and extract them there then traverse and remove the temp folder afterwards?

2
  • Are you using the zipfile module? Commented Sep 29, 2011 at 21:42
  • @GregHewgill: Yes, I am. Commented Sep 30, 2011 at 0:33

1 Answer 1

3

Use the namelist() method of the ZipFile class to get a list of all of the filenames in the archive without having to extract them. For example:

import zipfile myzip = zipfile.ZipFile('myzip.zip', 'r') filenames = myzip.namelist() # Do something with filenames 
Sign up to request clarification or add additional context in comments.

7 Comments

If the OP is trying to retrieve directories, he'll have to use infolist instead of namelist.
@Gabe: In an ZipInfo object, I can't seem to find a suitable property or method which would tell me if the item was a dir or not.
@Adam: Lets say I have the filenames, those are just strings. How can I tell which are directories and files?
@Marconi: Often zip file won't have entries for individual directories at all. But if they do, check for a trailing /.
@GregHewgill: Yeah, it looks like its impossible to fully traverse the archive without extracting it first.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.