Answers:
这会发现在当前文件夹的所有子文件夹(.
)中的文件(-type f
扩展名为)zip
(或ZIP
或Zip
或zIp
等等,忽略大小写,-iname
)并测试它们的完整性(选项-t
)保持安静(选项-q
,一起-tq
)。足够表示:不列出zip文件的内容,而仅报告测试结果。
find . -type f -iname '*.zip' -exec unzip -tq {} \;
如果只想检查当前目录中的文件,而不是任何子文件夹中的文件,请使用
unzip -tq '*.[Zz][Ii][Pp]'
在包含zip文件的目录中。此文件扩展名也检查ZIP
或Zip
或zIp
等等,忽略大小写。
find
,请安装Cygwin。
for
命令。
$7z = "T:\folder\to\7z.exe"
Dir "C:\folder\to\check" -r -include @("*.zip","*.7z") | % { & $7z t $_ -r}
7-Zip 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Processing archive: D:\testfile.zip
Testing my test file.txt
Testing second file.doc
Everything is Ok
Folders: 0
Files: 2
Size: 10353
Compressed: 5721
可能不是您所要求的,但是有一个名为Zip2Fix的工具。
我没有使用过,但是您可以从这里下载:
这是Python脚本下面的脚本,用于测试位于一个或多个文件夹中的zip文件。我在Windows 7 SP1 x64 Ultimate上进行了测试,但是我希望它可以在任何OS上运行。
输出示例:
Total time spent was 577.64 seconds, checking 100 files, totaling 77.06 GB,
among which 0 were corrupted.
脚本:
'''
Test if the zip files are not corrected
'''
from __future__ import print_function
from __future__ import division
import sys
import zipfile
import glob
import os
import time
def test_zipfile(filepath):
'''
Test whether a zipfile is valid
Some lines were taken from http://stackoverflow.com/questions/4875747/python-script-to-check-if-a-zip-file-is-corrupt
'''
start_time = time.time()
filesize = os.path.getsize(filepath)
print('Starting testing file: {0} ({1:.2f} MB)'.format(filepath,filesize/10**6), end='')
the_zip_file = zipfile.ZipFile(filepath)
ret = the_zip_file.testzip()
time_spent = time.time() - start_time
print('\tTest ended. Time spent: {0:.2f} s'.format(time_spent))
if ret is not None:
print("First bad file in zip {0}: {1}".format(filepath,ret))
is_valid = False
else:
#print "Zip file is good."
is_valid = True
return is_valid, time_spent, filesize
def main():
'''
This is the main function
'''
# Parameters
zipfiles_root_folder = '.'
log_filepath_corrupted = 'result_corrupted.log'
log_file_corrupted = open(log_filepath_corrupted, 'w')
log_filepath_valid = 'result_valid.log'
log_file_valid = open(log_filepath_valid, 'w')
zipfile_filepaths = sorted(glob.iglob(os.path.join(zipfiles_root_folder, '*', '*.zip'))) # Modify this to whatever folders you need
# Testing zipfiles
start_time = time.time()
total_filesize = 0
number_of_corrupted_zipfile = 0
for zipfile_filepath in zipfile_filepaths: # generator, search immediate subdirectories
is_valid, test_zipfile_time_spent, filesize = test_zipfile(zipfile_filepath)
total_filesize += filesize
if is_valid:
log_file_valid.write('{0}\n'.format(zipfile_filepath))
else:
log_file_corrupted.write('{0}\n'.format(zipfile_filepath))
number_of_corrupted_zipfile += 1
# Cleaning
log_file_corrupted.close()
log_file_valid.close()
time_spent = time.time() - start_time
print('Total time spent was {0:.2f} seconds, checking {1} files, totaling {2:.2f} GB, among which {3} were corrupted.'.format(time_spent, len(zipfile_filepaths),total_filesize/10**9,number_of_corrupted_zipfile))
if __name__ == "__main__":
main()
#cProfile.run('main()') # if you want to do some profiling
它还会写一个包含所有有效zip文件的日志文件以及一个包含所有损坏的zip文件的日志文件。
针对7zip的速度基准测试:Python 577.64秒和7zip 609秒