Test Archive Integrity with 7-Zip Command Line
Testing the integrity of an archive ensures that your compressed files are not corrupted and can be successfully extracted when needed. This guide explains how to use the 7-Zip command-line tool to test archive integrity without extracting the contents to your disk. You will learn the primary test command syntax, how to handle password-protected files, techniques for batch testing multiple archives, and how to verify exit codes for automated scripts.
The Basic Test Command
To verify the integrity of an archive without writing files to disk,
use the t (test) command followed by the archive name.
7z t archive.7zDepending on your installation and operating system, the executable
name might be 7z, 7za (standalone), or
7zr (reduced version).
When you run this command, 7-Zip reads the archive headers, decompresses the data streams in memory, and compares the calculated checksums against the stored CRC or SHA values. If the archive is healthy, the output concludes with:
Everything is Ok
Testing Password-Protected Archives
If the archive is encrypted, supply the password using the
-p switch so 7-Zip can decrypt and validate the compressed
data blocks.
7z t -pMyPassword secure_archive.7zIf you do not specify a password on the command line for an encrypted file, 7-Zip will prompt you to enter it interactively.
Testing Multiple Archives
You can test multiple archives simultaneously using wildcards.
To test all .zip files in the current directory:
7z t *.zipTo recursively test all supported archives within the current
directory and all subdirectories, add the -r switch:
7z t -r *.7zAutomating Integrity Checks with Exit Codes
When writing batch files or shell scripts, you can check 7-Zip's return code (exit code) to determine whether the test passed or failed.
Common 7-Zip Return Codes
- 0: Success (No errors found)
- 1: Warning (Non-fatal error)
- 2: Fatal error (Corrupted data, bad checksum, or incorrect password)
- 7: Command line error
- 8: Not enough memory for operation
Windows Batch Script Example
@echo off
7z t backup.zip > nul
if %errorlevel% equ 0 (
echo Archive is valid.
) else (
echo Archive is corrupt or test failed!
)Linux / Bash Script Example
#!/bin/bash
if 7z t backup.tar.gz > /dev/null; then
echo "Archive is valid."
else
echo "Archive is corrupt or test failed!"
fiDirecting the output to nul (Windows) or
/dev/null (Linux) suppresses the standard output, keeping
your terminal or logs clean while still capturing the return code.