/** * Add a file to the current TAR archive using an existing file in the filesystem * * @param string $file path to the original file * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original * @throws ArchiveIOException */ public function addFile($file, $fileinfo = '') { if (is_string($fileinfo)) { $fileinfo = FileInfo::fromPath($file, $fileinfo); } if ($this->closed) { throw new ArchiveIOException('Archive has been closed, files can no longer be added'); } $fp = fopen($file, 'rb'); if (!$fp) { throw new ArchiveIOException('Could not open file for reading: ' . $file); } // create file header $this->writeFileHeader($fileinfo); // write data while (!feof($fp)) { $data = fread($fp, 512); if ($data === false) { break; } if ($data === '') { break; } $packed = pack("a512", $data); $this->writebytes($packed); } fclose($fp); }
/** * Add a file to the current archive using an existing file in the filesystem * * @param string $file path to the original file * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original * @throws ArchiveIOException */ public function addFile($file, $fileinfo = '') { if (is_string($fileinfo)) { $fileinfo = FileInfo::fromPath($file, $fileinfo); } if ($this->closed) { throw new ArchiveIOException('Archive has been closed, files can no longer be added'); } $data = @file_get_contents($file); if ($data === false) { throw new ArchiveIOException('Could not open file for reading: ' . $file); } // FIXME could we stream writing compressed data? gzwrite on a fopen handle? $this->addData($fileinfo, $data); }