lzma压缩分卷解压

最近重写EncryptionAssistant的时候,发现bat版的EncryptionAssistant在创建压缩分卷的时候创建的是7z.xz.001后缀的压缩包,这个不是7z压缩格式,而是属于lzma压缩格式,如果要处理这种压缩格式的文件,就只能通过lzma模块来解决,还有对于压缩分卷,需要提前合并之后再提取,参考代码如下:

Python
def _extract_xz_file(self, file_path: str, target_path: str) -> bool:  
    """提取xz压缩分卷  
    实现原理:先合并所有的xz压缩分卷,然后通过lzma解码提取出7z文件,最后解压7z文件  
    """    file_list: list = self._find_sub_file(file_path)  
    combine_xz_file: str = os.path.splitext(os.path.basename(file_path))[0]  

    # 合并成一个xz压缩包  
    try:  
        with lzma.open(os.path.join(target_path, combine_xz_file), "wb") as xz_file:  
            for file in file_list:  
                with open(file, "rb") as f:  
                    xz_file.write(f.read())  
    except Exception as e:  
        customLogger.warning(f"合并xz压缩分卷错误,提示为{e}")  
        return False  
    # 提取压缩包中的内容  
    output_file: str = NameGenerator.temp_name("7z")  
    try:  
        with lzma.open(os.path.join(target_path, combine_xz_file), "rb") as xz_file:  
            lzc: lzma = lzma.LZMADecompressor()  
            result: bytes = lzc.decompress(xz_file.read())  
            with open(os.path.join(target_path, output_file), "wb") as f:  
                f.write(result)  
    except Exception as e:  
        customLogger.warning(f"提取xz压缩分卷错误,提示为{e}")  
        return False  
    # 提取7z压缩包中的内容  
    try:  
        with SevenZipFile(  
            os.path.join(target_path, output_file),  
            "r",  
            filters=CommonVariable.filters,  
        ) as archive:  
            archive.extractall(path=target_path)  
        try:  
            os.remove(os.path.join(target_path, combine_xz_file))  
            os.remove(os.path.join(target_path, output_file))  
        except Exception as e:  
            customLogger.warning(f"无法删除临时创建的文件,报错提示为{e}")  
        return True  
    except Exception as e:  
        customLogger.warning(f"提取7z压缩分卷失败,提示为{e}")  
        return False

对于xz压缩文件,在处理的时候比较特殊,虽然看着是压缩包,但是在编码层面上看可以认为是经过了特殊压缩的文件,就按照常规的文件读写去操作就行了,只不过在常规的文件操作的基础上要额外引入lzma模块