26

There was a utility a long time ago called CFAtest that was doing this quite nicely, but it has disappeared.

Basically, I am looking for a similar tool (preferably graphical) that will traverse a given path (preferably including subfolders) and test all zip files that it finds.

Decent logging options would be a plus.

2
  • 4
    Which operating system?
    – Matteo
    Dec 31, 2012 at 10:23
  • Any recent packer can make archive integrity checks.
    – Overmind
    Jul 20, 2017 at 12:31

6 Answers 6

20

Find every zip-file in every subfolder

This will find in all subfolders of the current folder (.) the files (-type f) with the extension zip (or ZIP or Zip or zIp and so on, case is ignored, -iname) and test their integrity (option -t) being quiet (option -q, together -tq). Being quite means: Not list the content of the zip-file, but only report test result.

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

Just current folder (no subfolders)

If you just want to check the files in the current directory, and not in any subfolders, use

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

in the directory with the zip files. This also checks for file extensions ZIP or Zip or zIp and so on, case is ignored.

6
  • 1
    And if you are on Windows and don't have find, install Cygwin. Jan 23, 2013 at 12:23
  • 2
    ... or use the for command.
    – Karan
    Jul 15, 2013 at 6:17
  • ... or download gnuwin32.sourceforge.net Mar 8, 2014 at 18:36
  • 2
    If there are many ZIP files, or if they contain many files, less verbose output might be desired. Use unzip's -q option for that: unzip -tq
    – malamut
    Mar 19, 2016 at 15:56
  • 1
    Or install git for Windows (you may have it already if you are a developer) which comes with a bash and many other helpful gnu cli tools.
    – wp78de
    Jul 15, 2017 at 1:00
18

On Windows I use 7zip: it provides a graphical user interface, is free and supports a wide range of archive file formats including zip.

Navigate to the given folder you want to analyze on Windows Explorer. Search for *.zip, select all files, right-click, select "Test Archive"

enter image description here

Then wait (note that it takes around 10 minutes for explorer.exe to go through 100,000 .zip before 7z start testing):

enter image description here

8

erik's answer didn't work for me on a Mac but this works for zips in current folder and all subfolders:

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

Outputs this for each file:

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

Quick PowerShell command - using 7zip's commandline "t" switch

$7z = "T:\folder\to\7z.exe"
Dir "C:\folder\to\check" -r -include @("*.zip","*.7z") | % { & $7z t $_ -r}

Output

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
1
  • Is there any way that we get the errors/warnings of this ?
    – e4rthdog
    Jan 16 at 10:08
1

It might not be quite what you asked for but there is a tool named Zip2Fix.

I haven't used it but you can download it from here:

http://leelusoft.blogspot.com/2011/06/zip2fix-10.html

0

Here is below a script in Python to test zipfiles located in one or several folders. I tested it on Windows 7 SP1 x64 Ultimate but I would expect it to work on any OS.

Example of output:

Total time spent was 577.64 seconds, checking 100 files, totaling 77.06 GB, 
among which 0 were corrupted.

Script:

'''
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

It also writes a log file containing all valid zip files as well as a log file containing all corrupted zip files.

Speed benchmark against 7zip: 577.64 seconds Python vs. 609 seconds 7zip

enter image description here

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.