1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-29 09:50:40 +02:00

Fix #22 Adding links to files, not open handles

This commit is contained in:
Ne-Lexa
2018-10-10 16:53:17 +03:00
parent ad3bac6f96
commit c863c18869
5 changed files with 77 additions and 21 deletions

View File

@@ -6,9 +6,6 @@ use PhpZip\Exception\InvalidArgumentException;
use PhpZip\ZipFileInterface;
/**
* Abstract class for new zip entry.
*
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
* @author Ne-Lexa alexey@nelexa.ru
* @license MIT
*/

View File

@@ -0,0 +1,53 @@
<?php
namespace PhpZip\Model\Entry;
use PhpZip\Exception\InvalidArgumentException;
use PhpZip\Exception\RuntimeException;
use PhpZip\Exception\ZipException;
/**
* @author Ne-Lexa alexey@nelexa.ru
* @license MIT
*/
class ZipNewFileEntry extends ZipAbstractEntry
{
/**
* @var string Filename
*/
protected $file;
/**
* ZipNewEntry constructor.
* @param string $file
* @throws ZipException
*/
public function __construct($file)
{
parent::__construct();
if ($file === null){
throw new InvalidArgumentException("file is null");
}
$file = (string)$file;
if (!is_file($file)){
throw new ZipException("File $file does not exist.");
}
if (!is_readable($file)){
throw new ZipException("The '$file' file could not be read. Check permissions.");
}
$this->file = $file;
}
/**
* Returns an string content of the given entry.
*
* @return null|string
*/
public function getEntryContent()
{
if (!is_file($this->file)){
throw new RuntimeException("File {$this->file} does not exist.");
}
return file_get_contents($this->file);
}
}

View File

@@ -7,6 +7,7 @@ use PhpZip\Exception\ZipEntryNotFoundException;
use PhpZip\Exception\ZipException;
use PhpZip\Exception\ZipUnsupportMethodException;
use PhpZip\Model\Entry\ZipNewEntry;
use PhpZip\Model\Entry\ZipNewFileEntry;
use PhpZip\Model\ZipEntry;
use PhpZip\Model\ZipEntryMatcher;
use PhpZip\Model\ZipInfo;
@@ -414,12 +415,7 @@ class ZipFile implements ZipFileInterface
*/
public function addFile($filename, $localName = null, $compressionMethod = null)
{
if ($filename === null) {
throw new InvalidArgumentException("Filename is null");
}
if (!is_file($filename)) {
throw new ZipException("File $filename is not exists");
}
$entry = new ZipNewFileEntry($filename);
if ($compressionMethod === null) {
if (function_exists('mime_content_type')) {
@@ -442,14 +438,24 @@ class ZipFile implements ZipFileInterface
throw new ZipUnsupportMethodException('Unsupported compression method ' . $compressionMethod);
}
if (!($handle = @fopen($filename, 'rb'))) {
throw new ZipException('File ' . $filename . ' can not open.');
}
if ($localName === null) {
$localName = basename($filename);
}
$this->addFromStream($handle, $localName, $compressionMethod);
$this->zipModel->getEntry($localName)->setTime(filemtime($filename));
$localName = ltrim((string)$localName, "\\/");
if (strlen($localName) === 0) {
throw new InvalidArgumentException("Empty entry name");
}
$stat = stat($filename);
$mode = sprintf('%o', $stat['mode']);
$externalAttributes = (octdec($mode) & 0xffff) << 16;
$entry->setName($localName);
$entry->setMethod($compressionMethod);
$entry->setTime($stat['mtime']);
$entry->setExternalAttributes($externalAttributes);
$this->zipModel->addEntry($entry);
return $this;
}