KD2 Framework  Check-in [8826f9765e]

Overview
Comment:ZipWriter: use statements for exceptions
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | 5.6
Files: files | file ages | folders
SHA1: 8826f9765e94c012bbdd2b91edec372b3e6a6455
User & Date: bohwaz on 2019-02-19 14:52:18
Other Links: manifest | tags
Context
2019-02-19
14:52
UserActions: fix typo check-in: 04ca11897e user: bohwaz tags: 5.6, trunk
14:52
ZipWriter: use statements for exceptions check-in: 8826f9765e user: bohwaz tags: 5.6, trunk
14:51
DB_SQLite3: fix PHPStan notices check-in: 52d1315667 user: bohwaz tags: 5.6, trunk
Changes

Modified src/lib/KD2/ZipWriter.php from [2af15f2add] to [1d6df2f495].

17
18
19
20
21
22
23



24
25
26
27
28
29
30

    You should have received a copy of the GNU Affero General Public License
    along with Foobar.  If not, see <https://www.gnu.org/licenses/>.
*/

namespace KD2;




/**
 * Very simple ZIP Archive writer
 *
 * for specs see http://www.pkware.com/appnote
 * Inspired by https://github.com/splitbrain/php-archive/blob/master/src/Zip.php
 */
class ZipWriter







>
>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

    You should have received a copy of the GNU Affero General Public License
    along with Foobar.  If not, see <https://www.gnu.org/licenses/>.
*/

namespace KD2;

use LogicException;
use RuntimeException;

/**
 * Very simple ZIP Archive writer
 *
 * for specs see http://www.pkware.com/appnote
 * Inspired by https://github.com/splitbrain/php-archive/blob/master/src/Zip.php
 */
class ZipWriter
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	 */
	public function __construct($file)
	{
		$this->handle = fopen($file, 'wb');

		if (!$this->handle)
		{
			throw new \RuntimeException('Could not open ZIP file for writing: ' . $file);
		}
	}

	/**
	 * Sets compression rate (0 = no compression)
	 *
	 * @param integer $compression 0 to 9







|







46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	 */
	public function __construct($file)
	{
		$this->handle = fopen($file, 'wb');

		if (!$this->handle)
		{
			throw new RuntimeException('Could not open ZIP file for writing: ' . $file);
		}
	}

	/**
	 * Sets compression rate (0 = no compression)
	 *
	 * @param integer $compression 0 to 9
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	 * @throws LogicException
	 * @throws RuntimeException
	 */
	public function add($file, $data = null)
	{
		if ($this->closed)
		{
			throw new \LogicException('Archive has been closed, files can no longer be added');
		}

		$source_handle = null;

		if ($data === null)
		{
			$csize = $size = filesize($file);
			list(, $crc) = unpack('N', hash_file('crc32b', $file, true));
			$source_handle = fopen($file, 'r');

			if ($this->compression)
			{
				// Unfortunately it's not possible to use stream_filter_append
				// to compress data on the fly, as it's not working correctly
				// with php://output, php://temp and php://memory streams
				throw new \RuntimeException('Compression is not supported with external files');
			}
		}
		else
		{
			$size = strlen($data);
			$crc  = crc32($data);








|















|







103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	 * @throws LogicException
	 * @throws RuntimeException
	 */
	public function add($file, $data = null)
	{
		if ($this->closed)
		{
			throw new LogicException('Archive has been closed, files can no longer be added');
		}

		$source_handle = null;

		if ($data === null)
		{
			$csize = $size = filesize($file);
			list(, $crc) = unpack('N', hash_file('crc32b', $file, true));
			$source_handle = fopen($file, 'r');

			if ($this->compression)
			{
				// Unfortunately it's not possible to use stream_filter_append
				// to compress data on the fly, as it's not working correctly
				// with php://output, php://temp and php://memory streams
				throw new RuntimeException('Compression is not supported with external files');
			}
		}
		else
		{
			$size = strlen($data);
			$crc  = crc32($data);

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	 * Add the closing footer to the archive
	 * @throws LogicException
	 */
	public function finalize()
	{
		if ($this->closed)
		{
			throw new \LogicException('The ZIP archive has been closed. Files can no longer be added.');
		}

		// write central directory
		$offset = $this->pos;
		$directory = implode('', $this->directory);
		$this->write($directory);








|







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	 * Add the closing footer to the archive
	 * @throws LogicException
	 */
	public function finalize()
	{
		if ($this->closed)
		{
			throw new LogicException('The ZIP archive has been closed. Files can no longer be added.');
		}

		// write central directory
		$offset = $this->pos;
		$directory = implode('', $this->directory);
		$this->write($directory);