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
43 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')
59 if mode
== 'a' and lock
:
60 # appending to a file which doesn't exist fails, but we can't check
61 # existence util we hold the lock
62 if (not os
.path
.isfile(file)) or os
.path
.getsize(file) == 0:
65 zipfile
.ZipFile
.__init
__(self
, file, mode
, compression
)
67 self
.end
= self
.fp
.tell()
70 def writestr(self
, zinfo_or_arcname
, bytes
):
71 """Write contents into the archive.
73 The contents is the argument 'bytes', 'zinfo_or_arcname' is either
74 a ZipInfo instance or the name of the file in the archive.
75 This method is overloaded to allow overwriting existing entries.
77 if not isinstance(zinfo_or_arcname
, zipfile
.ZipInfo
):
78 zinfo
= zipfile
.ZipInfo(filename
=zinfo_or_arcname
,
79 date_time
=time
.localtime(time
.time()))
80 zinfo
.compress_type
= self
.compression
81 # Add some standard UNIX file access permissions (-rw-r--r--).
82 zinfo
.external_attr
= (0x81a4 & 0xFFFF) << 16L
84 zinfo
= zinfo_or_arcname
86 # Now to the point why we overwrote this in the first place,
87 # remember the entry numbers if we already had this entry.
89 # If the entry to overwrite is the last one, just reuse that.
90 # If we store uncompressed and the new content has the same size
91 # as the old, reuse the existing entry.
93 doSeek
= False # store if we need to seek to the eof after overwriting
94 if self
.NameToInfo
.has_key(zinfo
.filename
):
95 # Find the last ZipInfo with our name.
96 # Last, because that's catching multiple overwrites
97 i
= len(self
.filelist
)
100 if self
.filelist
[i
].filename
== zinfo
.filename
:
102 zi
= self
.filelist
[i
]
103 if ((zinfo
.compress_type
== zipfile
.ZIP_STORED
104 and zi
.compress_size
== len(bytes
))
105 or (i
+ 1) == len(self
.filelist
)):
106 # make sure we're allowed to write, otherwise done by writestr below
108 # overwrite existing entry
109 self
.fp
.seek(zi
.header_offset
)
110 if (i
+ 1) == len(self
.filelist
):
111 # this is the last item in the file, just truncate
114 # we need to move to the end of the file afterwards again
116 # unhook the current zipinfo, the writestr of our superclass
119 self
.NameToInfo
.pop(zinfo
.filename
)
121 # Couldn't optimize, sadly, just remember the old entry for removal
122 self
._remove
.append(self
.filelist
.pop(i
))
123 zipfile
.ZipFile
.writestr(self
, zinfo
, bytes
)
124 self
.filelist
.sort(lambda l
, r
: cmp(l
.header_offset
, r
.header_offset
))
126 self
.fp
.seek(self
.end
)
127 self
.end
= self
.fp
.tell()
130 """Close the file, and for mode "w" and "a" write the ending
133 Overwritten to compact overwritten entries.
136 # we don't have anything special to do, let's just call base
137 r
= zipfile
.ZipFile
.close(self
)
141 if self
.fp
.mode
!= 'r+b':
142 # adjust file mode if we originally just wrote, now we rewrite
144 self
.fp
= open(self
.filename
, 'r+b')
145 all
= map(lambda zi
: (zi
, True), self
.filelist
) + \
146 map(lambda zi
: (zi
, False), self
._remove
)
147 all
.sort(lambda l
, r
: cmp(l
[0].header_offset
, r
[0].header_offset
))
148 # empty _remove for multiple closes
151 lengths
= [all
[i
+1][0].header_offset
- all
[i
][0].header_offset
152 for i
in xrange(len(all
)-1)]
153 lengths
.append(self
.end
- all
[-1][0].header_offset
)
155 for (zi
, keep
), length
in zip(all
, lengths
):
158 oldoff
= zi
.header_offset
159 # python <= 2.4 has file_offset
160 if hasattr(zi
, 'file_offset'):
161 zi
.file_offset
= zi
.file_offset
+ to_pos
- oldoff
162 zi
.header_offset
= to_pos
164 content
= self
.fp
.read(length
)
166 self
.fp
.write(content
)
169 zipfile
.ZipFile
.close(self
)