Package translate :: Package misc :: Module zipfileext

[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.zipfileext

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004, 2005 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      8  # translate is free software; you can redistribute it and/or modify
      9  # it under the terms of the GNU General Public License as published by
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     21
     22  """extensions to zipfile standard module that will hopefully get included in future..."""
     23
     24  from zipfile import ZipFile, struct, structCentralDir, stringCentralDir, structEndArchive, stringEndArchive
     25
     26
     27 -class ZipFileExt(ZipFile, object):
     28      """a ZipFile that can handle replacing objects"""
     29
     30 -    def delete(self, name):
     31          """Delete the file from the archive. If it appears multiple
     32          times only the first instance will be deleted."""
     33          for i in range(0, len(self.filelist)):
     34              if self.filelist[i].filename == name:
     35                  if self.debug:
     36                      print "Removing", name
     37                  deleted_offset = self.filelist[i].header_offset
     38                  # "file_offset" is only available in python up to 2.4
     39                  if hasattr(self.filelist[i], "file_offset"):
     40                      deleted_size = (self.filelist[i].file_offset - self.filelist[i].header_offset) + self.filelist[i].compress_size
     41                  else:
     42                      deleted_size = (len(self.filelist[i].FileHeader()) - self.filelist[i].header_offset) + self.filelist[i].compress_size
     43                  zinfo_size = struct.calcsize(structCentralDir) + len(self.filelist[i].filename) + len(self.filelist[i].extra)
     44                  # Remove the file's data from the archive.
     45                  current_offset = self.fp.tell()
     46                  # go to the end of the archive to calculate the total archive_size
     47                  self.fp.seek(0, 2)
     48                  archive_size = self.fp.tell()
     49                  self.fp.seek(deleted_offset + deleted_size)
     50                  buf = self.fp.read()
     51                  self.fp.seek(deleted_offset)
     52                  self.fp.write(buf)
     53                  self.fp.truncate(archive_size - deleted_size - zinfo_size)
     54                  # go to the end of the archive to calculate the total archive_size
     55                  self.fp.seek(0, 2)
     56                  if self.debug >= 2:
     57                      if self.fp.tell() != archive_size - deleted_size - zinfo_size:
     58                          print "truncation failed: %r != %r" % (self.fp.tell(), archive_size - deleted_size - zinfo_size)
     59                  if current_offset > deleted_offset + deleted_size:
     60                      current_offset -= deleted_size
     61                  elif current_offset > deleted_offset:
     62                      current_offset = deleted_offset
     63                  self.fp.seek(current_offset, 0)
     64                  # Remove file from central directory.
     65                  del self.filelist[i]
     66                  # Adjust the remaining offsets in the central directory.
     67                  for j in range(i, len(self.filelist)):
     68                      if self.filelist[j].header_offset > deleted_offset:
     69                          self.filelist[j].header_offset -= deleted_size
     70                      # "file_offset" is only available in python up to 2.4
     71                      if hasattr(self.filelist[i], "file_offset"):
     72                          if self.filelist[j].file_offset > deleted_offset:
     73                              self.filelist[j].file_offset -= deleted_size
     74                  del self.NameToInfo[name]
     75                  return
     76          if self.debug:
     77              print name, "not in archive"
     78
     79 -    def close(self):
     80          """Close the file, and for mode "w" and "a" write the ending
     81          records."""
     82          if self.fp is None:
     83              return
     84          self.writeendrec()
     85          if not self._filePassed:
     86              self.fp.close()
     87          self.fp = None
     88
     89 -    def writeendrec(self):
     90          """Write the ending records (without neccessarily closing the file)"""
     91          if self.mode in ("w", "a"):             # write ending records
     92              count = 0
     93              pos1 = self.fp.tell()
     94              for zinfo in self.filelist:         # write central directory
     95                  count = count + 1
     96                  dt = zinfo.date_time
     97                  dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
     98                  dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
     99                  centdir = struct.pack(structCentralDir,
    100                    stringCentralDir, zinfo.create_version,
    101                    zinfo.create_system, zinfo.extract_version, zinfo.reserved,
    102                    zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
    103                    zinfo.CRC, zinfo.compress_size, zinfo.file_size,
    104                    len(zinfo.filename), len(zinfo.extra), len(zinfo.comment),
    105                    0, zinfo.internal_attr, zinfo.external_attr,
    106                    zinfo.header_offset)
    107                  self.fp.write(centdir)
    108                  self.fp.write(zinfo.filename)
    109                  self.fp.write(zinfo.extra)
    110                  self.fp.write(zinfo.comment)
    111              pos2 = self.fp.tell()
    112              # Write end-of-zip-archive record
    113              endrec = struct.pack(structEndArchive, stringEndArchive,
    114                       0, 0, count, count, pos2 - pos1, pos1, 0)
    115              self.fp.write(endrec)
    116              self.fp.seek(pos1)
    117