KD2 Framework  Check-in [6029882a31]

Overview
Comment:Add typing to ZipWriter
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 7.3
Files: files | file ages | folders
SHA1: 6029882a312efd6efd3b3fa9ad3db9e59cb278fd
User & Date: bohwaz on 2021-09-20 17:59:30
Other Links: branch diff | manifest | tags
Context
2021-10-12
21:26
Smartyer: fix template path for some Windows setups check-in: 475c7190ca user: bohwaz tags: 7.3
2021-09-20
17:59
Add typing to ZipWriter check-in: 6029882a31 user: bohwaz tags: 7.3
2021-09-14
20:28
Fix SkrivLite call of extensions when there are no arguments check-in: d63c22fa14 user: bohwaz tags: 7.3
Changes

Modified src/lib/KD2/ZipWriter.php from [e058748519] to [bb9709959c].

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

	/**
	 * Sets compression rate (0 = no compression)
	 *
	 * @param integer $compression 0 to 9
	 * @return void
	 */
	public function setCompression($compression)
	{
		$compression = (int) $compression;
		$this->compression = max(min($compression, 9), 0);
	}

	/**
	 * Write to the current ZIP file
	 * @param string $data
	 * @return void
	 */
	protected function write($data)
	{
		// We can't use fwrite and ftell directly as ftell doesn't work on some pointers
		// (eg. php://output)
		fwrite($this->handle, $data);
		$this->pos += strlen($data);
	}

	/**
	 * Returns the content of the ZIP file
	 * 
	 * @return string
	 */
	public function get()
	{
		fseek($this->handle, 0);
		return stream_get_contents($this->handle);
	}

	public function __destruct()
	{
		$this->close();
	}

	/**
	 * Add a file to the current Zip archive using the given $data as content
	 *
	 * @param string $file File name
	 * @param string|null $data binary content of the file to add
	 * @param string|null $source Source file to use if no data is supplied
	 * @throws LogicException
	 * @throws RuntimeException
	 */
	public function add($file, $data = null, $source = null)
	{
		if ($this->closed)
		{
			throw new LogicException('Archive has been closed, files can no longer be added');
		}

		if (null === $data && null === $source) {







|










|









|


|



















|







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

	/**
	 * Sets compression rate (0 = no compression)
	 *
	 * @param integer $compression 0 to 9
	 * @return void
	 */
	public function setCompression(int $compression): void
	{
		$compression = (int) $compression;
		$this->compression = max(min($compression, 9), 0);
	}

	/**
	 * Write to the current ZIP file
	 * @param string $data
	 * @return void
	 */
	protected function write(string $data): void
	{
		// We can't use fwrite and ftell directly as ftell doesn't work on some pointers
		// (eg. php://output)
		fwrite($this->handle, $data);
		$this->pos += strlen($data);
	}

	/**
	 * Returns the content of the ZIP file
	 *
	 * @return string
	 */
	public function get(): string
	{
		fseek($this->handle, 0);
		return stream_get_contents($this->handle);
	}

	public function __destruct()
	{
		$this->close();
	}

	/**
	 * Add a file to the current Zip archive using the given $data as content
	 *
	 * @param string $file File name
	 * @param string|null $data binary content of the file to add
	 * @param string|null $source Source file to use if no data is supplied
	 * @throws LogicException
	 * @throws RuntimeException
	 */
	public function add(string $file, ?string $data = null, ?string $source = null): void
	{
		if ($this->closed)
		{
			throw new LogicException('Archive has been closed, files can no longer be added');
		}

		if (null === $data && null === $source) {
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		$this->directory[] = $this->makeRecord(true, $file, $size, $csize, $crc, $offset);
	}

	/**
	 * 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







|







172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		$this->directory[] = $this->makeRecord(true, $file, $size, $csize, $crc, $offset);
	}

	/**
	 * Add the closing footer to the archive
	 * @throws LogicException
	 */
	public function finalize(): void
	{
		if ($this->closed)
		{
			throw new LogicException('The ZIP archive has been closed. Files can no longer be added.');
		}

		// write central directory
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		$this->closed = true;
	}

	/**
	 * Close the file handle
	 * @return void
	 */
	public function close()
	{
		if (!$this->closed)
		{
			$this->finalize();
		}

		if ($this->handle)







|







202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		$this->closed = true;
	}

	/**
	 * Close the file handle
	 * @return void
	 */
	public function close(): void
	{
		if (!$this->closed)
		{
			$this->finalize();
		}

		if ($this->handle)
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	 * @param  string  $filename File name
	 * @param  integer $size     File size
	 * @param  integer $compressed_size
	 * @param  string  $crc      CRC32 of the file contents
	 * @param  integer|null  $offset
	 * @return string
	 */
	protected function makeRecord($central = false, $filename, $size, $compressed_size, $crc, $offset)
	{
		$header = ($central ? "\x50\x4b\x01\x02\x0e\x00" : "\x50\x4b\x03\x04");

		list($filename, $extra) = $this->encodeFilename($filename);

		$header .=
			"\x14\x00" // version needed to extract - 2.0







|







227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	 * @param  string  $filename File name
	 * @param  integer $size     File size
	 * @param  integer $compressed_size
	 * @param  string  $crc      CRC32 of the file contents
	 * @param  integer|null  $offset
	 * @return string
	 */
	protected function makeRecord(bool $central, string $filename, int $size, int $compressed_size, string $crc, ?int $offset): string
	{
		$header = ($central ? "\x50\x4b\x01\x02\x0e\x00" : "\x50\x4b\x03\x04");

		list($filename, $extra) = $this->encodeFilename($filename);

		$header .=
			"\x14\x00" // version needed to extract - 2.0
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

		$header .= $filename;
		$header .= $extra;

		return $header;
	}

	protected function encodeFilename($original)
	{
		if (utf8_decode($original) === $original) {
			return [$original, ''];
		}

		$data = "\x01" // version
			. pack('V', crc32($original))







|







260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

		$header .= $filename;
		$header .= $extra;

		return $header;
	}

	protected function encodeFilename(string $original): array
	{
		if (utf8_decode($original) === $original) {
			return [$original, ''];
		}

		$data = "\x01" // version
			. pack('V', crc32($original))