ZipArchive example

/** * @param string $filePath * @param string $extractPath * * @throws Exception * * @return array */
    private function extractEmotionArchive($filePath$extractPath)
    {
        $zip = new ZipArchive();

        if ($zip->open($filePath) !== true) {
            throw new EmotionImportException(sprintf('Could not open zip file "%s"!', $filePath));
        }

        if ($zip->locateName('emotion.json') === false) {
            throw new EmotionImportException(sprintf('Missing emotion.json in %s!', $filePath));
        }

        if ($zip->extractTo($extractPath) !== true) {
            throw new EmotionImportException(sprintf('Could not extract zip file %s to %s!', $filePath$extractPath));
        }
/** * @param string $fileName * * @throws Exception */
    public function __construct($fileName = null, $flags = null)
    {
        if (!\extension_loaded('zip')) {
            throw new Exception('The PHP extension "zip" is not loaded.');
        }

        $this->stream = new ZipArchive();

        if ($fileName != null) {
            if (($retval = $this->stream->open($fileName$flags)) !== true) {
                throw new RuntimeException($this->getErrorMessage($retval$fileName)$retval);
            }
            $this->position = 0;
            $this->count = $this->stream->numFiles;
        }
    }

    /** * @return Entry\Zip */


    /** * @param int $emotionId * * @throws Exception * * @return string */
    public function export($emotionId)
    {
        $zip = new ZipArchive();

        $name = $this->connection->fetchColumn('SELECT name FROM s_emotion WHERE id = :id', [':id' => $emotionId]);
        $name = strtolower($this->slug->slugify($name));

        $filename = $this->rootDirectory . '/files/downloads/' . $name . time() . '.zip';

        if ($zip->open($filename, ZipArchive::CREATE) !== true) {
            throw new Exception(sprintf('Could not create zip file "%s"!', $filename));
        }

        $emotionData = $this->transformer->transform($emotionId, true);

        
if (!is_file($path)) {
      throw new DriverException('File does not exist locally and cannot be uploaded to the remote instance.');
    }

    if (!class_exists('ZipArchive')) {
      throw new DriverException('Could not compress file, PHP is compiled without zip support.');
    }

    // Selenium only accepts uploads that are compressed as a Zip archive.     $tempFilename = tempnam('', 'WebDriverZip');

    $archive = new \ZipArchive();
    $result = $archive->open($tempFilename, \ZipArchive::OVERWRITE);
    if (!$result) {
      throw new DriverException('Zip archive could not be created. Error ' . $result);
    }
    $result = $archive->addFile($pathbasename($path));
    if (!$result) {
      throw new DriverException('File could not be added to zip archive.');
    }
    $result = $archive->close();
    if (!$result) {
      throw new DriverException('Zip archive could not be closed.');
    }

  public function __construct($file_path, array $configuration = []) {
    $this->zip = new \ZipArchive();
    if ($this->zip->open($file_path$configuration['flags'] ?? 0) !== TRUE) {
      throw new ArchiverException("Cannot open '$file_path'");
    }
  }

  /** * {@inheritdoc} */
  public function add($file_path) {
    $this->zip->addFile($file_path);

    
/** * @param string $fileName * * @throws Exception */
    public function __construct($fileName = null, $flags = null)
    {
        if (!\extension_loaded('zip')) {
            throw new Exception('The PHP extension "zip" is not loaded.');
        }

        $this->stream = new ZipArchive();

        if ($fileName != null) {
            if (($retval = $this->stream->open($fileName$flags)) !== true) {
                throw new RuntimeException($this->getErrorMessage($retval$fileName)$retval);
            }
            $this->position = 0;
            $this->count = $this->stream->numFiles;
        }
    }

    /** * @return Entry\Zip */
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Plugin\PluginException;

#[Package('core')] class ZipUtils
{
    private const HEADER_SIGNATURE = '504b0304';

    public static function openZip(string $filename): \ZipArchive
    {
        $stream = new \ZipArchive();

        if (!file_exists($filename)) {
            throw PluginException::cannotExtractNoSuchFile($filename);
        }

        if (!self::validateFileIsZip($filename)) {
            throw PluginException::cannotExtractInvalidZipFile($filename);
        }

        if (($retVal = $stream->open($filename)) !== true) {
            throw PluginException::cannotExtractZipOpenError(self::getErrorMessage($retVal$filename));
        }
function wp_generate_block_templates_export_file() {
    global $wp_version;

    if ( ! class_exists( 'ZipArchive' ) ) {
        return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.' ) );
    }

    $obscura    = wp_generate_password( 12, false, false );
    $theme_name = basename( get_stylesheet() );
    $filename   = get_temp_dir() . $theme_name . $obscura . '.zip';

    $zip = new ZipArchive();
    if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
        return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.' ) );
    }

    $zip->addEmptyDir( 'templates' );
    $zip->addEmptyDir( 'parts' );

    // Get path of the theme.     $theme_path = wp_normalize_path( get_stylesheet_directory() );

    // Create recursive directory iterator.
$archive_pathname = $exports_dir . $archive_filename;

        update_post_meta( $request_id, '_export_file_name', $archive_filename );
    }

    $archive_url = $exports_url . $archive_filename;

    if ( ! empty( $archive_pathname ) && file_exists( $archive_pathname ) ) {
        wp_delete_file( $archive_pathname );
    }

    $zip = new ZipArchive();
    if ( true === $zip->open( $archive_pathname, ZipArchive::CREATE ) ) {
        if ( ! $zip->addFile( $json_report_pathname, 'export.json' ) ) {
            $error = __( 'Unable to archive the personal data export file (JSON format).' );
        }

        if ( ! $zip->addFile( $html_report_pathname, 'index.html' ) ) {
            $error = __( 'Unable to archive the personal data export file (HTML format).' );
        }

        $zip->close();

        
use ZipArchive;

class ZipUtils
{
    /** * @param string $filename * * @return ZipArchive */
    public static function openZip($filename)
    {
        $stream = new ZipArchive();

        if (($retVal = $stream->open($filename)) !== true) {
            throw new RuntimeException(self::getErrorMessage($retVal$filename)$retVal);
        }

        return $stream;
    }

    /** * @param int $retVal * @param string $file * * @return string */

function _unzip_file_ziparchive( $file$to$needed_dirs = array() ) {
    global $wp_filesystem;

    $z = new ZipArchive();

    $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );

    if ( true !== $zopen ) {
        return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
    }

    $uncompressed_size = 0;

    for ( $i = 0; $i < $z->numFiles; $i++ ) {
        $info = $z->statIndex( $i );

        

    protected function createZip($name)
    {
        $zipPath = $this->outputPath . $name . '.zip';

        $zip = new ZipArchive();

        if ($zip->open($zipPath, ZipArchive::CREATE) !== true) {
            throw new RuntimeException(sprintf('Could not open %s, please check the permissions.', $zipPath));
        }

        $files = $this->getDirectoryList($this->outputPath);
        foreach ($files as $file) {
            $zip->addFile($filebasename($file));
        }
        $result = $zip->close();

        
Home | Imprint | This part of the site doesn't use cookies.