1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-02 13:37:25 +02:00

Merge branch 'master' into develop

# Conflicts:
#	src/PhpZip/ZipFile.php
#	tests/PhpZip/ZipFileTest.php
This commit is contained in:
Ne-Lexa
2018-10-11 10:39:05 +03:00
2 changed files with 52 additions and 45 deletions

View File

@@ -69,7 +69,6 @@ class ZipFile implements ZipFileInterface
* @var ZipInputStreamInterface Input seekable input stream. * @var ZipInputStreamInterface Input seekable input stream.
*/ */
protected $inputStream; protected $inputStream;
/** /**
* @var ZipModel * @var ZipModel
*/ */
@@ -93,7 +92,7 @@ class ZipFile implements ZipFileInterface
public function openFile($filename) public function openFile($filename)
{ {
if (!file_exists($filename)) { if (!file_exists($filename)) {
throw new ZipException("File $filename can't exists."); throw new InvalidArgumentException("File $filename does not exist.");
} }
if (!($handle = @fopen($filename, 'rb'))) { if (!($handle = @fopen($filename, 'rb'))) {
throw new ZipException("File $filename can't open."); throw new ZipException("File $filename can't open.");
@@ -560,10 +559,10 @@ class ZipFile implements ZipFileInterface
} }
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if (strlen($inputDir) === 0) { if (strlen($inputDir) === 0) {
throw new InvalidArgumentException('Input dir empty'); throw new InvalidArgumentException('The input directory is not specified');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
throw new ZipException('Directory ' . $inputDir . ' can\'t exists'); throw new InvalidArgumentException(sprintf('The "%s" directory does not exist.', $inputDir));
} }
$inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR;
@@ -587,12 +586,15 @@ class ZipFile implements ZipFileInterface
*/ */
public function addDirRecursive($inputDir, $localPath = "/", $compressionMethod = null) public function addDirRecursive($inputDir, $localPath = "/", $compressionMethod = null)
{ {
if ($inputDir === null) {
throw new InvalidArgumentException('Input dir is null');
}
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if (null === $inputDir || strlen($inputDir) === 0) { if (strlen($inputDir) === 0) {
throw new InvalidArgumentException('Input dir empty'); throw new InvalidArgumentException('The input directory is not specified');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
throw new InvalidArgumentException('Directory ' . $inputDir . ' can\'t exists'); throw new InvalidArgumentException(sprintf('The "%s" directory does not exist.', $inputDir));
} }
$inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR;
@@ -703,16 +705,19 @@ class ZipFile implements ZipFileInterface
$recursive = true, $recursive = true,
$compressionMethod = null $compressionMethod = null
) { ) {
if ($inputDir === null) {
throw new InvalidArgumentException('Input dir is null');
}
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if (strlen($inputDir) === 0) { if (strlen($inputDir) === 0) {
throw new InvalidArgumentException('Input dir empty'); throw new InvalidArgumentException('The input directory is not specified');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
throw new ZipException('Directory ' . $inputDir . ' can\'t exists'); throw new InvalidArgumentException(sprintf('The "%s" directory does not exist.', $inputDir));
} }
$globPattern = (string)$globPattern; $globPattern = (string)$globPattern;
if (empty($globPattern)) { if (empty($globPattern)) {
throw new InvalidArgumentException("glob pattern empty"); throw new InvalidArgumentException('The glob pattern is not specified');
} }
$inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR;
@@ -801,14 +806,14 @@ class ZipFile implements ZipFileInterface
) { ) {
$regexPattern = (string)$regexPattern; $regexPattern = (string)$regexPattern;
if (empty($regexPattern)) { if (empty($regexPattern)) {
throw new InvalidArgumentException("regex pattern empty"); throw new InvalidArgumentException('The regex pattern is not specified');
} }
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if (strlen($inputDir) === 0) { if (strlen($inputDir) === 0) {
throw new InvalidArgumentException('Input dir empty'); throw new InvalidArgumentException('The input directory is not specified');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
throw new InvalidArgumentException('Directory ' . $inputDir . ' can\'t exists'); throw new InvalidArgumentException(sprintf('The "%s" directory does not exist.', $inputDir));
} }
$inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR;
@@ -876,7 +881,6 @@ class ZipFile implements ZipFileInterface
* @param string $oldName Old entry name. * @param string $oldName Old entry name.
* @param string $newName New entry name. * @param string $newName New entry name.
* @return ZipFileInterface * @return ZipFileInterface
*
* @throws ZipException * @throws ZipException
*/ */
public function rename($oldName, $newName) public function rename($oldName, $newName)
@@ -918,7 +922,7 @@ class ZipFile implements ZipFileInterface
public function deleteFromGlob($globPattern) public function deleteFromGlob($globPattern)
{ {
if ($globPattern === null || !is_string($globPattern) || empty($globPattern)) { if ($globPattern === null || !is_string($globPattern) || empty($globPattern)) {
throw new InvalidArgumentException("Glob pattern is empty"); throw new InvalidArgumentException("The glob pattern is not specified");
} }
$globPattern = '~' . FilesUtil::convertGlobToRegEx($globPattern) . '~si'; $globPattern = '~' . FilesUtil::convertGlobToRegEx($globPattern) . '~si';
$this->deleteFromRegex($globPattern); $this->deleteFromRegex($globPattern);
@@ -934,7 +938,7 @@ class ZipFile implements ZipFileInterface
public function deleteFromRegex($regexPattern) public function deleteFromRegex($regexPattern)
{ {
if ($regexPattern === null || !is_string($regexPattern) || empty($regexPattern)) { if ($regexPattern === null || !is_string($regexPattern) || empty($regexPattern)) {
throw new InvalidArgumentException("Regex pattern is empty."); throw new InvalidArgumentException("The regex pattern is not specified");
} }
$this->matcher()->match($regexPattern)->delete(); $this->matcher()->match($regexPattern)->delete();
return $this; return $this;

View File

@@ -19,7 +19,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\ZipException * @expectedException \PhpZip\Exception\ZipException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
*/ */
public function testOpenFileCantExists() public function testOpenFileCantExists()
{ {
@@ -1292,7 +1292,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir is null * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddDirNullDirname() public function testAddDirNullDirname()
@@ -1303,7 +1303,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddDirEmptyDirname() public function testAddDirEmptyDirname()
@@ -1314,7 +1314,8 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\ZipException * @expectedException \PhpZip\Exception\ZipException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
* @throws ZipException
*/ */
public function testAddDirCantExists() public function testAddDirCantExists()
{ {
@@ -1324,7 +1325,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddDirRecursiveNullDirname() public function testAddDirRecursiveNullDirname()
@@ -1335,7 +1336,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddDirRecursiveEmptyDirname() public function testAddDirRecursiveEmptyDirname()
@@ -1346,7 +1347,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
* @throws ZipException * @throws ZipException
*/ */
public function testAddDirRecursiveCantExists() public function testAddDirRecursiveCantExists()
@@ -1357,7 +1358,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobNull() public function testAddFilesFromGlobNull()
@@ -1368,7 +1369,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobEmpty() public function testAddFilesFromGlobEmpty()
@@ -1378,8 +1379,9 @@ class ZipFileTest extends ZipTestCase
} }
/** /**
* @expectedException \PhpZip\Exception\ZipException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
* @throws ZipException
*/ */
public function testAddFilesFromGlobCantExists() public function testAddFilesFromGlobCantExists()
{ {
@@ -1389,7 +1391,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage glob pattern empty * @expectedExceptionMessage The glob pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobNullPattern() public function testAddFilesFromGlobNullPattern()
@@ -1400,7 +1402,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage glob pattern empty * @expectedExceptionMessage The glob pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobEmptyPattern() public function testAddFilesFromGlobEmptyPattern()
@@ -1411,7 +1413,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobRecursiveNull() public function testAddFilesFromGlobRecursiveNull()
@@ -1422,7 +1424,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobRecursiveEmpty() public function testAddFilesFromGlobRecursiveEmpty()
@@ -1432,8 +1434,8 @@ class ZipFileTest extends ZipTestCase
} }
/** /**
* @expectedException \PhpZip\Exception\ZipException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobRecursiveCantExists() public function testAddFilesFromGlobRecursiveCantExists()
@@ -1444,7 +1446,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage glob pattern empty * @expectedExceptionMessage The glob pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobRecursiveNullPattern() public function testAddFilesFromGlobRecursiveNullPattern()
@@ -1455,7 +1457,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage glob pattern empty * @expectedExceptionMessage The glob pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromGlobRecursiveEmptyPattern() public function testAddFilesFromGlobRecursiveEmptyPattern()
@@ -1466,7 +1468,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexDirectoryNull() public function testAddFilesFromRegexDirectoryNull()
@@ -1477,7 +1479,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexDirectoryEmpty() public function testAddFilesFromRegexDirectoryEmpty()
@@ -1488,7 +1490,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexCantExists() public function testAddFilesFromRegexCantExists()
@@ -1499,7 +1501,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage regex pattern empty * @expectedExceptionMessage The regex pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexNullPattern() public function testAddFilesFromRegexNullPattern()
@@ -1510,7 +1512,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage regex pattern empty * @expectedExceptionMessage The regex pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexEmptyPattern() public function testAddFilesFromRegexEmptyPattern()
@@ -1521,7 +1523,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexRecursiveDirectoryNull() public function testAddFilesFromRegexRecursiveDirectoryNull()
@@ -1532,7 +1534,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage Input dir empty * @expectedExceptionMessage The input directory is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexRecursiveEmpty() public function testAddFilesFromRegexRecursiveEmpty()
@@ -1543,7 +1545,8 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\ZipException * @expectedException \PhpZip\Exception\ZipException
* @expectedExceptionMessage can't exists * @expectedExceptionMessage does not exist
* @throws ZipException
*/ */
public function testAddFilesFromRegexRecursiveCantExists() public function testAddFilesFromRegexRecursiveCantExists()
{ {
@@ -1553,7 +1556,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage regex pattern empty * @expectedExceptionMessage The regex pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexRecursiveNullPattern() public function testAddFilesFromRegexRecursiveNullPattern()
@@ -1564,7 +1567,7 @@ class ZipFileTest extends ZipTestCase
/** /**
* @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedException \PhpZip\Exception\InvalidArgumentException
* @expectedExceptionMessage regex pattern empty * @expectedExceptionMessage The regex pattern is not specified
* @throws ZipException * @throws ZipException
*/ */
public function testAddFilesFromRegexRecursiveEmptyPattern() public function testAddFilesFromRegexRecursiveEmptyPattern()