| 1335 | return nbytes |
| 1336 | |
| 1337 | def close(self): |
| 1338 | if self.closed: |
| 1339 | return |
| 1340 | try: |
| 1341 | super().close() |
| 1342 | # Flush any data from the compressor, and update header info |
| 1343 | if self._compressor: |
| 1344 | buf = self._compressor.flush() |
| 1345 | self._compress_size += len(buf) |
| 1346 | self._fileobj.write(buf) |
| 1347 | self._zinfo.compress_size = self._compress_size |
| 1348 | else: |
| 1349 | self._zinfo.compress_size = self._file_size |
| 1350 | self._zinfo.CRC = self._crc |
| 1351 | self._zinfo.file_size = self._file_size |
| 1352 | |
| 1353 | if not self._zip64: |
| 1354 | if self._file_size > ZIP64_LIMIT: |
| 1355 | raise RuntimeError("File size too large, try using force_zip64") |
| 1356 | if self._compress_size > ZIP64_LIMIT: |
| 1357 | raise RuntimeError("Compressed size too large, try using force_zip64") |
| 1358 | |
| 1359 | # Write updated header info |
| 1360 | if self._zinfo.flag_bits & _MASK_USE_DATA_DESCRIPTOR: |
| 1361 | # Write CRC and file sizes after the file data |
| 1362 | fmt = '<LLQQ' if self._zip64 else '<LLLL' |
| 1363 | self._fileobj.write(struct.pack(fmt, _DD_SIGNATURE, self._zinfo.CRC, |
| 1364 | self._zinfo.compress_size, self._zinfo.file_size)) |
| 1365 | self._zipfile.start_dir = self._fileobj.tell() |
| 1366 | else: |
| 1367 | # Seek backwards and write file header (which will now include |
| 1368 | # correct CRC and file sizes) |
| 1369 | |
| 1370 | # Preserve current position in file |
| 1371 | self._zipfile.start_dir = self._fileobj.tell() |
| 1372 | self._fileobj.seek(self._zinfo.header_offset) |
| 1373 | self._fileobj.write(self._zinfo.FileHeader(self._zip64)) |
| 1374 | self._fileobj.seek(self._zipfile.start_dir) |
| 1375 | |
| 1376 | # Successfully written: Add file to our caches |
| 1377 | self._zipfile.filelist.append(self._zinfo) |
| 1378 | self._zipfile.NameToInfo[self._zinfo.filename] = self._zinfo |
| 1379 | finally: |
| 1380 | self._zipfile._writing = False |
| 1381 | |
| 1382 | |
| 1383 | |