Problem in Archive::Zip
Rajeshk
created: 2005-11-03 23:17:48

Dear Monks,

I faced a problem like 'Unexpected end of archive' when i open a Zipped file 'Test.zip'.

If i comment the line
#local $\ = "\n";
I get a perfect Result. If anyone can help me.

#Updated Script 
local $\ = "\n";
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
print "File Archive Started";
my $zip = Archive::Zip->new();
$member = $zip->addFile( 'Test.txt');
die 'write error' unless $zip->writeToFileNamed( 'Test.zip' ) == AZ_OK;

print "File Archive Competed";

Thanks,

Rajesh.K

Edited by [planetscape] - [id://17558|formatting]

Re: Problem in Archive::Zip
BUU
created: 2005-11-03 23:56:38
What exactly is the problem? If you want to know why $/ is messing with you, a quick trip to the perldoc provides the answer:
The output record separator for the print operator. Ordinarily the print operator simply prints out its arguments as is, with no trailing newline or other end-of-record string added. To get behavior more like awk, set this variable as you would set awk's ORS variable to specify what is printed at the end of the print. (Mnemonic: you set "$\" instead of adding "\n" at the end of the print. Also, it's just like $/, but it's what you get "back" from Perl.)
Which means that when Archive::Zip calls 'print' to output a chunk of it's generated binary data, perl appends a '\n' to the end of every chunk. Thus corrupting the archive.
Re: Problem in Archive::Zip
created: 2005-11-04 00:26:24
Hi Rajesh,
why are you using the local $\ = "\n";
I think it is not necessary.

Regards
Franklin.
Re: Problem in Archive::Zip
created: 2005-11-04 00:37:39
[BUU]'s right - don't do that. Do you mind saying what influenced you to change the default value of $\ in the first place? Have you been doing it in order to save a bit of typing when sending data to STDOUT? In other words, do you have a coding style similar to:
   sub display {
      local $\ = "\n";
      print "This is the first line. Look ma, no newline";
      print "This is the second line.";
   }
In a brief [Chatterbox] discussion on this point, [Zaxo] made the comment that [cpan://Archive::Zip] could avoid this type of problem by localizing $\ itself.
Re: Problem in Archive::Zip
created: 2005-11-04 05:25:43
Thanks BUU, I understand the problem.

Regards,
Rajesh.K

perlmonks.org content © perlmonks.org and BUU, l.frankline, Rajeshk, virtualsue

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03