wppaste
WordPress

_unzip_file_ziparchive( string $file, string $to, string[] $needed_dirs = array() ): true|WP_Error

Since
3.0.0
Source
wp-admin/includes/file.php:1683
Attempts to unzip an archive using the ZipArchive class.

Description

This function should not be called directly, use unzip_file() instead.

Assumes that WP_Filesystem() has already been called and set up.

Compatibility

WordPress
since 3.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filestring
Full path and filename of ZIP archive.
$tostring
Full path on the filesystem to extract archive to.
$needed_dirsstring[]optional
A partial list of required folders needed to be created.Default: array()

Return value

true|WP_Error
True on success, WP_Error on failure.

Performance profile

How much work a call to _unzip_file_ziparchive() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
25–115

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 281.

Plugin surface
2 hooks

Third-party callbacks on 'pre_unzip_file', 'unzip_file' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 15 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _unzip_file_ziparchive() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$zopen is null, false, long, double, string, array, object, resource25->open(), __(), ziparchive_error()
$zopen is not null, false, long, double, string, array, object, resource && $i36->open(), ->statIndex(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && !$i && !wp_doing_cron() && $pre !== null51–53->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->close()
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && $available_space52->open(), wp_doing_cron(), disk_free_space(), ->close(), __(), compact()
$zopen is not null, false, long, double, string, array, object, resource && !$i && !wp_doing_cron() && !$needed_dirs && !->mkdir() && !->is_dir()59–60->open(), wp_doing_cron(), array_unique(), asort(), ->mkdir(), ->is_dir(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && $pre !== null60–64->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->close()
$zopen is not null, false, long, double, string, array, object, resource && !$i && !wp_doing_cron() && $pre === null66–68->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->close(), apply_filters()
$zopen is not null, false, long, double, string, array, object, resource && !wp_doing_cron() && $pre === null68–70->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->statIndex(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && !$needed_dirs && !->mkdir() && !->is_dir()68–71->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), ->mkdir(), ->is_dir(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && $pre === null75–79->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->close(), apply_filters()
$zopen is not null, false, long, double, string, array, object, resource && wp_doing_cron() && $pre === null77–81->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->statIndex(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && !wp_doing_cron() && $pre === null && !str_ends_with() && $contents === false93–95->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->close(), __()
3 further outcomes, up to 115 instructions
$zopen is not null, false, long, double, string, array, object, resource && !wp_doing_cron() && $pre === null && !str_ends_with() && $contents !== false && !->put_contents()102–104->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->put_contents(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && wp_doing_cron() && $pre === null && !str_ends_with() && $contents === false102–106->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->close(), __()
$zopen is not null, false, long, double, string, array, object, resource && wp_doing_cron() && $pre === null && !str_ends_with() && $contents !== false && !->put_contents()111–115->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->put_contents(), ->close(), __()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev28025–114291 fewer instruction than PHP 8.5
8.528125–11529
8.428125–1152918 fewer instructions than PHP 8.3
8.329925–11829
8.229925–118291 more instruction than PHP 8.1
8.129825–146294 fewer instructions than PHP 7.4
7.430225–14829

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while _unzip_file_ziparchive() runs, in this order:

  1. apply_filters( pre_unzip_file )filterline 1792 (+109 into the body)

    Filters archive unzipping to override with a custom process.

  2. apply_filters( unzip_file )filterline 1848 (+165 into the body)

    Filters the result of unzipping an archive.

Uses · 10

Used by · 1

  • unzip_file()Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction.

Source code

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 ); 		if ( ! $info ) {			$z->close();			return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );		} 		if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.			continue;		} 		// Don't extract invalid files:		if ( 0 !== validate_file( $info['name'] ) ) {			continue;		} 		$uncompressed_size += $info['size']; 		$dirname = dirname( $info['name'] ); 		if ( str_ends_with( $info['name'], '/' ) ) {			// Directory.			$needed_dirs[] = $to . untrailingslashit( $info['name'] );		} elseif ( '.' !== $dirname ) {			// Path to a file.			$needed_dirs[] = $to . untrailingslashit( $dirname );		}	} 	// Enough space to unzip the file and copy its contents, with a 10% buffer.	$required_space = $uncompressed_size * 2.1; 	/*	 * disk_free_space() could return false. Assume that any falsey value is an error.	 * A disk that has zero free bytes has bigger problems.	 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.	 */	if ( wp_doing_cron() ) {		$available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; 		if ( $available_space && ( $required_space > $available_space ) ) {			$z->close();			return new WP_Error(				'disk_full_unzip_file',				__( 'Could not copy files. You may have run out of disk space.' ),				compact( 'uncompressed_size', 'available_space' )			);		}	} 	$needed_dirs = array_unique( $needed_dirs ); 	foreach ( $needed_dirs as $dir ) {		// Check the parent folders of the folders all exist within the creation array.		if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).			continue;		} 		if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it.			continue;		} 		$parent_folder = dirname( $dir ); 		while ( ! empty( $parent_folder )			&& untrailingslashit( $to ) !== $parent_folder

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.