Labo29
Labo29

Reputation: 117

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?

Upvotes: 2

Views: 10353

Answers (3)

Eugene Sweets
Eugene Sweets

Reputation: 31

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]

Upvotes: 3

Suraj shah
Suraj shah

Reputation: 31

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)

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123400

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.

Upvotes: 3

Related Questions