1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-02 21:47:23 +02:00

fix duplicates

This commit is contained in:
wapplay
2019-12-07 19:40:36 +03:00
parent a20e9e054d
commit f82a57e662
8 changed files with 91 additions and 80 deletions

View File

@@ -366,9 +366,9 @@ class TraditionalPkwareEncryptionEngine implements ZipEncryptionEngine
$headerBytes = array_values(unpack('C*', substr($content, 0, self::STD_DEC_HDR_SIZE))); $headerBytes = array_values(unpack('C*', substr($content, 0, self::STD_DEC_HDR_SIZE)));
$byte = 0; $byte = 0;
foreach ($headerBytes as &$byte) { for ($i = 0; $i < self::STD_DEC_HDR_SIZE; $i++) {
$byte = ($byte ^ $this->decryptByte()) & 0xff; $headerBytes[$i] = ($headerBytes[$i] ^ $this->decryptByte()) & 0xff;
$this->updateKeys($byte); $this->updateKeys($headerBytes[$i]);
} }
if ($this->entry->getGeneralPurposeBitFlag(ZipEntry::GPBF_DATA_DESCRIPTOR)) { if ($this->entry->getGeneralPurposeBitFlag(ZipEntry::GPBF_DATA_DESCRIPTOR)) {

View File

@@ -187,6 +187,7 @@ abstract class ZipAbstractEntry implements ZipEntry
* @return int Get platform * @return int Get platform
* *
* @deprecated Use {@see ZipEntry::getCreatedOS()} * @deprecated Use {@see ZipEntry::getCreatedOS()}
* @noinspection PhpUsageOfSilenceOperatorInspection
*/ */
public function getPlatform() public function getPlatform()
{ {
@@ -198,11 +199,12 @@ abstract class ZipAbstractEntry implements ZipEntry
/** /**
* @param int $platform * @param int $platform
* *
* @throws ZipException
*
* @return ZipEntry * @return ZipEntry
* *
* @throws ZipException
*
* @deprecated Use {@see ZipEntry::setCreatedOS()} * @deprecated Use {@see ZipEntry::setCreatedOS()}
* @noinspection PhpUsageOfSilenceOperatorInspection
*/ */
public function setPlatform($platform) public function setPlatform($platform)
{ {

View File

@@ -107,7 +107,7 @@ class ZipInfo
const UNX_IXOTH = 00001; // Unix execute permission: other const UNX_IXOTH = 00001; // Unix execute permission: other
private static $valuesMadeBy = [ private static $platformNames = [
self::MADE_BY_MS_DOS => 'FAT', self::MADE_BY_MS_DOS => 'FAT',
self::MADE_BY_AMIGA => 'Amiga', self::MADE_BY_AMIGA => 'Amiga',
self::MADE_BY_OPEN_VMS => 'OpenVMS', self::MADE_BY_OPEN_VMS => 'OpenVMS',
@@ -130,7 +130,7 @@ class ZipInfo
self::MADE_BY_OS_X => 'Mac OS X', self::MADE_BY_OS_X => 'Mac OS X',
]; ];
private static $valuesCompressionMethod = [ private static $compressionMethodNames = [
ZipEntry::UNKNOWN => 'unknown', ZipEntry::UNKNOWN => 'unknown',
ZipFile::METHOD_STORED => 'no compression', ZipFile::METHOD_STORED => 'no compression',
1 => 'shrink', 1 => 'shrink',
@@ -214,6 +214,7 @@ class ZipInfo
* @param ZipEntry $entry * @param ZipEntry $entry
* *
* @throws ZipException * @throws ZipException
* @noinspection PhpMissingBreakStatementInspection
*/ */
public function __construct(ZipEntry $entry) public function __construct(ZipEntry $entry)
{ {
@@ -364,9 +365,7 @@ class ZipInfo
$field = $entry->getExtraFieldsCollection()->get(WinZipAesEntryExtraField::getHeaderId()); $field = $entry->getExtraFieldsCollection()->get(WinZipAesEntryExtraField::getHeaderId());
if ($field !== null) { if ($field !== null) {
/** /** @var WinZipAesEntryExtraField $field */
* @var WinZipAesEntryExtraField $field
*/
$method = $field->getMethod(); $method = $field->getMethod();
} }
} }
@@ -389,7 +388,7 @@ class ZipInfo
if ($entry->isEncrypted()) { if ($entry->isEncrypted()) {
if ($entry->getMethod() === ZipEntry::METHOD_WINZIP_AES) { if ($entry->getMethod() === ZipEntry::METHOD_WINZIP_AES) {
$return .= ucfirst(self::$valuesCompressionMethod[$entry->getMethod()]); $return .= ucfirst(self::$compressionMethodNames[$entry->getMethod()]);
/** @var WinZipAesEntryExtraField|null $field */ /** @var WinZipAesEntryExtraField|null $field */
$field = $entry->getExtraFieldsCollection()->get(WinZipAesEntryExtraField::getHeaderId()); $field = $entry->getExtraFieldsCollection()->get(WinZipAesEntryExtraField::getHeaderId());
@@ -404,8 +403,8 @@ class ZipInfo
$return .= ' '; $return .= ' ';
} }
if (isset(self::$valuesCompressionMethod[$compressionMethod])) { if (isset(self::$compressionMethodNames[$compressionMethod])) {
$return .= ucfirst(self::$valuesCompressionMethod[$compressionMethod]); $return .= ucfirst(self::$compressionMethodNames[$compressionMethod]);
} else { } else {
$return .= 'unknown'; $return .= 'unknown';
} }
@@ -420,8 +419,8 @@ class ZipInfo
*/ */
public static function getPlatformName(ZipEntry $entry) public static function getPlatformName(ZipEntry $entry)
{ {
if (isset(self::$valuesMadeBy[$entry->getCreatedOS()])) { if (isset(self::$platformNames[$entry->getCreatedOS()])) {
return self::$valuesMadeBy[$entry->getCreatedOS()]; return self::$platformNames[$entry->getCreatedOS()];
} }
return 'unknown'; return 'unknown';

View File

@@ -66,6 +66,7 @@ class IgnoreFilesRecursiveFilterIterator extends \RecursiveFilterIterator
/** /**
* @return IgnoreFilesRecursiveFilterIterator * @return IgnoreFilesRecursiveFilterIterator
* @noinspection PhpMissingParentCallCommonInspection
*/ */
public function getChildren() public function getChildren()
{ {

View File

@@ -740,16 +740,7 @@ class ZipFile implements ZipFileInterface
natcasesort($files); natcasesort($files);
$path = array_shift($files); $path = array_shift($files);
foreach ($files as $file) { $this->doAddFiles($path, $files, $localPath, $compressionMethod);
$relativePath = str_replace($path, $localPath, $file);
$relativePath = ltrim($relativePath, '\\/');
if (is_dir($file) && FilesUtil::isEmptyDir($file)) {
$this->addEmptyDir($relativePath);
} elseif (is_file($file)) {
$this->addFile($file, $relativePath, $compressionMethod);
}
}
return $this; return $this;
} }
@@ -824,25 +815,7 @@ class ZipFile implements ZipFileInterface
return $this; return $this;
} }
if (!empty($localPath) && \is_string($localPath)) { $this->doAddFiles($inputDir, $filesFound, $localPath, $compressionMethod);
$localPath = trim($localPath, '/\\') . '/';
} else {
$localPath = '/';
}
/**
* @var string $file
*/
foreach ($filesFound as $file) {
$filename = str_replace($inputDir, $localPath, $file);
$filename = ltrim($filename, '\\/');
if (is_dir($file) && FilesUtil::isEmptyDir($file)) {
$this->addEmptyDir($filename);
} elseif (is_file($file)) {
$this->addFile($file, $filename, $compressionMethod);
}
}
return $this; return $this;
} }
@@ -932,18 +905,34 @@ class ZipFile implements ZipFileInterface
return $this; return $this;
} }
if (!empty($localPath) && \is_string($localPath)) { $this->doAddFiles($inputDir, $files, $localPath, $compressionMethod);
$localPath = trim($localPath, '\\/') . '/';
return $this;
}
/**
* @param string $fileSystemDir
* @param array $files
* @param string $zipPath
* @param int|null $compressionMethod
*
* @throws ZipException
*/
private function doAddFiles($fileSystemDir, array $files, $zipPath, $compressionMethod = null)
{
$fileSystemDir = rtrim($fileSystemDir, '/\\') . \DIRECTORY_SEPARATOR;
if (!empty($zipPath) && \is_string($zipPath)) {
$zipPath = trim($zipPath, '\\/') . '/';
} else { } else {
$localPath = '/'; $zipPath = '/';
} }
$inputDir = rtrim($inputDir, '/\\') . \DIRECTORY_SEPARATOR;
/** /**
* @var string $file * @var string $file
*/ */
foreach ($files as $file) { foreach ($files as $file) {
$filename = str_replace($inputDir, $localPath, $file); $filename = str_replace($fileSystemDir, $zipPath, $file);
$filename = ltrim($filename, '\\/'); $filename = ltrim($filename, '\\/');
if (is_dir($file) && FilesUtil::isEmptyDir($file)) { if (is_dir($file) && FilesUtil::isEmptyDir($file)) {
@@ -952,8 +941,6 @@ class ZipFile implements ZipFileInterface
$this->addFile($file, $filename, $compressionMethod); $this->addFile($file, $filename, $compressionMethod);
} }
} }
return $this;
} }
/** /**
@@ -1443,26 +1430,25 @@ class ZipFile implements ZipFileInterface
* @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline * @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline
* *
* @throws ZipException * @throws ZipException
*
* @return string
*/ */
public function outputAsAttachment($outputFilename, $mimeType = null, $attachment = true) public function outputAsAttachment($outputFilename, $mimeType = null, $attachment = true)
{ {
$outputFilename = (string) $outputFilename; $outputFilename = (string) $outputFilename;
if (empty($mimeType) || (!\is_string($mimeType) && !empty($outputFilename))) { if ($mimeType === null) {
$ext = strtolower(pathinfo($outputFilename, \PATHINFO_EXTENSION)); $mimeType = $this->getMimeTypeByFilename($outputFilename);
if (!empty($ext) && isset(self::$defaultMimeTypes[$ext])) {
$mimeType = self::$defaultMimeTypes[$ext];
}
} }
if (empty($mimeType)) { if (!($handle = fopen('php://temp', 'w+b'))) {
$mimeType = self::$defaultMimeTypes['zip']; throw new InvalidArgumentException('php://temp cannot open for write.');
} }
$this->writeZipToStream($handle);
$content = $this->outputAsString();
$this->close(); $this->close();
$size = fstat($handle)['size'];
$headerContentDisposition = 'Content-Disposition: ' . ($attachment ? 'attachment' : 'inline'); $headerContentDisposition = 'Content-Disposition: ' . ($attachment ? 'attachment' : 'inline');
if (!empty($outputFilename)) { if (!empty($outputFilename)) {
@@ -1471,9 +1457,32 @@ class ZipFile implements ZipFileInterface
header($headerContentDisposition); header($headerContentDisposition);
header('Content-Type: ' . $mimeType); header('Content-Type: ' . $mimeType);
header('Content-Length: ' . \strlen($content)); header('Content-Length: ' . $size);
exit($content); rewind($handle);
try {
return stream_get_contents($handle, -1, 0);
} finally {
fclose($handle);
}
}
/**
* @param string $outputFilename
*
* @return string
*/
protected function getMimeTypeByFilename($outputFilename)
{
$outputFilename = (string) $outputFilename;
$ext = strtolower(pathinfo($outputFilename, \PATHINFO_EXTENSION));
if (!empty($ext) && isset(self::$defaultMimeTypes[$ext])) {
return self::$defaultMimeTypes[$ext];
}
return self::$defaultMimeTypes['zip'];
} }
/** /**
@@ -1492,22 +1501,15 @@ class ZipFile implements ZipFileInterface
{ {
$outputFilename = (string) $outputFilename; $outputFilename = (string) $outputFilename;
if (empty($mimeType) || (!\is_string($mimeType) && !empty($outputFilename))) { if ($mimeType === null) {
$ext = strtolower(pathinfo($outputFilename, \PATHINFO_EXTENSION)); $mimeType = $this->getMimeTypeByFilename($outputFilename);
if (!empty($ext) && isset(self::$defaultMimeTypes[$ext])) {
$mimeType = self::$defaultMimeTypes[$ext];
}
} }
if (empty($mimeType)) { if (!($handle = fopen('php://temp', 'w+b'))) {
$mimeType = self::$defaultMimeTypes['zip']; throw new InvalidArgumentException('php://temp cannot open for write.');
}
if (!($handle = fopen('php://memory', 'w+b'))) {
throw new InvalidArgumentException('Memory can not open from write.');
} }
$this->writeZipToStream($handle); $this->writeZipToStream($handle);
$this->close();
rewind($handle); rewind($handle);
$contentDispositionValue = ($attachment ? 'attachment' : 'inline'); $contentDispositionValue = ($attachment ? 'attachment' : 'inline');
@@ -1548,15 +1550,17 @@ class ZipFile implements ZipFileInterface
*/ */
public function outputAsString() public function outputAsString()
{ {
if (!($handle = fopen('php://memory', 'w+b'))) { if (!($handle = fopen('php://temp', 'w+b'))) {
throw new InvalidArgumentException('Memory can not open from write.'); throw new InvalidArgumentException('php://temp cannot open for write.');
} }
$this->writeZipToStream($handle); $this->writeZipToStream($handle);
rewind($handle); rewind($handle);
$content = stream_get_contents($handle);
fclose($handle);
return $content; try {
return stream_get_contents($handle);
} finally {
fclose($handle);
}
} }
/** /**

View File

@@ -13,6 +13,8 @@ class Issue24Test extends ZipTestCase
{ {
/** /**
* This method is called before the first test of this test class is run. * This method is called before the first test of this test class is run.
*
* @noinspection PhpMissingParentCallCommonInspection
*/ */
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {

View File

@@ -45,6 +45,7 @@ class ZipAlignTest extends ZipTestCase
* Test zip alignment. * Test zip alignment.
* *
* @throws ZipException * @throws ZipException
* @throws \Exception
*/ */
public function testZipAlignSourceZip() public function testZipAlignSourceZip()
{ {

View File

@@ -19,6 +19,8 @@ abstract class ZipTestCase extends TestCase
/** /**
* Before test. * Before test.
*
* @noinspection PhpMissingParentCallCommonInspection
*/ */
protected function setUp() protected function setUp()
{ {