1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is Mozilla build system.
16 # The Initial Developer of the Original Code is
18 # Portions created by the Initial Developer are Copyright (C) 2007
19 # the Initial Developer. All Rights Reserved.
22 # Axel Hecht <axel@pike.org>
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
36 # ***** END LICENSE BLOCK *****
40 import binascii
, struct
42 from utils
import lockFile
45 class ZipFile(zipfile
.ZipFile
):
46 """ Class with methods to open, read, write, close, list zip files.
48 Subclassing zipfile.ZipFile to allow for overwriting of existing
49 entries, though only for writestr, not for write.
51 def __init__(self
, file, mode
="r", compression
=zipfile
.ZIP_STORED
,
54 assert isinstance(file, basestring
)
55 self
.lockfile
= lockFile(file + '.lck')
58 zipfile
.ZipFile
.__init
__(self
, file, mode
, compression
)
60 self
.end
= self
.fp
.tell()
63 def writestr(self
, zinfo_or_arcname
, bytes
):
64 """Write contents into the archive.
66 The contents is the argument 'bytes', 'zinfo_or_arcname' is either
67 a ZipInfo instance or the name of the file in the archive.
68 This method is overloaded to allow overwriting existing entries.
70 if not isinstance(zinfo_or_arcname
, zipfile
.ZipInfo
):
71 zinfo
= zipfile
.ZipInfo(filename
=zinfo_or_arcname
,
72 date_time
=time
.localtime(time
.time()))
73 zinfo
.compress_type
= self
.compression
74 # Add some standard UNIX file access permissions (-rw-r--r--).
75 zinfo
.external_attr
= (0x81a4 & 0xFFFF) << 16L
77 zinfo
= zinfo_or_arcname
79 # Now to the point why we overwrote this in the first place,
80 # remember the entry numbers if we already had this entry.
82 # If the entry to overwrite is the last one, just reuse that.
83 # If we store uncompressed and the new content has the same size
84 # as the old, reuse the existing entry.
86 doSeek
= False # store if we need to seek to the eof after overwriting
87 if self
.NameToInfo
.has_key(zinfo
.filename
):
88 # Find the last ZipInfo with our name.
89 # Last, because that's catching multiple overwrites
90 i
= len(self
.filelist
)
93 if self
.filelist
[i
].filename
== zinfo
.filename
:
96 if ((zinfo
.compress_type
== zipfile
.ZIP_STORED
97 and zi
.compress_size
== len(bytes
))
98 or (i
+ 1) == len(self
.filelist
)):
99 # make sure we're allowed to write, otherwise done by writestr below
101 # overwrite existing entry
102 self
.fp
.seek(zi
.header_offset
)
103 if (i
+ 1) == len(self
.filelist
):
104 # this is the last item in the file, just truncate
107 # we need to move to the end of the file afterwards again
109 # unhook the current zipinfo, the writestr of our superclass
112 self
.NameToInfo
.pop(zinfo
.filename
)
114 # Couldn't optimize, sadly, just remember the old entry for removal
115 self
._remove
.append(self
.filelist
.pop(i
))
116 zipfile
.ZipFile
.writestr(self
, zinfo
, bytes
)
117 self
.filelist
.sort(lambda l
, r
: cmp(l
.header_offset
, r
.header_offset
))
119 self
.fp
.seek(self
.end
)
120 self
.end
= self
.fp
.tell()
123 """Close the file, and for mode "w" and "a" write the ending
126 Overwritten to compact overwritten entries.
129 # we don't have anything special to do, let's just call base
130 r
= zipfile
.ZipFile
.close(self
)
134 if self
.fp
.mode
!= 'r+b':
135 # adjust file mode if we originally just wrote, now we rewrite
137 self
.fp
= open(self
.filename
, 'r+b')
138 all
= map(lambda zi
: (zi
, True), self
.filelist
) + \
139 map(lambda zi
: (zi
, False), self
._remove
)
140 all
.sort(lambda l
, r
: cmp(l
[0].header_offset
, r
[0].header_offset
))
141 # empty _remove for multiple closes
144 lengths
= [all
[i
+1][0].header_offset
- all
[i
][0].header_offset
145 for i
in xrange(len(all
)-1)]
146 lengths
.append(self
.end
- all
[-1][0].header_offset
)
148 for (zi
, keep
), length
in zip(all
, lengths
):
151 oldoff
= zi
.header_offset
152 # python <= 2.4 has file_offset
153 if hasattr(zi
, 'file_offset'):
154 zi
.file_offset
= zi
.file_offset
+ to_pos
- oldoff
155 zi
.header_offset
= to_pos
157 content
= self
.fp
.read(length
)
159 self
.fp
.write(content
)
162 zipfile
.ZipFile
.close(self
)