Construct an appropriate ZipInfo for a file on the filesystem. filename should be the path to a file or directory on the filesystem. arcname is the name which it will have within the archive (by default, this will be the same as filename, but without a drive letter and
(cls, filename, arcname=None, *, strict_timestamps=True)
| 521 | |
| 522 | @classmethod |
| 523 | def from_file(cls, filename, arcname=None, *, strict_timestamps=True): |
| 524 | """Construct an appropriate ZipInfo for a file on the filesystem. |
| 525 | |
| 526 | filename should be the path to a file or directory on the filesystem. |
| 527 | |
| 528 | arcname is the name which it will have within the archive (by default, |
| 529 | this will be the same as filename, but without a drive letter and with |
| 530 | leading path separators removed). |
| 531 | """ |
| 532 | if isinstance(filename, os.PathLike): |
| 533 | filename = os.fspath(filename) |
| 534 | st = os.stat(filename) |
| 535 | isdir = stat.S_ISDIR(st.st_mode) |
| 536 | mtime = time.localtime(st.st_mtime) |
| 537 | date_time = mtime[0:6] |
| 538 | if not strict_timestamps and date_time[0] < 1980: |
| 539 | date_time = (1980, 1, 1, 0, 0, 0) |
| 540 | elif not strict_timestamps and date_time[0] > 2107: |
| 541 | date_time = (2107, 12, 31, 23, 59, 59) |
| 542 | # Create ZipInfo instance to store file information |
| 543 | if arcname is None: |
| 544 | arcname = filename |
| 545 | arcname = os.path.normpath(os.path.splitdrive(arcname)[1]) |
| 546 | while arcname[0] in (os.sep, os.altsep): |
| 547 | arcname = arcname[1:] |
| 548 | if isdir: |
| 549 | arcname += '/' |
| 550 | zinfo = cls(arcname, date_time) |
| 551 | zinfo.external_attr = (st.st_mode & 0xFFFF) << 16 # Unix attributes |
| 552 | if isdir: |
| 553 | zinfo.file_size = 0 |
| 554 | zinfo.external_attr |= 0x10 # MS-DOS directory flag |
| 555 | else: |
| 556 | zinfo.file_size = st.st_size |
| 557 | |
| 558 | return zinfo |
| 559 | |
| 560 | def is_dir(self): |
| 561 | """Return True if this archive member is a directory.""" |