Refactoring - Removed decompress_two_layer_type2()
[deark.git] / scripts / deark-recursive.pl
blob1e6113f0b64f5821a0f82eaca6cc9373e3ea3ed9
1 #!/usr/bin/perl -w
2 # A Perl5 sample script that uses Deark to try to recursively extract all the
3 # files from the files given on the command line.
4 # All extracted files will be written to the current directory, and will have
5 # names beginning with "output.".
6 # It is normal for error messages to be printed, when unsupported formats are
7 # extracted.
8 # This script is quick and dirty. Use at your own risk.
9 # Terms of use: Public domain
10 # By Jason Summers, 2018
11 use strict;
13 my $deark_exe = "/usr/local/bin/deark";
15 sub do_onefile {
16 my $nlistref = $_[0];
17 my $fn = $_[1];
19 my $code = join('.', @$nlistref);
20 print "extracting from: $fn\n";
22 my @args = ($deark_exe, $fn, "-extrlist", "output.list",
23 "-a", "-o", "output.$code");
24 system(@args);
26 if($#$nlistref > 10) {
27 return; # emergency brake
30 # Make a list of the filenames that the previous command extracted.
31 my @outputfns = ();
32 open(my $extrlist, "<", "output.list") or die "Can't read output.list";
33 while(<$extrlist>) {
34 my $line = $_;
35 chomp($line);
36 push @outputfns, $line;
38 close($extrlist);
39 unlink("output.list");
41 # Now we have the list. Call ourselves recursively.
42 my $counter = 0;
43 foreach my $fn (@outputfns) {
44 push @$nlistref, sprintf "%03d", $counter;
45 do_onefile($nlistref, $fn);
46 pop @$nlistref;
47 $counter++;
51 sub main {
52 my @nlist = (); # A stack used to construct output filenames
54 foreach my $fn (@ARGV) {
55 my $fn_sanitized = $fn;
56 if($fn_sanitized =~ /^(.*)[\/\\](.*)$/) { # Only use basename
57 if($2 ne "") {
58 $fn_sanitized = $2;
61 $fn_sanitized =~ s/[\/\\:\*\?\"<>\|\c@-\c_]/_/g;
62 push @nlist, $fn_sanitized;
63 do_onefile(\@nlist, $fn);
64 pop @nlist;
68 main();