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.zip
can 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!
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())'
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.
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
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.zip
can 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!
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())'
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.
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