<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Conseguido!!<br>
    <br>
    A partir de esta información:
<a class="moz-txt-link-freetext" href="http://stackoverflow.com/questions/297345/create-a-zip-file-from-a-generator-in-python/2734156#2734156">http://stackoverflow.com/questions/297345/create-a-zip-file-from-a-generator-in-python/2734156#2734156</a><br>
    <br>
    He creado esta implementación, os la dejo por si alguno le
    interesara:<br>
    <br>
    <tt><br>
      import time, shutil<br>
      from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED<br>
      import zlib, binascii, struct<br>
      <br>
      class ZipWriter(object):<br>
          def __init__(self, zf, filename):<br>
              self.zf = zf<br>
              self.filename = filename<br>
              self.zinfo = self._ZipInfo(filename)<br>
              self.file_size = 0<br>
              self.compress_size = 0<br>
              self.CRC = 0        <br>
              <br>
              if self.zinfo.compress_type == ZIP_DEFLATED:<br>
                  self.cmpr =
      zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15)<br>
              else:<br>
                  self.cmpr = None        <br>
              <br>
          def _ZipInfo(self, filename):<br>
              zinfo = ZipInfo(filename=filename,<br>
                                 
      date_time=time.localtime(time.time())[:6])<br>
              zinfo.compress_type = self.zf.compression<br>
              zinfo.external_attr = 0600 &lt;&lt; 16   <br>
              zinfo.file_size = 0<br>
              zinfo.flag_bits = 0x00<br>
              zinfo.CRC = 0<br>
              zinfo.compress_size = 0<br>
              <br>
              zinfo.header_offset = self.zf.fp.tell()    # Start of
      header bytes<br>
      <br>
              self.zf._writecheck(zinfo)<br>
              self.zf._didModify = True<br>
      <br>
              self.zf.fp.write(zinfo.FileHeader())                <br>
              <br>
              return zinfo       <br>
              <br>
          def write(self, bytes):<br>
              self.file_size = self.file_size + len(bytes)<br>
              self.CRC = binascii.crc32(bytes, self.CRC)<br>
              if self.cmpr:<br>
                  bytes = self.cmpr.compress(bytes)<br>
                  self.compress_size = self.compress_size + len(bytes)<br>
          <br>
              self.zf.fp.write(bytes)<br>
          <br>
          def close(self):<br>
              if self.cmpr:<br>
                  buf = cmpr.flush()<br>
                  self.compress_size = self.compress_size + len(buf)<br>
                  self.zf.fp.write(buf)<br>
                  self.zinfo.compress_size = self.compress_size<br>
              else:<br>
                  self.zinfo.compress_size = self.file_size<br>
      <br>
              self.zinfo.CRC = self.CRC<br>
              self.zinfo.file_size = self.file_size<br>
      <br>
              position = self.zf.fp.tell()<br>
              self.zf.fp.seek(self.zinfo.header_offset + 14, 0)<br>
              self.zf.fp.write(struct.pack("&lt;lLL", self.zinfo.CRC,
      self.zinfo.compress_size, self.zinfo.file_size))<br>
              self.zf.fp.seek(position, 0)<br>
              self.zf.filelist.append(self.zinfo)<br>
              self.zf.NameToInfo[self.zinfo.filename] = self.zinfo<br>
              <br>
      def copyZipFile(source, target):<br>
          zsource = ZipFile(source, "r")<br>
          ztarget = ZipFile(target, "w")<br>
          <br>
          for item in zsource.infolist():<br>
              fsrc = zsource.open(item.filename, "r")<br>
              fdst = ZipWriter(ztarget, item.filename)     <br>
              shutil.copyfileobj(fsrc, fdst)            <br>
              fdst.close()<br>
              fsrc.close()<br>
              <br>
          zsource.close()<br>
          ztarget.close()   <br>
    </tt><br>
    <br>
    <br>
    Saludos<br>
    <br>
    Juande<br>
    <br>
    <br>
    <br>
    <br>
    <br>
     <br>
    <br>
    El 24/01/2011 23:29, Juan de Dios Manjón Pérez escribió:
    <blockquote cite="mid:4D3DFD4D.6040404@jdmanjon.net" type="cite">
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
      Andrey,<br>
      <br>
      también he llegado al punto que comentas, <br>
      el problema que tengo ahora es para escribir el chuck, ya que <tt>ZipFile.open(name,

        mode='r', pwd=None)</tt> no admite <tt>mode='w'</tt>.<br>
      <br>
      El unico metodo <tt>ZipFile.writestr(zinfo_or_arcname, bytes)</tt>
      que he encontrado para escribir requiere que se escriba TODO de
      golpe.<br>
      <br>
      La implementación que busco es parecida a:<br>
      <br>
      <tt>from zipfile import ZipFile<br>
        from shutil import copyfileobj<br>
        <br>
        def copyZipFile(source, target):<br>
            zsource = ZipFile(source,"r")<br>
            ztarget = ZipFile(target,"w")<br>
            <br>
            for item in zsource.infolist():<br>
                fsrc = zsource.open(item.filename,"r")<br>
                fdst = ztarget.open(item.filename,"w") #mode no acepta
        "w". exception     <br>
                copyfileobj(fsrc,fdst)            <br>
                fdst.close()<br>
                fsrc.close()<br>
                <br>
            zsource.close()<br>
            ztarget.close()</tt>   <br>
      <br>
      <br>
      Saludos<br>
      <br>
      Juande<br>
      <br>
      <br>
      <br>
      El 24/01/2011 20:14, Andrey Antoukh escribió:
      <blockquote
        cite="mid:AANLkTinQ8mosCY18D=16rpDwpFqNABDsqShUwwke6EbK@mail.gmail.com"
        type="cite"><a moz-do-not-send="true"
          href="http://docs.python.org/py3k/library/zipfile.html">http://docs.python.org/py3k/library/zipfile.html</a><br>
        <br>
        ZipFile.open(name, mode='r', pwd=None)<br>
        Extract a member from the archive as a file-like object
        (ZipExtFile). name is the name of the file in the archive, or a
        ZipInfo object. The mode parameter, if included, must be one of
        the following: 'r' (the default), 'U', or 'rU'. Choosing 'U' or
        'rU' will enable universal newline support in the read-only
        object. pwd is the password used for encrypted files. Calling
        open() on a closed ZipFile will raise a RuntimeError.<br>
        <br>
        Note The file-like object is read-only and provides the
        following methods: read(), readline(), readlines(), __iter__(),
        __next__().<br>
        <br>
        Lo que a groso modo quiere decir que devuelve un objeto fichero
        que tiene todos los métodos estándares para leer. De lo que se
        deduce que podemos leerlos a cachitos para que no ocupe memoria.<br>
        <br>
        La implementación seria parecida a la que ya se ha propuesto.  O
        como el simple ejemplo...<br>
        <br>
        <font class="Apple-style-span" face="'courier new', monospace">&gt;&gt;&gt;

          zipobj = zipfile.ZipFile("prueba.zip", "r")<br>
          &gt;&gt;&gt; for zip in zipobj.namelist():<br>
          ...     zipfileobj = zipobj.open(zip, "r")<br>
          ...     for chuck in zipfileobj:<br>
          ...         # lo que sea que tengas que hacer con estos datos.</font><br>
        <br>
        Un saludo.<br>
      </blockquote>
      <br>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
Python-es mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python-es@python.org">Python-es@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/python-es">http://mail.python.org/mailman/listinfo/python-es</a>
FAQ: <a class="moz-txt-link-freetext" href="http://python-es-faq.wikidot.com/">http://python-es-faq.wikidot.com/</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>