本文主要是介绍Python压缩与解压zip/rar文件 [附源码],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
前言
效果
代码
成品体验
应用场景示例
微信文件拖拽解压与压缩
前言
没想到整个压缩解压还挺麻烦,以下代码是经过实测通过的。
目前只完成了zip的压缩和解压,rar的由于需要另外装exe所以暂时简单实现。
效果
文件
压缩
解压
代码
import os
import shutil
import zipfile
os.environ['UNRAR_LIB_PATH'] = r'./UnRAR64.dll'
from unrar import rarfiledef zip_uncompress(file_name: str):if not zipfile.is_zipfile(file_name):print('>> 不是有效的zip文件!')returnre_decode = Falsestore_path = os.path.dirname(file_name)with zipfile.ZipFile(file_name, 'r') as f:namelist = sorted(f.namelist())unzip_filepath = store_path + os.sep + namelist[0]try:unzip_filepath_gbk = unzip_filepath.encode('cp437').decode('gbk')re_decode = Trueexcept:unzip_filepath_gbk = unzip_filepathif os.path.exists(unzip_filepath_gbk):print('>> 文件夹已存在,请先删除!')returnfor fn in namelist:f.extract(fn, store_path)try:fn_gbk = fn.encode('cp437').decode('gbk')re_decode = Trueexcept:fn_gbk = fnfn = os.path.join(store_path, fn)fn_gbk = os.path.join(store_path, fn_gbk)os.rename(fn, fn_gbk)if os.path.exists(unzip_filepath) and re_decode and unzip_filepath.encode('utf-8').isalpha():print('删除临时文件')shutil.rmtree(unzip_filepath)os.startfile(unzip_filepath_gbk)print('>> 解压完成,已打开对应文件夹!')def zip_compress(to_zip, save_zip_name=None):if not os.path.exists(to_zip):print('>> 文件夹不存在!')returnsave_zip_name = save_zip_name or (to_zip + '.zip')# 1.先判断输出save_zip_name的上级是否存在(判断绝对目录),否则创建目录# save_zip_name的上级目录save_zip_dir = os.path.split(os.path.abspath(save_zip_name))[0]if not os.path.exists(save_zip_dir):os.makedirs(save_zip_dir)print('>> 创建新目录%s' % save_zip_dir)f = zipfile.ZipFile(os.path.abspath(save_zip_name), 'w', zipfile.ZIP_DEFLATED)# 2.判断要被压缩的to_zip是否目录还是文件,是目录就遍历操作,是文件直接压缩# 如果不是目录,那就是文件if not os.path.isdir(os.path.abspath(to_zip)):# 判断文件是否存在if os.path.exists(os.path.abspath(to_zip)):f.write(to_zip)f.close()print('%s压缩为%s' % (to_zip, save_zip_name))else:print('%s文件不存在' % os.path.abspath(to_zip))else:# 判断目录是否存在,遍历目录if os.path.exists(os.path.abspath(to_zip)):zipList = []zipList.append(to_zip)# 遍历目录,加入列表for dir, subdirs, files in os.walk(to_zip):for fileItem in files:zipList.append(os.path.join(dir, fileItem))for dirItem in subdirs:zipList.append(os.path.join(dir, dirItem))# 读取列表压缩目录和文件for i in zipList:# replace是减少压缩文件的一层目录,即压缩文件不包括to_zip这个目录# os.path.dirname(to_zip)表示到上层目录# .encode('gbk').decode('cp437')f.write(i, i.replace(os.path.dirname(to_zip), ''))f.close()print('>> 压缩完成,路径是:' + save_zip_name)else:print('>> [%s]文件夹不存在' % os.path.abspath(to_zip))def rar_uncompress(file_name: str):if not rarfile.is_rarfile(file_name):print('>> 不是有效的rar文件!')returnunzip_filepath = file_name.split('.')[0]if os.path.exists(unzip_filepath):print('>> 文件夹已存在!')# returnelse:os.mkdir(unzip_filepath)with rarfile.RarFile(file_name, 'r') as f:f.extractall(unzip_filepath)os.startfile(unzip_filepath)print('>> 解压完成,已打开对应文件夹!')#rar_uncompress(r'C:\Users\SXF\Desktop\1.rar')
#zip_compress(r'C:\Users\SXF\Desktop\build\demo')
#zip_uncompress(r'C:\Users\SXF\Desktop\build\demo.zip')
#exit()while True:file = input('>> 请将文件拖入后按回车键: ')file = file.strip('"')if os.path.isdir(file):print('>> 是文件夹,即将进入压缩模式!')zip_compress(file)elif os.path.isfile(file):print('>> 是压缩包,即将进入解压模式!')if file.endswith('rar'):print('>> rar格式!')rar_uncompress(file)elif file.endswith('zip'):print('>> zip格式!')zip_uncompress(file)else:print('>> 输入非法!')print('\n')input('\n>> 任意键退出')
成品体验
python压缩与解压 - 成品+源码+dll文件http://xfxuezhang.cn/web/share/%E8%BD%AF%E4%BB%B6-%E7%94%B5%E8%84%91/python%E5%8E%8B%E7%BC%A9%E4%B8%8E%E8%A7%A3%E5%8E%8B.zip
应用场景示例
微信文件拖拽解压与压缩
这篇关于Python压缩与解压zip/rar文件 [附源码]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!