Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Missing bytes??

by stevee (Acolyte)
on Apr 06, 2008 at 15:32 UTC ( [id://678641]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Venerable Monks, I am struggling with a corrupt zip archive (another story really!) but to help me in wading through the analysis I have written the following code:
open (FILE, "< i:\\lenova_2008-03-28.zip"); open (OUT, "> i:\\fullhexdump.txt"); binmode(FILE); $count=0; while (<FILE>) { $count++; read(FILE, $buf, 1); $byte = unpack "H*", $buf; print "$byte"; print OUT "$byte "; } close FILE; close OUT; print "\n\n$count\n\n";
Now the zip archive is 5,914,271KB big, but running the above the count only gets to 22,780,549 bytes (giving a text output file of length 66,740KB). Obviously I am not reading the whole file in, but I can not see why. Does any Learned Brother or Sister have any idea?
Thanks
Stevee

Replies are listed 'Best First'.
Re: Missing bytes??
by Corion (Patriarch) on Apr 06, 2008 at 15:36 UTC

    You are reading from the file in two locations:

    while (<FILE>) { ... read(FILE, $buf, 1);

    More likely you want while (! eof FILE) { instead of reading a line from FILE and then reading another byte from FILE.

      or
      while (read(FILE, $buf, 1)) { ... }
      Thanks Corion, trying it now. Sadly I did not realise that while (<FILE>) read the file too!
        interesting, what did you think ?
Re: Missing bytes??
by jwkrahn (Abbot) on Apr 06, 2008 at 18:19 UTC

    You don't have to read only one byte at a time:

    open my $FILE, '<:raw', 'i:/lenova_2008-03-28.zip' or die "Cannot open + 'i:/lenova_2008-03-28.zip' $!"; open my $OUT, '>', 'i:/fullhexdump.txt' or die "Cannot open 'i:/fullhe +xdump.txt' $!"; $/ = \2048; my $count; while ( <$FILE> ) { $count += length; print $OUT unpack 'H*', $_; } close $FILE; close $OUT; print "\n\n$count\n\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://678641]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found