'How to get meta-data of files inside compressed folder
I am trying to build a script written in Python which explore an archive (in this case a zip), and recursively gets all meta-data of files.
I usually use the following command to get meta-data:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(fname)
The problem is that I don't want to extract the files from the zip, so I don't have a path to provide to os.stat(). The only thing I am able to do is:
z=zipfile.ZipFile(zfilename,'r')
for info in z.infolist():
fname = info.filename
data = z.read(fname)
Can I use the 'data' to get informations I need? or should I use another approach?
Solution 1:[1]
The ZIP format doesn't contain nearly as much metadata as a file on the filesystem (nor does it need to). You can extract all metadata from a zipfile without decompressing the file contents.
The ZipFile.infolist()
method gives you a list of ZipInfo
instances, giving you access to that metadata.
Solution 2:[2]
with zipfile.ZipFile(path_zip_file, 'r') as zip_file:
for elem in zip_file.infolist():
if elem.filename.endswith('/'):
continue
print('year_last_modified', elem.date_time[0])
print('month_last_modified', elem.date_time[1])
You can get file lists * .zip files using the method infolist()
To work only with files, check is used if elem.filename.endswith('/')
In order to get the year and month of creation / modification of a file, you can use elem.date_time[0]
and elem.date_time[1]
Solution 3:[3]
import os
import zipfile
z=zipfile.ZipFile(zfilename,'r')
for info in z.infolist():
fname = info.filename
data = z.read(fname)
print(fname)
print(data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martijn Pieters |
Solution 2 | |
Solution 3 | slfan |