miurahr / py7zr

7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.

Home Page:https://pypi.org/project/py7zr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error appending file: `KeyError: 'lastwritetime'`

pmolodo opened this issue · comments

Describe the bug

If I try to append a file to a 7z archive created with certain settings from 7za, I get an error:

Traceback (most recent call last):
  File "./test_append.py", line 16, in <module>
    archive.write(new_path)
  File "/src/_external/py7zr/py7zr/py7zr.py", line 409, in __exit__
    self.close()
  File "/src/_external/py7zr/py7zr/py7zr.py", line 1081, in close
    self._write_flush()
  File "/src/_external/py7zr/py7zr/py7zr.py", line 690, in _write_flush
    self._write_header()
  File "/src/_external/py7zr/py7zr/py7zr.py", line 698, in _write_header
    encrypted=self.header_encryption,
  File "/src/_external/py7zr/py7zr/archiveinfo.py", line 1004, in write
    startpos, headercrc = self._encode_header(file, afterheader, filters)
  File "/src/_external/py7zr/py7zr/archiveinfo.py", line 971, in _encode_header
    _, raw_header_len, raw_crc = self.write(buf, 0, False)
  File "/src/_external/py7zr/py7zr/archiveinfo.py", line 1011, in write
    self.files_info.write(crcfile)
  File "/src/_external/py7zr/py7zr/archiveinfo.py", line 875, in write
    self._write_times(file, PROPERTY.LAST_WRITE_TIME, "lastwritetime")
  File "/src/_external/py7zr/py7zr/archiveinfo.py", line 796, in _write_times
    write_real_uint64(fp, file[name])
KeyError: 'lastwritetime'

To Reproduce

Steps to reproduce the behavior:

  1. Download archive_from_7za.zip to the current directory

  2. Run following code with python3:

import os

import py7zr

# this archive made with:
#   echo "old content encoded by 7za" > old_file.txt
#   rm -f archive_from_7za.*
#   7za a -mx=5 -mtm=off -mmt32 -bsp2 archive_from_7za.7z old_file.txt
#   mv archive_from_7za.7z archive_from_7za.zip
#   rm old_file.txt

# note - archive is a 7za, but renamed .zip because github only allows .zip attachments, not .7z
archive_path = "archive_from_7za.zip"

# make a file we will append to the archive
new_path = os.path.join("new_file.txt")
with open(new_path, "w", encoding="utf8") as f:
    f.write("new content")

# then try to append archive
with py7zr.SevenZipFile(archive_path, "a") as archive:
    archive.write(new_path)
  1. See error

Expected behavior

Should be able to append file without error

Environment (please complete the following information):

  • OS: Ubuntu Linux 20.04
  • Python 3.7
  • py7zr version: 0.20.5 (afa9951)

Test data(please attach in the report):

archive_from_7za.zip

Fix #518 merged.