python解压rar文件,直接在内存读取

必须要的依赖:apt install unrar

from rarfile import RarFile
io_buffer = io.BytesIO(response.body)
with RarFile(io_buffer) as fs:
          for each in fs.infolist():
              content = ""
              bytes_info = b''
              for i in range(0, 2):
                  try:
                      bytes_info = fs.open(each, pwd=f"{i}").read()
                      break
                  except Exception:
                      log_error(f"parse_file_error:{traceback.format_exc()}")

              try:
                  content = bytes_info.decode("utf-8")
              except Exception:
                  content = bytes_info.decode("gb18030")
posted @ 2024-04-03 17:44  公众号python学习开发  阅读(52)  评论(0编辑  收藏  举报