這裏是我已經把一個Python腳本。到目前爲止,它的測試已經很少了。我已經在Python 2.6中完成了基本的測試。但我更喜歡Python的概念,因爲如果發生任何錯誤,它應該異常中止,而bash腳本可能不會。
這首先檢查輸入文件是否有效並且尚未解壓縮。然後它將輸入文件複製到「.bak」擴展名的「備份」文件中。然後它解壓縮原始文件,覆蓋它。
我確定有些事情我忽略了。請隨時提供反饋。
#!/usr/bin/python
# Note, written for Python 2.6
import sys
import shutil
import zipfile
# Get a single command-line argument containing filename
commandlineFileName = sys.argv[1]
backupFileName = commandlineFileName + ".bak"
inFileName = backupFileName
outFileName = commandlineFileName
checkFilename = commandlineFileName
# Check input file
# First, check it is valid (not corrupted)
checkZipFile = zipfile.ZipFile(checkFilename)
checkZipFile.testzip()
# Second, check that it's not already uncompressed
isCompressed = False
for fileObject in checkZipFile.infolist():
if fileObject.compress_type != zipfile.ZIP_STORED:
isCompressed = True
if isCompressed == False:
raise Exception("File is already uncompressed")
checkZipFile.close()
# Copy to "backup" file and use that as the input
shutil.copy(commandlineFileName, backupFileName)
inputZipFile = zipfile.ZipFile(inFileName)
outputZipFile = zipfile.ZipFile(outFileName, "w", zipfile.ZIP_STORED)
# Copy each input file's data to output, making sure it's uncompressed
for fileObject in inputZipFile.infolist():
fileData = inputZipFile.read(fileObject)
outFileObject = fileObject
outFileObject.compress_type = zipfile.ZIP_STORED
outputZipFile.writestr(outFileObject, fileData)
outputZipFile.close()
這是在Mercurial repository in BitBucket。
了不起的信息。目前我最感興趣的是Subversion和Mercurial。我不認爲Subversion有乾淨/污跡類型的功能。 Mercurial沒有想法 - 我相對較新。 – 2009-06-10 16:16:28
@克雷格:Mercurial有鉤子。 – Borealid 2010-08-12 00:55:02