jujiro_eb has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Need your help again.

I am trying to decode a large mime encoded file using MIME::Base64. I am not getting the original file back. The file is getting corrupted when decoded. I am using the following code.

use strict; my $file_name=$ARGV[$0]; my $buf; use MIME::Base64; open(FILE, $file_name); while ($buf = <FILE>) { print MIME::Base64::decode($buf); }
Can you please give me some pointers. Loading the entire file into a string variable and decoding it is out of question here because I have limited memory on this notebook.

As usual, thanks so much for the help.

Ash

Replies are listed 'Best First'.
Re: Help: Mime decoding of a large file
by Gangabass (Vicar) on Apr 16, 2009 at 06:00 UTC

    How are you checking decoded file?

    May be you're under Windows and need to use binmode?

      Hello Gangabass,

      Yes, I am on Windows platform. What is binmode? I did not see any documentation in the Mime package about Binmode. Can you please elaborate.

      Thanks.

      Ash

        Read: perldoc -f binmode

        Oversimplified explanation: Win will puke trying to cope with binary data unless suitably instructed (binmode) that said data includes (most) non-ASCII chars.

Re: Help: Mime decoding of a large file
by almut (Canon) on Apr 16, 2009 at 03:03 UTC

    It should work just fine to decode the file line by line.  "Proof":

    #!/usr/bin/perl use MIME::Base64 (); my $file_name = shift @ARGV; my $buf; open my $fh_in, "<", $file_name or die $!; open my $fh_out, ">", $file_name.".base64" or die $!; while (read($fh_in, $buf, 100*57)) { print $fh_out MIME::Base64::encode($buf); } close $fh_out; open my $fh_base64, "<", $file_name.".base64" or die $!; open my $fh_decoded, ">", $file_name.".decoded" or die $!; while ($buf = <$fh_base64>) { print $fh_decoded MIME::Base64::decode($buf); } __END__ $ ./757820.pl hugefile $ ls -l hugefile* -rw-r--r-- 1 almut almut 618119168 2009-02-07 02:01 hugefile -rw-r--r-- 1 almut almut 835003088 2009-04-16 04:51 hugefile.base64 -rw-r--r-- 1 almut almut 618119168 2009-04-16 04:51 hugefile.decoded $ cmp hugefile hugefile.decoded $

    Do you maybe have some junk around the actual base64 data section in your encoded file, or is it maybe additionally transfer-encoded one way or another (e.g. quoted-printable)?