mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-06 07:27:23 +02:00
Improved Windows compatibility, fix #54
This commit is contained in:
@@ -40,7 +40,7 @@ class DummyFileSystemStream
|
||||
public function stream_open($path, $mode, $options, &$opened_path)
|
||||
{
|
||||
$parsedUrl = parse_url($path);
|
||||
$uri = str_replace('//', '/', $parsedUrl['path']);
|
||||
$uri = substr($parsedUrl['path'], 1);
|
||||
$this->fp = @fopen($uri, $mode);
|
||||
|
||||
return $this->fp !== false;
|
||||
|
@@ -76,7 +76,7 @@ class Zip64Test extends ZipTestCase
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
if (!is_dir($this->outputDirname)) {
|
||||
mkdir($this->outputDirname, 0755, true);
|
||||
static::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
}
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
|
@@ -12,22 +12,8 @@ use Symfony\Component\Finder\Finder;
|
||||
*
|
||||
* @small
|
||||
*/
|
||||
final class SymlinkTest extends ZipFileTest
|
||||
final class SymlinkTest extends ZipTestCase
|
||||
{
|
||||
/**
|
||||
* This method is called before the first test of this test class is run.
|
||||
*/
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
parent::setUpBeforeClass();
|
||||
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
self::markTestSkipped('only linux test');
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideAllowSymlink
|
||||
*
|
||||
@@ -37,6 +23,10 @@ final class SymlinkTest extends ZipFileTest
|
||||
*/
|
||||
public function testSymlink($allowSymlink)
|
||||
{
|
||||
if (self::skipTestForWindows()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_dir($this->outputDirname)) {
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
}
|
||||
|
@@ -75,7 +75,7 @@ abstract class ZipFileSetTestCase extends ZipTestCase
|
||||
$zipEntryName = $localPath . $file;
|
||||
|
||||
if (isset($actualResultFiles[$file])) {
|
||||
static::assertTrue(isset($zipFile[$zipEntryName]));
|
||||
static::assertTrue(isset($zipFile[$zipEntryName]), 'Not found entry name ' . $zipEntryName);
|
||||
static::assertSame(
|
||||
$zipFile[$zipEntryName],
|
||||
$content,
|
||||
|
@@ -34,7 +34,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$this->setExpectedException(ZipException::class, 'does not exist');
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFile(uniqid('', true));
|
||||
$zipFile->openFile(uniqid('', false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,12 +42,16 @@ class ZipFileTest extends ZipTestCase
|
||||
*/
|
||||
public function testOpenFileCantOpen()
|
||||
{
|
||||
$this->setExpectedException(ZipException::class, 'can\'t open');
|
||||
if (static::skipTestForWindows()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (static::skipTestForRootUser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setExpectedException(ZipException::class, 'can\'t open');
|
||||
|
||||
static::assertNotFalse(file_put_contents($this->outputFilename, 'content'));
|
||||
static::assertTrue(chmod($this->outputFilename, 0222));
|
||||
|
||||
@@ -1031,7 +1035,12 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->extractTo($this->outputDirname, null, [], $extractedEntries);
|
||||
|
||||
foreach ($entries as $entryName => $contents) {
|
||||
$fullExtractedFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $entryName;
|
||||
$name = $entryName;
|
||||
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
$name = str_replace('/', '\\', $name);
|
||||
}
|
||||
$fullExtractedFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $name;
|
||||
|
||||
static::assertTrue(
|
||||
isset($extractedEntries[$fullExtractedFilename]),
|
||||
@@ -1393,14 +1402,18 @@ class ZipFileTest extends ZipTestCase
|
||||
/**
|
||||
* @throws ZipException
|
||||
*/
|
||||
public function testAddFileCantOpen()
|
||||
public function testAddFileCannotOpen()
|
||||
{
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'is not readable');
|
||||
if (static::skipTestForWindows()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (static::skipTestForRootUser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'is not readable');
|
||||
|
||||
static::assertNotFalse(file_put_contents($this->outputFilename, ''));
|
||||
static::assertTrue(chmod($this->outputFilename, 0244));
|
||||
|
||||
@@ -1438,7 +1451,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'does not exist');
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDir(uniqid('', true));
|
||||
$zipFile->addDir(uniqid('', false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1471,7 +1484,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'does not exist');
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDirRecursive(uniqid('', true));
|
||||
$zipFile->addDirRecursive(uniqid('', false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1711,7 +1724,9 @@ class ZipFileTest extends ZipTestCase
|
||||
*/
|
||||
public function testSaveAsFileNotWritable()
|
||||
{
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'can not open from write');
|
||||
if (static::skipTestForWindows()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (static::skipTestForRootUser()) {
|
||||
return;
|
||||
@@ -1722,6 +1737,8 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
$this->outputFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . basename($this->outputFilename);
|
||||
|
||||
$this->setExpectedExceptionRegExp(InvalidArgumentException::class, '~Cannot open ".*?" for writing.~');
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
}
|
||||
@@ -1877,7 +1894,7 @@ class ZipFileTest extends ZipTestCase
|
||||
*/
|
||||
public function testAddEmptyDirNullName()
|
||||
{
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'Dir name is null');
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'Entry name is null');
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addEmptyDir(null);
|
||||
@@ -1888,7 +1905,7 @@ class ZipFileTest extends ZipTestCase
|
||||
*/
|
||||
public function testAddEmptyDirEmptyName()
|
||||
{
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'Empty dir name');
|
||||
$this->setExpectedException(InvalidArgumentException::class, 'Empty entry name');
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addEmptyDir('');
|
||||
@@ -1947,12 +1964,21 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['file'] = 'content';
|
||||
$zipFile['file2'] = 'content2';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFromString(file_get_contents($this->outputFilename));
|
||||
$zipFile['file2'] = 'content 2';
|
||||
$zipFile->rewrite();
|
||||
static::assertSame(\count($zipFile), 2);
|
||||
static::assertTrue(isset($zipFile['file']));
|
||||
static::assertTrue(isset($zipFile['file2']));
|
||||
$zipFile['file3'] = 'content3';
|
||||
$zipFile = $zipFile->rewrite();
|
||||
static::assertSame(\count($zipFile), 3);
|
||||
static::assertTrue(isset($zipFile['file']));
|
||||
static::assertTrue(isset($zipFile['file2']));
|
||||
static::assertTrue(isset($zipFile['file3']));
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1966,6 +1992,87 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->rewrite();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the ability to overwrite an open zip file with a relative path.
|
||||
*
|
||||
* @throws ZipException
|
||||
*/
|
||||
public function testRewriteRelativeFile()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['entry.txt'] = 'test';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$outputDirname = \dirname($this->outputFilename);
|
||||
static::assertTrue(chdir($outputDirname));
|
||||
|
||||
$relativeFilename = basename($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile['entry2.txt'] = 'test';
|
||||
$zipFile->saveAsFile($relativeFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the ability to overwrite an open zip file with a relative path.
|
||||
*
|
||||
* @throws ZipException
|
||||
*/
|
||||
public function testRewriteDifferentWinDirectorySeparator()
|
||||
{
|
||||
if (\DIRECTORY_SEPARATOR !== '\\') {
|
||||
static::markTestSkipped('Windows test only');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['entry.txt'] = 'test';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$alternativeOutputFilename = str_replace('\\', '/', $this->outputFilename);
|
||||
self::assertCorrectZipArchive($alternativeOutputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile['entry2.txt'] = 'test';
|
||||
$zipFile->saveAsFile($alternativeOutputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($alternativeOutputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
static::assertCount(2, $zipFile);
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipException
|
||||
*/
|
||||
public function testRewriteRelativeFile2()
|
||||
{
|
||||
$this->outputFilename = basename($this->outputFilename);
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['entry.txt'] = 'test';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$absoluteOutputFilename = getcwd() . \DIRECTORY_SEPARATOR . $this->outputFilename;
|
||||
self::assertCorrectZipArchive($absoluteOutputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile['entry2.txt'] = 'test';
|
||||
$zipFile->saveAsFile($absoluteOutputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($absoluteOutputFilename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ZipException
|
||||
*/
|
||||
|
@@ -77,7 +77,8 @@ class ZipPasswordTest extends ZipFileSetTestCase
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
static::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
/** @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/ WinZip 99-character limit */
|
||||
static::assertCorrectZipArchive($this->outputFilename, substr($password, 0, 99));
|
||||
|
||||
// check from WinZip AES encryption
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
@@ -137,7 +138,7 @@ class ZipPasswordTest extends ZipFileSetTestCase
|
||||
);
|
||||
}
|
||||
|
||||
$password = base64_encode(random_bytes(50));
|
||||
$password = md5(random_bytes(50));
|
||||
|
||||
$zip = new ZipFile();
|
||||
$zip->addDirRecursive($this->outputDirname);
|
||||
|
@@ -29,8 +29,8 @@ class ZipStreamOpenTest extends TestCase
|
||||
*/
|
||||
public function testOpenStream($resource, $exceptionClass = null, $exceptionMessage = null)
|
||||
{
|
||||
if ($resource === null) {
|
||||
static::markTestSkipped('skip null resource');
|
||||
if ($resource === null || $resource === false) {
|
||||
static::markTestSkipped('skip resource');
|
||||
|
||||
return;
|
||||
}
|
||||
|
@@ -24,14 +24,14 @@ abstract class ZipTestCase extends TestCase
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$id = uniqid('phpzip', true);
|
||||
$tempDir = sys_get_temp_dir() . '/phpunit-phpzip';
|
||||
$id = uniqid('phpzip', false);
|
||||
$tempDir = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'phpunit-phpzip';
|
||||
|
||||
if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) {
|
||||
throw new \RuntimeException('Dir ' . $tempDir . " can't created");
|
||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempDir));
|
||||
}
|
||||
$this->outputFilename = $tempDir . '/' . $id . '.zip';
|
||||
$this->outputDirname = $tempDir . '/' . $id;
|
||||
$this->outputFilename = $tempDir . \DIRECTORY_SEPARATOR . $id . '.zip';
|
||||
$this->outputDirname = $tempDir . \DIRECTORY_SEPARATOR . $id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,64 +58,91 @@ abstract class ZipTestCase extends TestCase
|
||||
*/
|
||||
public static function assertCorrectZipArchive($filename, $password = null)
|
||||
{
|
||||
if (self::existsProgram('unzip')) {
|
||||
$command = 'unzip';
|
||||
|
||||
if ($password !== null) {
|
||||
$command .= ' -P ' . escapeshellarg($password);
|
||||
}
|
||||
$command .= ' -t ' . escapeshellarg($filename);
|
||||
$command .= ' 2>&1';
|
||||
exec($command, $output, $returnCode);
|
||||
|
||||
$output = implode(\PHP_EOL, $output);
|
||||
|
||||
if ($password !== null && $returnCode === 81) {
|
||||
if (self::existsProgram('7z')) {
|
||||
/**
|
||||
* WinZip 99-character limit.
|
||||
*
|
||||
* @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
*/
|
||||
$password = substr($password, 0, 99);
|
||||
|
||||
$command = '7z t -p' . escapeshellarg($password) . ' ' . escapeshellarg($filename);
|
||||
exec($command, $output, $returnCode);
|
||||
/**
|
||||
* @var array $output
|
||||
*/
|
||||
$output = implode(\PHP_EOL, $output);
|
||||
|
||||
static::assertSame($returnCode, 0);
|
||||
static::assertNotContains(' Errors', $output);
|
||||
static::assertContains(' Ok', $output);
|
||||
} else {
|
||||
fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
|
||||
fwrite(\STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . \PHP_EOL);
|
||||
}
|
||||
} else {
|
||||
static::assertSame($returnCode, 0, $output);
|
||||
static::assertNotContains('incorrect password', $output);
|
||||
static::assertContains(' OK', $output);
|
||||
static::assertContains('No errors', $output);
|
||||
}
|
||||
if (self::existsProgram('7z')) {
|
||||
self::assertCorrectZipArchiveFrom7z($filename, $password);
|
||||
} elseif (self::existsProgram('unzip')) {
|
||||
self::assertCorrectZipArchiveFromUnzip($filename, $password);
|
||||
} else {
|
||||
fwrite(\STDERR, 'Skipped testing the zip archive for errors using third-party utilities.' . \PHP_EOL);
|
||||
fwrite(\STDERR, 'To fix this, install 7-zip or unzip.' . \PHP_EOL);
|
||||
fwrite(\STDERR, \PHP_EOL);
|
||||
fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install p7zip-full unzip' . \PHP_EOL);
|
||||
fwrite(\STDERR, \PHP_EOL);
|
||||
fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
|
||||
fwrite(\STDERR, ' * 7-zip - https://www.7-zip.org/download.html' . \PHP_EOL);
|
||||
fwrite(\STDERR, ' * unzip - http://gnuwin32.sourceforge.net/packages/unzip.htm' . \PHP_EOL);
|
||||
fwrite(\STDERR, \PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param string|null $password
|
||||
*/
|
||||
private static function assertCorrectZipArchiveFrom7z($filename, $password = null)
|
||||
{
|
||||
$command = '7z t';
|
||||
|
||||
if ($password !== null) {
|
||||
$command .= ' -p' . escapeshellarg($password);
|
||||
}
|
||||
$command .= ' ' . escapeshellarg($filename) . ' 2>&1';
|
||||
|
||||
exec($command, $output, $returnCode);
|
||||
$output = implode(\PHP_EOL, $output);
|
||||
|
||||
static::assertSame($returnCode, 0);
|
||||
static::assertNotContains(' Errors', $output);
|
||||
static::assertContains(' Ok', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param string|null $password
|
||||
*/
|
||||
private static function assertCorrectZipArchiveFromUnzip($filename, $password = null)
|
||||
{
|
||||
$command = 'unzip';
|
||||
|
||||
if ($password !== null) {
|
||||
$command .= ' -P ' . escapeshellarg($password);
|
||||
}
|
||||
$command .= ' -t ' . escapeshellarg($filename) . ' 2>&1';
|
||||
|
||||
exec($command, $output, $returnCode);
|
||||
$output = implode(\PHP_EOL, $output);
|
||||
|
||||
if ($password !== null && $returnCode === 81) {
|
||||
fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
|
||||
fwrite(\STDERR, 'You have to install 7-zip to complete this test.' . \PHP_EOL);
|
||||
fwrite(\STDERR, 'Install 7-Zip on Ubuntu: sudo apt-get install p7zip-full' . \PHP_EOL);
|
||||
fwrite(\STDERR, 'Install 7-Zip on Windows: https://www.7-zip.org/download.html' . \PHP_EOL);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static::assertSame($returnCode, 0, $output);
|
||||
static::assertNotContains('incorrect password', $output);
|
||||
static::assertContains(' OK', $output);
|
||||
static::assertContains('No errors', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $program
|
||||
* @param array $successCodes
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function existsProgram($program)
|
||||
protected static function existsProgram($program, array $successCodes = [0])
|
||||
{
|
||||
if (\DIRECTORY_SEPARATOR !== '\\') {
|
||||
exec('which ' . escapeshellarg($program), $output, $returnCode);
|
||||
$command = \DIRECTORY_SEPARATOR === '\\' ?
|
||||
escapeshellarg($program) :
|
||||
'which ' . escapeshellarg($program);
|
||||
$command .= ' 2>&1';
|
||||
|
||||
return $returnCode === 0;
|
||||
}
|
||||
// false for Windows
|
||||
return false;
|
||||
exec($command, $output, $returnCode);
|
||||
|
||||
return \in_array($returnCode, $successCodes, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,7 +171,7 @@ abstract class ZipTestCase extends TestCase
|
||||
*/
|
||||
public static function assertVerifyZipAlign($filename, $showErrors = false)
|
||||
{
|
||||
if (self::existsProgram('zipalign')) {
|
||||
if (self::existsProgram('zipalign', [0, 2])) {
|
||||
exec('zipalign -c -v 4 ' . escapeshellarg($filename), $output, $returnCode);
|
||||
|
||||
if ($showErrors && $returnCode !== 0) {
|
||||
@@ -155,6 +182,14 @@ abstract class ZipTestCase extends TestCase
|
||||
}
|
||||
|
||||
fwrite(\STDERR, "Cannot find the program 'zipalign' for the test" . \PHP_EOL);
|
||||
fwrite(\STDERR, 'To fix this, install zipalign.' . \PHP_EOL);
|
||||
fwrite(\STDERR, \PHP_EOL);
|
||||
fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install zipalign' . \PHP_EOL);
|
||||
fwrite(\STDERR, \PHP_EOL);
|
||||
fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
|
||||
fwrite(\STDERR, ' 1. Install Android Studio' . \PHP_EOL);
|
||||
fwrite(\STDERR, ' 2. Install Android Sdk' . \PHP_EOL);
|
||||
fwrite(\STDERR, ' 3. Add zipalign path to \$Path' . \PHP_EOL);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -173,4 +208,18 @@ abstract class ZipTestCase extends TestCase
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function skipTestForWindows()
|
||||
{
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
static::markTestSkipped('Skip on Windows');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user