Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Stripping characters from filenames to ensure they are legal Windows …
Browse files Browse the repository at this point in the history
…filenames
  • Loading branch information
Nik Barham committed Jul 27, 2016
1 parent e4a7bd0 commit 628d028
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ZipStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ public function __construct($name = null, $opt = array())
*/
public function addFile($name, $data, $opt = array())
{
$name = $this->filterFilename($name);

// compress data
$zdata = gzdeflate($data);

Expand Down Expand Up @@ -306,6 +308,8 @@ public function addFile($name, $data, $opt = array())
*/
public function addFileFromPath($name, $path, $opt = array())
{
$name = $this->filterFilename($name);

if(!is_readable($path)) {
if(!file_exists($path)) {
throw new Ex\FileNotFoundException($path);
Expand Down Expand Up @@ -350,6 +354,8 @@ public function addFileFromPath($name, $path, $opt = array())
*/
public function addFileFromStream($name, $stream, $opt = array())
{
$name = $this->filterFilename($name);

$block_size = 1048576; // process in 1 megabyte chunks
$algo = 'crc32b';
$meth = static::METHOD_STORE;
Expand Down Expand Up @@ -403,6 +409,8 @@ public function addFileFromStream($name, $stream, $opt = array())
*/
public function addFileFromPsr7Stream($name, \Psr\Http\Message\StreamInterface $stream, $opt = array())
{
$name = $this->filterFilename($name);

$block_size = 1048576; // process in 1 megabyte chunks
$algo = 'crc32b';
$meth = static::METHOD_STORE;
Expand Down Expand Up @@ -1032,4 +1040,15 @@ protected function packFields($fields)
// build output string from header and compressed data
return call_user_func_array('pack', $args);
}

/**
* Strip characters that are not legal in Windows filenames to prevent compatability issues
*
* @param string $filename Unprocessed filename
* @return string
*/
protected function filterFilename($filename)
{
return str_replace(['\\', ':', '*', '?', '"', '<', '>', '|'], '_', $filename);
}
}

0 comments on commit 628d028

Please sign in to comment.