Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP ZipArchive::extractTo() truncates path segments ending in `.`
<?php
$filename = 'test-archive.zip';
if (is_readable('test/test.txt')) {
unlink('test/test.txt');
}
if (is_readable('test./test.txt')) {
unlink('test./test.txt');
}
if (is_readable('.test/test.txt')) {
unlink('.test/test.txt');
}
if (is_readable('test')) {
rmdir('test');
}
mkdir('test');
if (is_readable('test.')) {
rmdir('test.');
}
mkdir('test.');
if (is_readable('.test')) {
rmdir('.test');
}
mkdir('.test');
if (is_readable('test-extract')) {
if (is_readable('test-extract/test.txt')) {
unlink('test-extract/test.txt');
}
if (is_readable('test-extract/test/test.txt')) {
unlink('test-extract/test/test.txt');
rmdir('test-extract/test');
}
if (is_readable('test-extract/.test/test.txt')) {
unlink('test-extract/.test/test.txt');
rmdir('test-extract/.test');
}
if (is_readable('test-extract/test./test.txt')) {
unlink('test-extract/test./test.txt');
rmdir('test-extract/test.');
}
rmdir('test-extract');
}
mkdir('test-extract');
file_put_contents('test/test.txt', 'This is just a text file.');
file_put_contents('test./test.txt', 'This is just a text file.');
file_put_contents('.test/test.txt', 'This is just a text file.');
if (is_readable($filename)) {
unlink($filename);
}
$archive = new ZipArchive();
$archive->open($filename, ZipArchive::CREATE|ZipArchive::OVERWRITE);
$archive->addFile('test/test.txt');
$archive->addFile('test./test.txt');
$archive->addFile('.test/test.txt');
$archive->close();
$archive = new ZipArchive();
$archive->open($filename);
$error = $archive->open($filename);
if ($error !== true) {
var_dump($error);
} else {
echo "Opened archive {$filename} successfully\n";
}
for ($i = 0; $i < $archive->numFiles; $i++) {
echo $archive->getNameIndex($i) . "\n";
}
$result = $archive->extractTo('test-extract/');
if (!file_exists('test-extract/test./test.txt')) {
echo "Extract failure: expected file to exist at test-extract/test./text.txt\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.