如何测试文件夹中的所有zip文件,以验证它们是否已损坏?


22

很久以前就有一个名为CFAtest的实用程序,它做得很好,但是已经消失了。

基本上,我正在寻找一种类似的工具(最好是图形工具),该工具将遍历给定的路径(最好包括子文件夹)并测试找到的所有zip文件。

体面的日志记录选项将是一个加号。


4
哪个操作系统?
Matteo

任何最近的打包程序都可以进行存档完整性检查。
忘了

Answers:


16

在每个子文件夹中找到每个zip文件

这会发现在当前文件夹的所有子文件夹(.)中的文件(-type f扩展名为)zip(或ZIPZipzIp等等,忽略大小写,-iname)并测试它们的完整性(选项-t)保持安静(选项-q,一起-tq)。足够表示:不列出zip文件的内容,而仅报告测试结果。

find . -type f -iname '*.zip' -exec unzip -tq {} \;

仅当前文件夹(无子文件夹)

如果只想检查当前目录中的文件,而不是任何子文件夹中的文件,请使用

unzip -tq '*.[Zz][Ii][Pp]'

在包含zip文件的目录中。此文件扩展名也检查ZIPZipzIp等等,忽略大小写。


1
如果您使用的是Windows,并且没有find,请安装Cygwin。
Daniel R Hicks

2
...或使用for命令。
卡兰


2
如果有许多ZIP文件,或者它们包含许多文件,则可能需要较少的详细输出。为此使用unzip的-q选项:unzip -tq
malamut

1
或为Windows安装git(如果您是开发人员,则可能已经安装了git),它带有bash和许多其他有用的gnu cli工具。
wp78de

14

在Windows上,我使用7zip:它提供了图形用户界面,是免费的,并且支持包括zip在内的各种存档文件格式。

导航到要在Windows资源管理器上分析的给定文件夹。搜索*.zip,选择所有文件,单击鼠标右键,选择“测试存档”

在此处输入图片说明

然后等待(注意,在7z开始测试之前,explorer.exe大约需要10分钟才能通过100,000 .zip):

在此处输入图片说明


8

在Mac上,erik的答案对我不起作用,但这适用于当前文件夹和所有子文件夹中的zip:

find . -name '*.zip' -exec unzip -tq {} \;

为每个文件输出:

No errors detected in compressed data of ./2013-10-16.zip.

2

快速PowerShell命令-使用7zip的命令行“ t”开关

$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


0

这是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秒

在此处输入图片说明

在此处输入图片说明

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.