Oihana PHP

zip.php

Table of Contents

Functions

zip()  : string
Creates a zip archive from one or more files and/or directories.

Functions

zip()

Creates a zip archive from one or more files and/or directories.

zip(string|array<string|int, string> $paths[, string|null $outputPath = null ][, string|null $compression = CompressionType::ZIP ][, string|null $preserveRoot = null ]) : string

This function supports adding multiple paths (files or directories) to a zip archive, with a per-entry compression method. It can preserve the root directory structure inside the archive, and generates a unique temporary archive if no output path is specified.

Empty directories are preserved in the archive.

Parameters
$paths : string|array<string|int, string>

Absolute path(s) to file(s) or directory(ies) to include in the archive.

$outputPath : string|null = null

Optional full path to the final archive file to create. If null, an automatic unique filename with timestamp is generated in the system temp directory.

$compression : string|null = CompressionType::ZIP

Per-entry compression method. Supported values are CompressionType::ZIP (DEFLATE, the default) and CompressionType::NONE (stored, no compression).

$preserveRoot : string|null = null

If set, paths inside the archive will be stored relative to this directory, allowing to preserve directory structure when extracting. Must be an absolute path.

Tags
throws
FileException

If any of the provided paths does not exist, or if the archive file cannot be created.

UnsupportedCompressionException

If the requested compression method is not supported for zip archives.

DirectoryException

If the temporary directory cannot be created or accessed.

RuntimeException

If no files are added to the archive.

see
CompressionType
example

Archive a single file, auto-named, DEFLATE compressed (default):

$zipPath = zip('/var/www/html/index.php');

Archive a directory without compression (stored):

$zipPath = zip('/var/www/html', '/tmp/site.zip', CompressionType::NONE);

Archive multiple files:

$zipPath = zip(['/etc/hosts', '/etc/hostname'], '/tmp/config.zip');

Archive directory with root preserved (relative paths):

$zipPath = zip('/var/www/html/project', '/tmp/project.zip', CompressionType::ZIP, '/var/www/html/project');
author

Marc Alcaraz (ekameleon)

since
1.2.0
Return values
string

Returns the full path to the created zip archive file.

On this page

Search results