| 1180 | return nbytes |
| 1181 | |
| 1182 | def close(self): |
| 1183 | if self.closed: |
| 1184 | return |
| 1185 | try: |
| 1186 | super().close() |
| 1187 | # Flush any data from the compressor, and update header info |
| 1188 | if self._compressor: |
| 1189 | buf = self._compressor.flush() |
| 1190 | self._compress_size += len(buf) |
| 1191 | self._fileobj.write(buf) |
| 1192 | self._zinfo.compress_size = self._compress_size |
| 1193 | else: |
| 1194 | self._zinfo.compress_size = self._file_size |
| 1195 | self._zinfo.CRC = self._crc |
| 1196 | self._zinfo.file_size = self._file_size |
| 1197 | |
| 1198 | if not self._zip64: |
| 1199 | if self._file_size > ZIP64_LIMIT: |
| 1200 | raise RuntimeError("File size too large, try using force_zip64") |
| 1201 | if self._compress_size > ZIP64_LIMIT: |
| 1202 | raise RuntimeError("Compressed size too large, try using force_zip64") |
| 1203 | |
| 1204 | # Write updated header info |
| 1205 | if self._zinfo.flag_bits & _MASK_USE_DATA_DESCRIPTOR: |
| 1206 | # Write CRC and file sizes after the file data |
| 1207 | fmt = '<LLQQ' if self._zip64 else '<LLLL' |
| 1208 | self._fileobj.write(struct.pack(fmt, _DD_SIGNATURE, self._zinfo.CRC, |
| 1209 | self._zinfo.compress_size, self._zinfo.file_size)) |
| 1210 | self._zipfile.start_dir = self._fileobj.tell() |
| 1211 | else: |
| 1212 | # Seek backwards and write file header (which will now include |
| 1213 | # correct CRC and file sizes) |
| 1214 | |
| 1215 | # Preserve current position in file |
| 1216 | self._zipfile.start_dir = self._fileobj.tell() |
| 1217 | self._fileobj.seek(self._zinfo.header_offset) |
| 1218 | self._fileobj.write(self._zinfo.FileHeader(self._zip64)) |
| 1219 | self._fileobj.seek(self._zipfile.start_dir) |
| 1220 | |
| 1221 | # Successfully written: Add file to our caches |
| 1222 | self._zipfile.filelist.append(self._zinfo) |
| 1223 | self._zipfile.NameToInfo[self._zinfo.filename] = self._zinfo |
| 1224 | finally: |
| 1225 | self._zipfile._writing = False |
| 1226 | |
| 1227 | |
| 1228 | |