Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/ZipStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ protected function getStorageConstant($storage_method)
*
* @return void
*/
public function addFileFromStream($name, $stream, $opt = array(), $storage_method = 'deflate')
public function addFileFromStream($name, $stream, $opt = array(), $storage_method = self::METHOD_DEFLATE)
{
$block_size = 1048576; // process in 1 megabyte chunks
$algo = 'crc32b';
Expand All @@ -698,6 +698,15 @@ public function addFileFromStream($name, $stream, $opt = array(), $storage_metho
$crc = $zlen = $len = 0;
$hash_ctx = hash_init($algo);

if ($storage_method == self::METHOD_DEFLATE)
{
$deflateCtx = deflate_init(ZLIB_ENCODING_RAW, ['level' => 6]);
}
else
{
$deflateCtx = null;
}

// send local file header.
$num_bytes_written = $this->addFileHeader($name, $opt, $meth, $crc, $zlen, $len, $genb);

Expand All @@ -713,7 +722,14 @@ public function addFileFromStream($name, $stream, $opt = array(), $storage_metho
break;
}

$zdata = $meth == self::COMPRESS ? gzdeflate($data) : $data;
if ($deflateCtx !== null)
{
$zdata = deflate_add($deflateCtx, $data, ZLIB_NO_FLUSH);
}
else
{
$zdata = $data;
}

$this->send($zdata);

Expand All @@ -723,6 +739,14 @@ public function addFileFromStream($name, $stream, $opt = array(), $storage_metho
$zlen += strlen($zdata);
}

if ($deflateCtx !== null)
{
//finalize the compressed data
$zdata = deflate_add($deflateCtx, '', ZLIB_FINISH);
$zlen += strlen($zdata);
$this->send($zdata);
}

// Calculate the actual crc.
$crc = hexdec(hash_final($hash_ctx));

Expand Down