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

I´m having trouble with a simple perl script, anyone can tell me what I´m doing wrong?

I get this messsage on WinZip when I try to open the file.
Error in file #1: bad Zip file offset (Error local header signature not found): 0
(attempting to re-compensate)
Extracting darla.jpg
Error: unexpected end of file encountered
Error: invalid compressed data to inflate



#!/usr/local/bin/perl
chdir ("d:\\www\\mypage.net\\www\\test\\");
$zipfile = "zipfile.zip";
print "Content-type:application/x-zip-compressed\n\n";
open FILE, "< $zipfile";
binmode FILE;
print <FILE>;
close FILE;


Tks. Sliva.

Replies are listed 'Best First'.
Re: Get a zip file with script
by graff (Chancellor) on Mar 22, 2004 at 01:55 UTC
    With all due respect, I think you should avoid ysth's guess about the matter -- his advice would obliterate the input file, and you don't want that (unless you have a backup copy somewhere).

    The problem is that you need to do binmode on STDOUT, as well as on FILE (the input file handle):

    #!/usr/local/bin/perl chdir ("d:\\www\\mypage.net\\www\\test\\"); $zipfile = "zipfile.zip"; print "Content-type:application/x-zip-compressed\n\n"; open FILE, "< $zipfile"; binmode FILE; binmode STDOUT; ## add this $/ = undef; ## use slurp mode for input, too print <FILE>; # closing both FILE and STDOUT is automatic at end-of-script
    update: If you're going to be dealing with really big zip files (I'm guessing 2 MB might be a practical limit), you might opt for fixed-length read/write instead of slurp-mode (taking the whole file into memory at once and printing it all at once). In any case, don't rely on the "default" value of $/ (input record separator), since there's no telling what sort of read/write sizes you'll get that way. Set $/ to a reference to a sensible integer value to control the number of bytes read from FILE:

    $/ = \16386 # (d'oh!)

    $/ = \16384; # 16 KB/read while (<FILE>) { print; } # It's not a problem if the last "record" read from FILE is <16 KB.
    (another update: I agree with Jaap about the chdir thing.)
      Wow, cool trick on the reference to do the fixed-length read. I've never seen that before. Just so happens that I'm about to finish this coffee, and get back to a program that needs to do fixed reads -- I'll check it out right now.

      So, great timing, thanks graff...

Re: Get a zip file with script
by Jaap (Curate) on Mar 22, 2004 at 01:05 UTC
    I might be WAY off here, but i think i had a similar problem once and i had to tell the http connection to use binary mode too. Or was that with sockets? I'm sure a much wiser monk will tell us soon ;)

    Oh b.t.w., you probably should't use chdir, unless you know what you're doing. Why not just this:
    #!/usr/local/bin/perl print "Content-type:application/x-zip-compressed\n\n"; open FILE, "d:/www/mypage.net/www/test/zipfile.zip"; binmode FILE; print <FILE>; close FILE;
    Update:
    Turns out i was right ;)
    binmode STDOUT;
Re: Get a zip file with script
by ysth (Canon) on Mar 22, 2004 at 01:14 UTC
    You are opening the FILE with read access. Try: open FILE, "> $zipfile" or die "Couldn't open file: $!" and don't forget to use warnings;

    Update: strike out foolishness

      NO. You missed the part about FILE being the INPUT -- it's not the output. STDOUT is the output.

      Oh well, if Sliva makes the mistake of following your advice, it will be a learning experience...