ZipStream\ZipStream::addFileFromPath PHP Method

addFileFromPath() public method

add a file at path to the archive. Note that large files may be compresed differently than smaller files; see the "Large File Support" section above for more information.
public addFileFromPath ( String $name, String $path, array $opt = [] ) : void
$name String - name of file in archive (including directory path). @param String $path - path to file on disk (note: paths should be encoded using UNIX-style forward slashes -- e.g '/path/to/some/file'). @param array $opt - Hash of options for file (optional, see "File Options" below). File Options: time - Last-modified timestamp (seconds since the epoch) of this file. Defaults to the current time. comment - Comment related to this file. Examples: // add a file named 'foo.txt' from the local file '/tmp/foo.txt' $zip->addFileFromPath('foo.txt', '/tmp/foo.txt'); // add a file named 'bigfile.rar' from the local file // '/usr/share/bigfile.rar' with a comment and a last-modified // time of two hours ago $path = '/usr/share/bigfile.rar'; $zip->addFileFromPath('bigfile.rar', $path, array( 'time' => time() - 2 * 3600, 'comment' => 'this is a comment about bar.jpg', ));
$path String
$opt array
return void
    public function addFileFromPath($name, $path, $opt = array())
    {
        if (!is_readable($path)) {
            if (!file_exists($path)) {
                throw new FileNotFoundException($path);
            }
            throw new FileNotReadableException($path);
        }
        if ($this->isLargeFile($path)) {
            // file is too large to be read into memory; add progressively
            $this->addLargeFile($name, $path, $opt);
        } else {
            // file is small enough to read into memory; read file contents and
            // handle with addFile()
            $data = file_get_contents($path);
            $this->addFile($name, $data, $opt);
        }
    }

Usage Example

 public function testAddFileFromPath_largeFileMethods()
 {
     $methods = array(ZipStream::METHOD_STORE, ZipStream::METHOD_DEFLATE);
     foreach ($methods as $method) {
         list($tmp, $stream) = $this->getTmpFileStream();
         $zip = new ZipStream(null, array(ZipStream::OPTION_OUTPUT_STREAM => $stream, ZipStream::OPTION_LARGE_FILE_METHOD => $method, ZipStream::OPTION_LARGE_FILE_SIZE => 5));
         list($tmpExample, $streamExample) = $this->getTmpFileStream();
         for ($i = 0; $i <= 100000; $i++) {
             fwrite($streamExample, sha1($i));
             if ($i % 100 === 0) {
                 fwrite($streamExample, "\n");
             }
         }
         fclose($streamExample);
         $shaExample = sha1_file($tmpExample);
         $zip->addFileFromPath('sample.txt', $tmpExample);
         unlink($tmpExample);
         $zip->finish();
         fclose($stream);
         $tmpDir = $this->validateAndExtractZip($tmp);
         $files = $this->getRecursiveFileList($tmpDir);
         $this->assertEquals(array('sample.txt'), $files);
         $this->assertEquals(sha1_file($tmpDir . '/sample.txt'), $shaExample, "SHA-1 Mismatch Method: {$method}");
     }
 }