% stephan@ape (/home/stephan/t0) %
% echo 'just another perl hacker' > secret.txt
% stephan@ape (/home/stephan/t0) %
% zip -P sesame safe.zip secret.txt
adding: secret.txt (stored 0%)
% stephan@ape (/home/stephan/t0) %
% perl safe_unzip.px sesame safe.zip
.cmd: [unzip -qoP sesame safe.zip 2>/dev/null]
.okay: got secret file! status = [0]
1 just another perl hacker$
####
#!/usr/bin/perl
use strict;
use warnings;
my $password = shift or die "args?";
my $zipfile = shift or die "zipfile?";
my $secret_file = 'secret.txt';
# use -q (silent mode)
# use -o (overwrite mode) o unzip hangs if secret file already exists
my $cmd = "unzip -qoP $password $zipfile 2>/dev/null";
print ".cmd: [$cmd]\n";
system($cmd);
my $status = ($? >> 8); # status of cmd, like POSIX WIFEXITED() macro
if ($status) {
die "**ERROR: unzip failed! status = [$status]";
}
print ".okay: got secret file! status = [$status]\n";
-f $secret_file && print qx(cat -evnt $secret_file), "\n";
####
% stephan@ape (/home/stephan/t0) %
% perl safe_unzip.px sesame safe1.zip
.cmd: [unzip -qoP sesame safe1.zip 2>/dev/null]
**ERROR: unzip failed! status = [9] at safe_unzip.px line 18.