Cómo leer desde un archivo zip dentro de un archivo zip en Python?
Tengo un archivo que quiero leer que está comprimido dentro de un archivo zip. Por ejemplo, padre.zip contiene niño.zip, que contiene niño.txt. Estoy teniendo problemas para leer niño.Postal. ¿Alguien puede corregir mi código?
Asumo que necesito crear hijo.comprime como un objeto similar a un archivo y luego ábrelo con una segunda instancia de zipfile, pero siendo nuevo en python mi zipfile.ZipFile (zfile.open(nombre)) es tonto. Genera un archivo zip.BadZipfile: "El archivo no es un archivo zip" en (independientemente validado) niño.zip
import zipfile
with zipfile.ZipFile("parent.zip", "r") as zfile:
for name in zfile.namelist():
if re.search(r'\.zip$', name) != None:
# We have a zip within a zip
with **zipfile.ZipFile(zfile.open(name))** as zfile2:
for name2 in zfile2.namelist():
# Now we can extract
logging.info( "Found internal internal file: " + name2)
print "Processing code goes here"
3 answers
Cuando se utiliza la llamada .open()
en una instancia ZipFile
se obtiene un identificador de archivo abierto. Sin embargo, para leer un archivo zip, la clase ZipFile
necesita un poco más. Necesita ser capaz de buscar en ese archivo, y el objeto devuelto por .open()
no es buscable.
La solución es leer toda la entrada zip en memoria usando .read()
, almacenarla en un objeto BytesIO
(un archivo en memoria que es buscable) y alimentarlo a ZipFile
:
from io import BytesIO
# ...
zfiledata = BytesIO(zfile.read(name))
with zipfile.ZipFile(zfiledata) as zfile2:
O, en el contexto de su ejemplo:
import zipfile
from io import BytesIO
with zipfile.ZipFile("parent.zip", "r") as zfile:
for name in zfile.namelist():
if re.search(r'\.zip$', name) != None:
# We have a zip within a zip
zfiledata = BytesIO(zfile.read(name))
with zipfile.ZipFile(zfiledata) as zf
for name2 in zfile2.namelist():
# Now we can extract
logging.info( "Found internal internal file: " + name2)
print "Processing code goes here"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-10 16:11:54
Para conseguir que esto funcione con python33 (bajo windows pero que podría ser irrelevante) tuve que hacer:
import zipfile, re, io
with zipfile.ZipFile(file, 'r') as zfile:
for name in zfile.namelist():
if re.search(r'\.zip$', name) != None:
zfiledata = io.BytesIO(zfile.read(name))
with zipfile.ZipFile(zfiledata) as zfile2:
for name2 in zfile2.namelist():
print(name2)
CStringIO no existe, así que usé io.BytesIO
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-20 14:57:29
Aquí hay una función que se me ocurrió. (Copiado de aquí.)
def extract_nested_zipfile(path, parent_zip=None):
"""Returns a ZipFile specified by path, even if the path contains
intermediary ZipFiles. For example, /root/gparent.zip/parent.zip/child.zip
will return a ZipFile that represents child.zip
"""
def extract_inner_zipfile(parent_zip, child_zip_path):
"""Returns a ZipFile specified by child_zip_path that exists inside
parent_zip.
"""
memory_zip = StringIO()
memory_zip.write(parent_zip.open(child_zip_path).read())
return zipfile.ZipFile(memory_zip)
if ('.zip' + os.sep) in path:
(parent_zip_path, child_zip_path) = os.path.relpath(path).split(
'.zip' + os.sep, 1)
parent_zip_path += '.zip'
if not parent_zip:
# This is the top-level, so read from disk
parent_zip = zipfile.ZipFile(parent_zip_path)
else:
# We're already in a zip, so pull it out and recurse
parent_zip = extract_inner_zipfile(parent_zip, parent_zip_path)
return extract_nested_zipfile(child_zip_path, parent_zip)
else:
if parent_zip:
return extract_inner_zipfile(parent_zip, path)
else:
# If there is no nesting, it's easy!
return zipfile.ZipFile(path)
Así es como lo probé:
echo hello world > hi.txt
zip wrap1.zip hi.txt
zip wrap2.zip wrap1.zip
zip wrap3.zip wrap2.zip
print extract_nested_zipfile('/Users/mattfaus/dev/dev-git/wrap1.zip').open('hi.txt').read()
print extract_nested_zipfile('/Users/mattfaus/dev/dev-git/wrap2.zip/wrap1.zip').open('hi.txt').read()
print extract_nested_zipfile('/Users/mattfaus/dev/dev-git/wrap3.zip/wrap2.zip/wrap1.zip').open('hi.txt').read()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:02:38