Listing all directories recursively within the zipfile without extracting in python
<p>In Python we can get the list of all files within a zipfile without extracting the zip file using the below code.</p>

<pre><code>import zipfile
zip_ref = zipfile.ZipFile(zipfilepath, 'r')
    for file in zip_ref.namelist():
        print file
</code></pre>

<p>Similarly is there a way to fetch the list of all directories and sub directories within the zipfile without extracting the zipfile?</p>

<p>Thanks everyone for your help.</p>

<pre><code>import zipfile

subdirs_list = []
zip_ref = zipfile.ZipFile('C:/Download/sample.zip', 'r')
for dir in zip_ref.namelist():
    if dir.endswith('/'):
        subdirs_list.append(os.path.basename(os.path.normpath(dir)))

print subdirs_list
</code></pre>

<p>With the above code, I would be able to get a list of all directories and subdirectoies within my zipfile without extracting the sample.zip.</p>

python file directory zipfile filestructure