ineedhelp
ineedhelp

Reputation: 43

Is there a way to check if a zip file is password protected?

I'm writing a bash script which compresses (with encryption or without, depending on what the user chooses) and decompresses files using zip. My problem is the decompressing part - when the user chooses a zip file to decompress (via zenity file selection), is there any way to check if the file is password protected in order to ask the user for the password (or if it isn't, just proceed and decompress the file)?

I didn't really try anything yet, as I've struggled to come up with any idea or find a solution on stack and other websites.

Upvotes: 2

Views: 8002

Answers (3)

The best way i found

To always try to set password.

If password not exist set qwerty

7z x -aou /tmp/archive.zip -pqwerty

If encrypted, there must be message encrypted archive! Wrong password

For additional check i recommend command that print technical info by 7z.

If password not exist set qwerty

7z l -slt /tmp/archive.zip -pqwerty

Output must contain Encrypted = + or encrypted archive! Wrong password

Upvotes: 0

Steve Hibbert
Steve Hibbert

Reputation: 2175

Also: In a DOS prompt, type "unzip myfile.zip" and if you get prompted for the password, it is protected.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88949

if 7z l -slt file.zip | grep -q ZipCrypto; then
  echo "password protected / encrypted"
else
  echo "no password protection / not encrypted"
fi

See: How to detect zip file encryption algorithm

Upvotes: 6

Related Questions