python 解压zip rar 7z文件
- 1、zip等格式文件解压文件
- 2、删除临时文件
- 3、shutil添加解压7z格式文件支持
- 4、rar格式文件解压
- 利用 winrar 软件进行解压
- 5、zip和rar文件格式
1、zip等格式文件解压文件
使用shutil,支持的压缩文件格式,一般常用解压格式为.zip文件。
import shutilprint(shutil.unpack_formats())
输出:
[('bztar', ['.tar.bz2', '.tbz2'], "bzip2'ed tar-file"), ('gztar', ['.tar.gz', '.tgz'], "gzip'ed tar-file"), ('tar', ['.tar'], 'uncompressed tar file'), ('xztar', ['.tar.xz', '.txz'], "xz'ed tar-file"), ('zip', ['.zip'], 'ZIP file')]
解压文件
extract_dir = "./tmp/"
shutil.unpack_archive(current_file, extract_dir)
2、删除临时文件
解压文件后需要删除临时文件
import os
os.remove(full_name)
3、shutil添加解压7z格式文件支持
import os
from py7zr import unpack_7zarchive
import shutilif __name__ == "__main__":path = r"E:\Dataset\新建文件夹"suffix = ".zip" # ".7z" ".rar"# file_list = GetFiles(path, suffix)# print("there are ", len(file_list), "zip files")# file_list = GetFiles(path, suffix)# print("there are ", len(file_list), "rar files")shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)file_list = GetFiles(path, suffix)print("there are ", len(file_list), "7z files")current_file = file_list[0]print("current file ", current_file)extract_dir = "./tmp/"shutil.unpack_archive(current_file, extract_dir)
4、rar格式文件解压
安装 unrar 模块:
pip install rarfile
pip install unrar
下载 http://www.rarlab.com/rar/UnRARDLL.exe
安装后设置环境变量
测试
(pytorch190) C:\Users\wmz>python
Python 3.8.10 (default, May 19 2021, 13:12:57) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from unrar import rarfile
参考:解决Python下安装unrar后仍然提示Couldn’t find path to unrar library…
如果文件后缀名并不是文件的压缩格式,这就比较麻烦,比如后缀名为.rar的文件实际是.zip压缩格式。
这就需要判断文件的真实压缩格式,然后在做相应的处理。
import gzip
import os
import tarfile
import zipfile
from unrar import rarfiledef decompress_rar(src_file, dest_dir):"""Decompress rar file into destination direction"""rv = (True, '')try:rar = rarfile.RarFile(src_file)rar.extractall(dest_dir)except Exception as e:rv = (False, e)return rvreturn rvdef decompress_tar_and_tgz(src_file, dest_dir):"""Decomporess .tar or .tgz file into destination directory"""rv = (True, '')try:tar = tarfile.open(src_file)names = tar.getnames()for name in names:tar.extract(name, dest_dir)tar.close()except Exception as e:rv = (False, e)return rvreturn rvdef decompress_zip(src_file, dest_dir):"""Decompress .zip file into destination folder"""rv = (True, '')try:zip_file = zipfile.ZipFile(src_file)for name in zip_file.namelist():zip_file.extract(name, dest_dir)zip_file.close()except Exception as e:rv = (False, e)return rvreturn rvdef decompress_gz(src_file, dest_dir):"""Decompress .gz file into destination folder"""rv = (True, "")try:fname = dest_dir + '/' + os.path.basename(src_file)gfile = gzip.GzipFile(src_file)open(fname, "w+").write(gfile.read())gfile.close()except Exception as e:rv = (False, e)return rvreturn rvdef decompress(src_file, dest_dir):fname, ext = os.path.splitext(src_file)if ext in ('.tgz', '.tar'):decompress_tar_and_tgz(src_file, dest_dir)elif ext == '.zip':decompress_zip(src_file, dest_dir) elif ext == '.rar':decompress_rar(src_file, dest_dir)elif ext == '.gz':decompress_gz(src_file, dest_dir)def decompress_folder(src_dir):files = os.listdir(src_dir)for fname in files: # fname is file name with extensionname, ext = os.path.splitext(fname) # name is file name without extensionsrc_file = os.path.join(src_dir, fname)dest_path = os.path.join(src_dir, name)if not os.path.exists(dest_path):os.mkdir(dest_path)decompress(src_file, dest_path)print(src_file, 'was decompressed.')if __name__ == '__main__':src_dir = r'D:\some_folder_name'decompress_folder(src_dir)
利用 winrar 软件进行解压
winrar 提供命令行解压,Python 语言可以调用 winrar 命令。在执行命令之前,把 winrar.exe 所在的路径加到环境变量。然后,比如,我们要把 D:\test 下所有 zip 文件解压,可以用下面的命令:
winrar.exe x D:\test\*.zip D:\test\unzip\
winrar 的命令行参数很多,这里不展开,x 表示使用绝对路径进行解压。Python 对 winrar 命令进行封装的代码如下:
import osdef unzip_folder(sourcepath):if sourcepath[-1:] == "\\":files = sourcepath + "*.zip"else:files = sourcepath + "\\" + "*.zip"dest = sourcepath + "\\unzip\\"cmd = 'WinRAR.exe x {} {}'.format(files, dest)os.system(cmd) if __name__ == "__main__":source_folder = r"D:\test\\"unzip_folder(source_folder)
参考:Python解压常见格式的压缩文件
5、zip和rar文件格式
根据文件头判断文件格式
参考:压缩包Zip格式详析(全网最详细)
参考:RAR文件格式学习(了解)