Пример #1
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);
 }
Пример #2
0
 /**
  * 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);
 }
Пример #3
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);
     }
 }
Пример #4
0
 /**
  * Get image string data
  *
  * @param bool $base64
  * @return string|null
  * @since 0.11.0
  */
 public function getImageStringData($base64 = false)
 {
     $source = $this->source;
     $actualSource = null;
     $imageBinary = null;
     $imageData = null;
     $isTemp = false;
     // Get actual source from archive image or other source
     // Return null if not found
     if ($this->sourceType == self::SOURCE_ARCHIVE) {
         $source = substr($source, 6);
         list($zipFilename, $imageFilename) = explode('#', $source);
         $zip = new ZipArchive();
         if ($zip->open($zipFilename) !== false) {
             if ($zip->locateName($imageFilename)) {
                 $isTemp = true;
                 $zip->extractTo(Settings::getTempDir(), $imageFilename);
                 $actualSource = Settings::getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
             }
         }
         $zip->close();
     } else {
         $actualSource = $source;
     }
     // Can't find any case where $actualSource = null hasn't captured by
     // preceding exceptions. Please uncomment when you find the case and
     // put the case into Element\ImageTest.
     // if ($actualSource === null) {
     //     return null;
     // }
     // Read image binary data and convert to hex/base64 string
     if ($this->sourceType == self::SOURCE_GD) {
         $imageResource = call_user_func($this->imageCreateFunc, $actualSource);
         ob_start();
         call_user_func($this->imageFunc, $imageResource);
         $imageBinary = ob_get_contents();
         ob_end_clean();
     } else {
         $fileHandle = fopen($actualSource, 'rb', false);
         if ($fileHandle !== false) {
             $imageBinary = fread($fileHandle, filesize($actualSource));
             fclose($fileHandle);
         }
     }
     if ($imageBinary !== null) {
         if ($base64) {
             $imageData = chunk_split(base64_encode($imageBinary));
         } else {
             $imageData = chunk_split(bin2hex($imageBinary));
         }
     }
     // Delete temporary file if necessary
     if ($isTemp === true) {
         @unlink($actualSource);
     }
     return $imageData;
 }