我尝试了这个代码,它从编译的qrc中提取捆绑的zip文件,但它给了我错误:
OSError: [Errno 22] Invalid argument: ':/files/file.zip'
任何帮助/建议吗?
import zipfile
import resources
zf = zipfile.ZipInfo(":/files/file.zip")
for file in zf.infolist():
zf.extract(file)
zipfile是一个不直接与Qt一起使用的模块,相反,您必须提取字节并使用BytesIO作为中介:
import io
import sys
import zipfile
import resource_rc
from PyQt5 import QtCore
file = QtCore.QFile(":/files/file.zip")
if not file.open(QtCore.QFile.ReadOnly):
print(file.errorString())
sys.exit(-1)
ba = file.readAll()
f = io.BytesIO(ba)
with zipfile.ZipFile(f, "r") as zf:
for file in zf.infolist():
data = zf.extract(file)
print(data)