py7zr icon indicating copy to clipboard operation
py7zr copied to clipboard

Extracting a corrupt 7z file and try to delete it will cause the PermissionError in windows

Open ok-oldking opened this issue 7 months ago • 4 comments

import os

import py7zr

try:
    with py7zr.SevenZipFile('corrupt.zip', mode='r') as z:
        z.extractall('test')
except Exception as e:
    os.remove('corrupt.zip')

When I’m extracting a particular corrupted 7z file, I get the PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ‘corrupt.7z’. This error only occurs with this corrupted file. When I test with other random invalid 7z files, the error does not occur. Because the file is 80MB, I can't upload it to github, here is the google drive link

Traceback (most recent call last):
  File "D:\projects\ok-wuthering-waves\test.py", line 7, in <module>
    z.extractall('test')
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\py7zr.py", line 999, in extractall
    self._extract(path=path, return_dict=False, callback=callback)
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\py7zr.py", line 629, in _extract
    self.worker.extract(
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\py7zr.py", line 1313, in extract
    raise exc_info[1].with_traceback(exc_info[2])
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\py7zr.py", line 1338, in extract_single
    self._extract_single(fp, files, path, src_end, q, skip_notarget)
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\py7zr.py", line 1407, in _extract_single
    crc32 = self.decompress(fp, f.folder, obfp, f.uncompressed, f.compressed, src_end, q)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\py7zr.py", line 1466, in decompress
    tmp = decompressor.decompress(fp, min(out_remaining, max_block_size))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\compressor.py", line 721, in decompress
    tmp = self._decompress(data, max_length)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\ok-wuthering-waves\venv\Lib\site-packages\py7zr\compressor.py", line 677, in _decompress
    data = decompressor.decompress(data, max_length)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_lzma.LZMAError: Corrupt input data

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\projects\ok-wuthering-waves\test.py", line 9, in <module>
    os.remove('corrupt.7z')
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'corrupt.7z'

ok-oldking avatar Jul 21 '24 13:07 ok-oldking