in reply to extract jpg from binary file

I have a similar script to pull PNG images out of Word documents. There might be edge cases, but its been Good Enough so far:
my $HEADER = "\211PNG"; my $FOOTER = "IEND\xAEB`\x82"; foreach my $file ( @ARGV ) { print "Extracting $file\n"; (my $image_base = $file) =~ s/(.*)\..*/$1/; my $data = do { local $/; open my( $fh ), $file; <$fh> }; my $count = 0; while( $data =~ m/($HEADER.*?$FOOTER)/sg ) { my $image = $1; $count++; my $image_name = "$image_base.$count.png"; open my $fh, "> $image_name" or warn "$image_name: $!", next; print "Writing $image_name: ", length($image), " bytes\n"; print $fh $image; close $fh; } }
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review