예제 #1
1
 /**
  * Test all methods
  *
  * @param string $zipClass
  * @covers ::<public>
  */
 public function testZipArchive($zipClass = 'ZipArchive')
 {
     // Preparation
     $existingFile = __DIR__ . '/../_files/documents/sheet.xls';
     $zipFile = __DIR__ . '/../_files/documents/ziptest.zip';
     $destination1 = __DIR__ . '/../_files/documents/extract1';
     $destination2 = __DIR__ . '/../_files/documents/extract2';
     @mkdir($destination1);
     @mkdir($destination2);
     Settings::setZipClass($zipClass);
     $object = new ZipArchive();
     $object->open($zipFile, ZipArchive::CREATE);
     $object->addFile($existingFile, 'xls/new.xls');
     $object->addFromString('content/string.txt', 'Test');
     $object->close();
     $object->open($zipFile);
     // Run tests
     $this->assertEquals(0, $object->locateName('xls/new.xls'));
     $this->assertFalse($object->locateName('blablabla'));
     $this->assertEquals('Test', $object->getFromName('content/string.txt'));
     $this->assertEquals('Test', $object->getFromName('/content/string.txt'));
     $this->assertFalse($object->getNameIndex(-1));
     $this->assertEquals('content/string.txt', $object->getNameIndex(1));
     $this->assertFalse($object->extractTo('blablabla'));
     $this->assertTrue($object->extractTo($destination1));
     $this->assertTrue($object->extractTo($destination2, 'xls/new.xls'));
     $this->assertFalse($object->extractTo($destination2, 'blablabla'));
     // Cleanup
     $this->deleteDir($destination1);
     $this->deleteDir($destination2);
     @unlink($zipFile);
 }
예제 #2
0
 /**
  * Get DOMDocument from ZipArchive
  *
  * @param string $zipFile
  * @param string $xmlFile
  * @return \DOMDocument|false
  * @throws \PhpOffice\PhpWord\Exception\Exception
  */
 public function getDomFromZip($zipFile, $xmlFile)
 {
     if (file_exists($zipFile) === false) {
         throw new Exception('Cannot find archive file.');
     }
     $zip = new ZipArchive();
     $zip->open($zipFile);
     $content = $zip->getFromName($xmlFile);
     $zip->close();
     if ($content === false) {
         return false;
     } else {
         return $this->getDomFromString($content);
     }
 }
예제 #3
0
 /**
  * Extract file from archive by given file name (emulate \ZipArchive)
  *
  * @param  string $filename Filename for the file in zip archive
  * @return string $contents File string contents
  */
 public function getFromName($filename)
 {
     if (!$this->usePclzip) {
         $contents = $this->zip->getFromName($filename);
         if ($contents === false) {
             $filename = substr($filename, 1);
             $contents = $this->zip->getFromName($filename);
         }
     } else {
         $contents = $this->pclzipGetFromName($filename);
     }
     return $contents;
 }
 /**
  * Add chart.
  *
  * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
  * @param integer &$rId
  * @return void
  */
 private function addChart(ZipArchive $zip, &$rId)
 {
     $phpWord = $this->getPhpWord();
     $collection = $phpWord->getCharts();
     $index = 0;
     if ($collection->countItems() > 0) {
         foreach ($collection->getItems() as $chart) {
             $index++;
             $rId++;
             $filename = "charts/chart{$index}.xml";
             // ContentTypes.xml
             $this->contentTypes['override']["/word/{$filename}"] = 'chart';
             // word/_rels/document.xml.rel
             $this->relationships[] = array('target' => $filename, 'type' => 'chart', 'rID' => $rId);
             // word/charts/chartN.xml
             /** @var \PhpOffice\PhpWord\Element\Chart $chart */
             $chart->setRelationId($rId);
             $writerPart = $this->getWriterPart('Chart');
             $writerPart->setElement($chart);
             $zip->addFromString("word/{$filename}", $writerPart->write());
         }
     }
 }
예제 #5
0
파일: Word2007.php 프로젝트: hcvcastro/pxp
 /**
  * Add header/footer content
  *
  * @param \PhpOffice\PhpWord\Element\Section $section
  * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
  * @param string $elmType header|footer
  * @param integer $rId
  */
 private function addHeaderFooterContent(Section &$section, ZipArchive $zip, $elmType, &$rId)
 {
     $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
     $elmCount = ($section->getSectionId() - 1) * 3;
     $elements = $section->{$getFunction}();
     foreach ($elements as &$element) {
         /** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */
         $elmCount++;
         $element->setRelationId(++$rId);
         $elmFile = "{$elmType}{$elmCount}.xml";
         // e.g. footer1.xml
         $this->contentTypes['override']["/word/{$elmFile}"] = $elmType;
         $this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
         /** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $writerPart Type hint */
         $writerPart = $this->getWriterPart($elmType)->setElement($element);
         $zip->addFromString("word/{$elmFile}", $writerPart->write());
     }
 }
예제 #6
0
 /**
  * Add file to package
  *
  * Get the actual source from an archive image
  *
  * @param \PhpOffice\PhpWord\Shared\ZipArchive $zipPackage
  * @param string $source
  * @param string $target
  */
 protected function addFileToPackage($zipPackage, $source, $target)
 {
     $isArchive = strpos($source, 'zip://') !== false;
     $actualSource = null;
     if ($isArchive) {
         $source = substr($source, 6);
         list($zipFilename, $imageFilename) = explode('#', $source);
         $zip = new ZipArchive();
         if ($zip->open($zipFilename) !== false) {
             if ($zip->locateName($imageFilename)) {
                 $zip->extractTo($this->getTempDir(), $imageFilename);
                 $actualSource = $this->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
             }
         }
         $zip->close();
     } else {
         $actualSource = $source;
     }
     if (!is_null($actualSource)) {
         $zipPackage->addFile($actualSource, $target);
     }
 }
예제 #7
0
 /**
  * Test all methods
  *
  * @covers ::<public>
  */
 public function testAllMethods()
 {
     // Preparation
     $existingFile = __DIR__ . "/../_files/documents/sheet.xls";
     $zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
     $destination1 = __DIR__ . "/../_files/extract1";
     $destination2 = __DIR__ . "/../_files/extract2";
     $destination3 = __DIR__ . "/../_files/extract3";
     @mkdir($destination1);
     @mkdir($destination2);
     @mkdir($destination3);
     $object = new ZipArchive();
     $object->open($zipFile);
     $object->addFile($existingFile, 'xls/new.xls');
     $object->addFromString('content/string.txt', 'Test');
     $object->close();
     // Run tests
     $this->assertEquals(0, $object->locateName('xls/new.xls'));
     $this->assertFalse($object->locateName('blablabla'));
     $this->assertEquals('Test', $object->getFromName('content/string.txt'));
     $this->assertEquals('Test', $object->getFromName('/content/string.txt'));
     $this->assertFalse($object->getNameIndex(-1));
     $this->assertEquals('content/string.txt', $object->getNameIndex(1));
     $this->assertFalse($object->extractTo('blablabla'));
     $this->assertTrue($object->extractTo($destination1));
     $this->assertTrue($object->extractTo($destination2, 'xls/new.xls'));
     $this->assertFalse($object->extractTo($destination2, 'blablabla'));
     // Cleanup
     $this->deleteDir($destination1);
     $this->deleteDir($destination2);
     $this->deleteDir($destination3);
     @unlink($zipFile);
 }
예제 #8
0
파일: Image.php 프로젝트: Senasum/PHPWord
 /**
  * Get image size from archive
  *
  * @since 0.12.0 Throws CreateTemporaryFileException.
  *
  * @param string $source
  * @return array|null
  * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
  */
 private function getArchiveImageSize($source)
 {
     $imageData = null;
     $source = substr($source, 6);
     list($zipFilename, $imageFilename) = explode('#', $source);
     $tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage');
     if (false === $tempFilename) {
         throw new CreateTemporaryFileException();
     }
     $zip = new ZipArchive();
     if ($zip->open($zipFilename) !== false) {
         if ($zip->locateName($imageFilename)) {
             $imageContent = $zip->getFromName($imageFilename);
             if ($imageContent !== false) {
                 file_put_contents($tempFilename, $imageContent);
                 $imageData = getimagesize($tempFilename);
                 unlink($tempFilename);
             }
         }
         $zip->close();
     }
     return $imageData;
 }
 /**
  * Read all relationship files
  *
  * @param string $docFile
  * @return array
  */
 private function readRelationships($docFile)
 {
     $relationships = array();
     // _rels/.rels
     $relationships['main'] = $this->getRels($docFile, '_rels/.rels');
     // word/_rels/*.xml.rels
     $wordRelsPath = 'word/_rels/';
     $zip = new ZipArchive();
     if ($zip->open($docFile) === true) {
         for ($i = 0; $i < $zip->numFiles; $i++) {
             $xmlFile = $zip->getNameIndex($i);
             if (substr($xmlFile, 0, strlen($wordRelsPath)) == $wordRelsPath && substr($xmlFile, -1) != '/') {
                 $docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile));
                 $relationships[$docPart] = $this->getRels($docFile, $xmlFile, 'word/');
             }
         }
         $zip->close();
     }
     return $relationships;
 }
예제 #10
0
 /**
  * Get image size from archive
  *
  * @param string $source
  * @return array|null
  */
 private function getArchiveImageSize($source)
 {
     $imageData = null;
     $source = substr($source, 6);
     list($zipFilename, $imageFilename) = explode('#', $source);
     $tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
     $zip = new ZipArchive();
     if ($zip->open($zipFilename) !== false) {
         if ($zip->locateName($imageFilename)) {
             $imageContent = $zip->getFromName($imageFilename);
             if ($imageContent !== false) {
                 file_put_contents($tempFilename, $imageContent);
                 $imageData = @getimagesize($tempFilename);
                 unlink($tempFilename);
             }
         }
         $zip->close();
     }
     return $imageData;
 }