Jump to content


Photo

PECL ZIP extension module: line endings affect .zip format validity


  • Please log in to reply
3 replies to this topic

#1 e11j4y

e11j4y
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 October 2006 - 11:54 AM

On Windows, the PECL ZIP extension module (ZipArchive class) appears to create invalid .zip archives when compressing files that contain '\n' line endings.

Doing something simple like this:

$zip = new ZipArchive();
$filename = "./test.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)===TRUE) {
  $zip->addFile("test.txt","text.txt");
  $zip->close();
}

where test.txt contains '\n' line endings results in a .zip archive that isn't valid, ie it cannot be opened using tools like WinZip. However, when test.txt contains '\r\n' line endings, the archive created is valid and can be opened without error. If the files to be archived are mixed -- some with '\n' endings and some with '\r\n' -- the resulting archive can be opened, however those files with '\r\n' endgins can't be extracted.

Has anyone else observed this? Any suggestions for a workaround (apart from modifying the line endings of the files to be archived)? There seems to be a similar bug here http://pecl.php.net/...bug.php?id=8711, however I'm not sure it's quite the same problem.

I've tried:
. setting auto_detect_line_endings to 'On'.
. using ZIPARCHIVE::OVERWRITE instead of ZIPARCHIVE::CREATE.

This is on:
- Zip version 2.0.0, Libzip version 0.7.1
- PHP 5.2.0 (Built Oct 03, 2006)
- Apache 2.0.52
- Windows XP Pro

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 03 October 2006 - 12:58 PM

I don't see any other way of doing it. To me this is short coming of PHP, not the script. In other languages like Perl, line endings are always internally handled, even using the PHP.INI setting does not always get it right. This only happens on the windows version of PHP, I've not seen it happen once on Linux. There is a patch that you can apply to make all IO functions follow Perl's way of handling line endings on windows, which is pretty nifty. So that you understand the diferance between Perl and PHP here is a simple example write example!

// perl, write Unix / Linux style line endings on Windows

open DAT, '>perl.txt';
for ( $i = 0; $i < 50; $i++ )
{
	print DAT $i . "\n";
}
close DAT;

// php

<?

$io = fopen ( 'php.txt', 'w' );

for ( $i = 0; $i < 50; $i++ )
{
	fputs ( $io, $i . "\n" );
}

fclose ( $io );

?>


After, open each file in notepad, you will see PHP does not convert line ending to \r\n, while Perl does, it's done in a way that makes it read as \r\n on windows, but still be \n on Unix / Linux.


me!

#3 e11j4y

e11j4y
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 October 2006 - 02:21 PM

Just to make sure I understand, are you confirming that there's no way to archive files that contain \n line endings on Windows using PECL Zip without first modifying the line endings to \r\n?

Thanks.

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 03 October 2006 - 03:13 PM

yes, no way!

me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users