Reputation: 4291
url='http://www.test.com/test.zip'
z = zipfile.ZipFile(BytesIO(urllib.urlopen(url).read()))
z.extractall(path='D:')
I am writing above code to download a zipped file from a url and have downloaded and extracted all files from it to a specified drive and it is working fine. Is there a way I can get meta data of all files extracted from z for example. filenames,file sizes and file extenstions etc?
Upvotes: 0
Views: 698
Reputation: 392
I don't know of a built-in way to do that using the zipfile
module, however it is easily done using os.path
:
import os
EXTRACT_PATH = "D:"
z= zipfile.ZipFile(BytesIO(urllib.urlopen(url).read()))
z.extractall(path=EXTRACT_PATH)
extracted_files = [os.path.join(EXTRACT_PATH, filename) for filename in z.namelist()]
for extracted_file in extracted_files:
# All metadata operations here, such as:
print os.path.getsize(extracted_file)
Upvotes: 1
Reputation: 9969
Zipfile objects actually have built in tools for this that you can use without even extracting anything. infolist
returns a list of ZipInfo objects that you can read certain information out of, including full file name and uncompressed size.
import os
url='http://www.test.com/test.zip'
z = zipfile.ZipFile(BytesIO(urllib.urlopen(url).read()))
info = z.infolist()
data = []
for obj in info:
name = os.path.splitext(obj.filename)
data.append(name[0], name[1], obj.file_size)
I also used os.path.splitext
just to separate out the file's name from its extension as you did ask for file type separately from the name.
Upvotes: 1