Reading multiple zip archive comments with python

Question:

My zip file contains a lot of smaller zip files.

I want to iterate through all those files,
reading and printing each of their comments.

I’ve found out that zipfile file.zip or unzip -z file.zipcan do this to a file in separate, but I’m looking for a way to go through all of them.

Couldn’t find anything perfect yet, but this post. However, the code is too advanced for me, and I need something very basic, to begin with 🙂

Any ideas or information would be great, thanks!

Asked By: MatanyaP

||

Answers:

Not sure exactly what your looking for but here are a few ways I did it on an Ubuntu Linux machine.

for i in `ls *.zip`; do unzip -l $i; done

or

unzip -l myzip.zip

or

unzip -p myzip.zip | python -c 'import zipfile,sys,StringIO;print     "n".join(zipfile.ZipFile(StringIO.StringIO(sys.stdin.read())).namelist())'
Answered By: James Knott

You can use the zipfile library to iterate through your files and
get their comments using zipinfo.comment

import zipfile

file = zipfile.ZipFile('filepath.zip')

infolist = file.infolist()
    for info in infolist:
        print(info.comment)

The example above prints the comment of each file in your zip file.

You could loop through your zip files and print their contents comments similiarly.

Check out the official zipfile documentation, its super clear.

Answered By: Ariel Gorlik

A short and easy way to achieve this:

from zipfile import ZipFile

ziplist = ZipFile('parentzip.zip').namelist()

for childzip in ziplist:
    zip_comment = ZipFile(childzip).comment

Reminder that if you want to do string based comparisons you should either encode your reference string as bytes, or convert the comment into a string. Ex:

from zipfile import ZipFile

paths = ['file1.zip', 'file2.zip', 'file3.zip']

bad_str = 'please ignore me'
new = []
for filename in paths:
    zip_comment = zipfile.ZipFile(filename).comment
    if not zip_comment == str.encode(bad_str):
        new.append(filename)

paths = new
Answered By: Clara
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.