如何知道压缩文件中的文件夹大小

How to know the folder size in a zipfile

我知道您可以使用 .file_size 方法获取 ZIP 文件中文件的大小(以字节为单位) 但是有没有什么可以代替文件夹的大小?

例如:

import zipfile, os

os.chdir('C:\')    
zp= zipfile.ZipFile('example.zip')

spamInfo = zp.getinfo('spam.txt')    #Here, Instead of a file I'd like to put a folder
spamInfo.file_size

zp.close()
import zipfile

zp = zipfile.ZipFile("example.zip")

size = sum([zinfo.file_size for zinfo in zp.filelist])
zip_kb = float(size) / 1000  # kB