zipfile.extract throwing CrcError for .7z files

the following code I wrote to extract the files from .7z zip file. Code is throwing "Exception has occurred: CrcError". if I am specifying files one by one code works fine.In for loop it throws CrcError.

    selective_files = 'folder\abc[1].txt,folder\abc[2].txt'
    with py7zr.SevenZipFile(fol_doc_text, 'r') as archive:
        
        allfiles = archive.getnames()
        file_exists_list = []
        for file_name in selective_files.split(","):
            if file_name in allfiles:
                # extract the files in the folders
                archive.extract(awards_extract_location,targets=file_name)
            else:
                print(f'file does not exists in .7z')
                new_file_name = check_and_rename(file_name,allfiles,file_exists_list)
                archive.extract(location,targets=new_file_name)

        extraction works fine for 
        c:\data\extract\folder\abc[1].txt 
        
        throw CRC error with abc[2].txt file.
        c:\data\extract\folder\abc[2].txt 
        
        

>Solution :

According to the documentation:

Once extract() called, the SevenZipFIle object become exhausted and EOF
state. If you want to call read(), readall(), extract(),
extractall() again, you should call reset() before it.

Leave a Reply