2008年3月21日 星期五

[Python] 將一堆 zip 檔批次壓成 7z -- zip27Zip

根據經驗, 7 zip 在壓縮 nds 檔時的會比 zip 小大約 1/3,

一次把所有的 nds 檔, 全部壓縮成 7z.

 


# Python 批次處理範例 Part 1
# 將目前目錄中所有的 nds 檔全部重新壓縮成 7-zip 檔

# Note: 原來的 nds 保留給 user 自行刪除
# 井民全
import os
import glob

# Specified the file types which you want to compress
strSearchingPattern=['*.nds']

# save old directory info
oldDir=os.getcwd()
top=oldDir;

print top
count=0
for root, dirs, files in os.walk(top, topdown=False):
for pattern in strSearchingPattern :
ndsWide=os.path.join(top, pattern) # L:/nds_back/*.nds
ndsFileList=glob.glob(ndsWide) # list all files end with nds in current directory walk
for x in ndsFileList :
print x # list all nds files
# \" ==> "
# windows directory \ => \\
tartgetString='\"'+x+ '.7z'+'\"'
source=' \"'+x+'\"'
op=' a -t7z '
command='C:\\\"Program Files\"\\7-Zip\\7z.exe'+op+tartgetString+source
print command

os.system(command)

os.chdir(oldDir)



如果你想將手上所有的 zip 檔全部轉成 7-zip,
下面是一個 Python 批次範例




# Python 批次處理範例
# 將目前目錄中所有的 zip 檔全部重新壓縮成 7-zip 檔
# 簡單的 3 步驟
# 1. 把 test.zip 解到 UnzipTemp 目錄中
# 7z e test.zip -o.\UnzipTemp
# 2. 把 UnzipTemp 目錄的內容重新壓縮成 7-zip 格式
# 7z a -t7z test.zip.7z UnzipTemp
# 3. 移除 UnzipTemp 目錄

# Note:
# 1. 原來的 zip 保留給 user 自行刪除
# 2. 因為這是要來壓 nds 檔 (ARM CPU) , 所以如果使用其他檔的壓縮請把 BC_ARM 拿掉
# 井民全

import os
import glob

# Specified the file types which you want to compress
strSearchingPattern=['*.zip']

# save old directory info
oldDir=os.getcwd()
top=oldDir;

print 'Current Directory:\n\t'+top
count=0
for root, dirs, files in os.walk(top, topdown=False):
for pattern in strSearchingPattern :
Wide=os.path.join(top, pattern) # L:/nds_back/*.zip
FileList=glob.glob(Wide) # list all files end with zip in current directory walk
for x in FileList :
print x # list all nds files
# Step 1:
# unZip all file endwith zip to UnzipTemp
zipSource=' \"'+x+'\" '
unzipCommand= '7z.exe e '+zipSource+ '-o.\UnzipTemp'
print 'unzipCommand Command:\n\t'+unzipCommand
os.system(unzipCommand);

# Step 2:
# 7-Zip the UnzipTemp directory
# \" ==> "
# windows directory \ => \\
tartgetString='\"'+x+ '.7z'+'\" '
source=' UnzipTemp'
op=' a -t7z '
op2=' -m0=BC_ARM -m1=LZMA:d=21 -ms -mmt' # solid archive using LZMA method with 2 MB dictionary and BCJ converter. Compression will use multithreading optimization.
command='C:\\\"Program Files\"\\7-Zip\\7z.exe'+op+tartgetString+source+op2
print 'command:\n\t'+command
os.system(command)

# Step 3: remove the source zip file
# os.system('del'+zipSource);

# Step 4:
# remove the tempory directory UnzipTemp
os.system('rmdir /s /q UnzipTemp')
os.chdir(oldDir)



by Jing

1 則留言: