1

I am working on an ETL process for a customer. Another vendor has provided the raw data as a set of approximately 100 password protected ZIP files.

I want to validate that the password given is correct for this set of files.

The script I am currently working with uses a loop and 7zip:

#!/bin/bash
set -x
for filename in ../TheData/*Data*of*.zip; do
        echo "Checking $filename"
        7z t -ple_super_secret_assword $filename
done

The output piped to a file is like this:

Checking ../TheData/Project1999Data_1of7.zip
+ 7z t '-pseeeecret' ../TheData/Project1999Data_1of7.zip

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Xeon(R) CPU E5-2470 0 @ 2.30GHz (206D7),ASM)

Scanning the drive for archives:
1 file, 632866983 bytes (604 MiB)

Testing archive: ../TheData/Project1999Data_1of7.zip

ERRORS:
Headers Error

When run without pipes there is a progress meter. I think the headers error is spurious since testing the archive works and spot checking some full extracts also seems OK.

Simply extracting all the data is not an option at this time, waiting on the change-request process for storage allocation.

Is there a faster way to simply make sure that a password works on a set of zip files?

Is there some way to capture the return codes from this and echo out a simple pass/fail? 7zip has several return codes documented

Is it possible to change this script to run the checks in parallel? 7z appears to use only 80% of one core, suggesting it is I/O bound.

There does not appear to be a change in performance with 7zip using either the t (test) or l (list) function.

  • if it helps to extract the files as a more thorough test, you could use the -so switch and redirect the output to /dev/null – Jeff Schaller Mar 29 '17 at 1:15
1

Check the shell error/exit code.

When I run 7zip to compress a file (and protect with a password),

echo "now that is what I call bubba" > bubba
7z a -psecret foo bubba
rm bubba #remove for convenience

I can run 7zip to attempt to extract/test the contents of the archive, and 7zip yields errors,

7z t -pnotsecret foo.7z
...
Extracting bubba    Data error in encrypted file. Wrong password?
...
Sub items Errors: 1

Then I can check the shell error code,

echo $?
2

When I provide the correct password,

7z t -psecret foo.7z
...
Extracting bubba
...
Everything is Ok

Then I check the shell error code,

echo $?
0

So you can use the shell error/exit code to simply check for correct password, or you can scan the output of 7z and grep -i "everything is ok".

Here is a revision of your script,

#!/bin/bash
set -x
for filename in ../TheData/*Data*of*.zip; do
    #echo "Checking $filename"
    7z t -ple_super_secret_assword $filename
    if [ $? -eq 0 ]; then echo "ok: $filename"
    else echo "error: password failed $filename"
    fi
done
| improve this answer | |
  • Thanks Chuck I see how the technique works. In my case checking the error code doesn't work because of the headers error. This doesn't seem to effect the extract but 7z still records it as an error and returns 2. I don't believe that header error is fatal but thats another post – Freiheit Mar 29 '17 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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