一、7z壓縮文件的壓縮和解壓
1、安裝py7zr
我們要先安裝py7zr
第三方庫:
pip install py7zr
如果python環(huán)境有問題,執(zhí)行上面那一條安裝語句老是安裝在默認(rèn)的python環(huán)境的話,我們可以執(zhí)行下面這條語句,將第三方庫安裝在項目的虛擬環(huán)境中:
pip install py7zr --target=E:\Python腳本\作業(yè)查重\OS_Study\venv\Lib\site-packages
2、解壓7z文件
import py7zr
# 將壓縮文件解壓到指定目錄
def decompress_7z():
# 將要解壓的壓縮文件路徑
archive = py7zr.SevenZipFile(r'E:\Python腳本\作業(yè)查重\20大數(shù)據(jù)班Javaweb新聞系統(tǒng).7z', mode='r')
# 壓縮文件的解壓目錄
archive.extractall(path=r'E:\Python腳本\作業(yè)查重\20大數(shù)據(jù)班Javaweb新聞系統(tǒng)')
archive.close()
3、壓縮成7z文件
import py7zr
# 將指定目錄壓縮到指定壓縮文件test.7z'
def compression_7z():
# 生成的壓縮文件路徑
archive = py7zr.SevenZipFile(r'E:\Python腳本\作業(yè)查重\test.7z', mode='w')
# 需要壓縮的壓縮文件
archive.writeall(path=r'../test')
archive.close()
二、rar壓縮文件的壓縮和解壓
1、環(huán)境準(zhǔn)備
我們用到的第三方庫為rarfile
,因為我們的這個第三方庫需要用到第三方程序,所以我們要先配一下環(huán)境。
(1)導(dǎo)入unrar
模塊:
pip install unrar
(2)下載 unrar library
并按照默認(rèn)安裝路徑安裝,下載鏈接:下載
(3) 編輯環(huán)境變量:
用戶變量 -> 變量名:
x64
-> 變量值:C:\Program Files (x86)\UnrarDLL\x64
(默認(rèn)路徑是這個)
系統(tǒng)變量 -> 變量名:UNRAR_LIB_PATH
-> 變量值:C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll
(默認(rèn)路徑)[32位系統(tǒng)下的變量值為C:\Program Files (x86)\UnrarDLL\UnRAR.dll
]
(4)安裝winrar
(360軟件中心有):
將 winrar
的目錄下的 unrar.exe
復(fù)制到 Python 路徑的 Scripts
文件夾下。
(5)重啟Pycharm
2、安裝rarfile
執(zhí)行以下命令:
pip install rarfile
3、解壓rar文件
import rarfile
def decompress_rar():
# 找到rar文件
z = rarfile.RarFile(r'E:\Python腳本\作業(yè)查重\2015090103石凱-新聞管理系統(tǒng).rar')
# 指定解壓輸出的目錄
z.extractall(r'E:\Python腳本\作業(yè)查重\2015090103石凱-新聞管理系統(tǒng)')
z.close()
# 刪除壓縮文件
# os.remove(pathRar)
4、壓縮成rar文件
由于rarfile
只能解壓文件不能壓縮文件,所以我們需要調(diào)用第三方程序來完成。
def compress(input_file, output_file, root_path,
rar_path='D:/"Program Files"/WinRAR/WinRAR.exe'):
"""
調(diào)用CMD命令壓縮文件/文件夾
Parameters
----------
input_file : 需要壓縮的文件/文件夾名。從哪一級目錄開始,就會從哪一級開始壓縮;
output_file : 壓縮文件的輸出路徑及其壓縮的文件名;
可以是.rar, .zip;
root_path: input_file 所在目錄;
rar_path : WinRAR軟件的安裝路徑,
The default is 'C:/"Program Files"/WinRAR/WinRAR.exe'.
NOTE: 路徑和文件名中帶空格的時候一定要多加一重引號?。? """
cmd_command = r'%s a %s %s' % (rar_path, output_file, input_file)
print(root_path)
os.chdir(root_path) # 切換工作目錄
print(cmd_command)
os.system(cmd_command)
if os.system(cmd_command)==0:
print('Successful backup to', output_file)
else:
print('Backup FAILED', input_file)
def rar(paths):
files = os.listdir(paths)
for path in files:
input_file = '"' + path + '"'
out = path.split('.')[0] + '_bak.rar'
out_file = '"' + out + '"'
print(path)
print(out)
compress(input_file,out_file,paths)
參考文章:https://blog.csdn.net/hanmengaidudu/article/details/120193682
三、zip文件的壓縮和解壓
1、安裝zipfile
執(zhí)行以下命令:
pip install zipfile
2、解壓zip文件
使用zipfile
的extract()
或extractall()
方法直接解壓時,文件名可能會出現(xiàn)亂碼,所以我們要特別解決這個問題:
# 出現(xiàn)亂碼時解碼
def recode(raw: str) -> str:
try:
return raw.encode('cp437').decode('gbk')
except:
return raw.encode('utf-8').decode('utf-8')
# 解壓zip文件
def decompress_zip(pathZip, obj):
zipFile = zipfile.ZipFile(pathZip) # 壓縮包路徑
zipFileList = zipFile.namelist() # 獲取壓縮包里所有文件
print('-------------------正在解壓-----------------------')
for f in zipFileList:
zipFile.extract(f, obj) # 循環(huán)解壓文件到指定目錄
name1 = os.path.join(obj, f) # 亂碼文件名
name2 = os.path.join(obj, recode(f)) # 解碼后文件名
os.rename(name1, name2) # 文件重命名
zipFile.close() # 關(guān)閉文件釋放內(nèi)存
print('-------------------解壓完成-----------------------')
# 刪除壓縮文件
# os.remove(pathZip)
3、壓縮成zip文件
參考文章:https://blog.csdn.net/Likianta/article/details/126710855
參考文章:https://blog.csdn.net/ooowwq/article/details/125949394
參考 文章:https://blog.csdn.net/qq_36182112/article/details/127630950文章來源:http://www.zghlxwxcb.cn/news/detail-470747.html
四、批量解壓
如果想要實現(xiàn)批量解壓某個目錄下的全部壓縮包的話,可以配合python自帶的od庫來實現(xiàn),一些參考代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-470747.html
# 批量解壓文件
def batch_decompress():
file_names = os.listdir(r'E:\Python腳本\作業(yè)查重\20大數(shù)據(jù)班Javaweb新聞系統(tǒng)') # 壓縮文件所在的目錄
# print(file_names)
print('-------------------正在解壓-----------------------')
for file_name in file_names:
# 將文件名與目錄拼接起來,得到文件絕對路徑地址
path_name = os.path.join(r'E:\Python腳本\作業(yè)查重\20大數(shù)據(jù)班Javaweb新聞系統(tǒng)', file_name)
print(path_name)
if path_name[path_name.find('.')+1:] == 'zip':
decompress_zip(path_name)
elif path_name[path_name.find('.')+1:] == 'rar':
decompress_rar(path_name)
else:
decompress_7z(path_name)
print('-------------------解壓完成-----------------------')
到了這里,關(guān)于Python實現(xiàn)rar、zip和7z文件的壓縮和解壓的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!