unzip_file
Filter HookDescription
Attempts to unzip an archive using the PclZip library. This function should not be called directly, use `unzip_file()` instead. Assumes that WP_Filesystem() has already been called and set up. / function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) { global $wp_filesystem; mbstring_binary_safe_encoding(); require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; $archive = new PclZip( $file ); $archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING ); reset_mbstring_encoding(); // Is the archive valid? if ( ! is_array( $archive_files ) ) { return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) ); } if ( 0 === count( $archive_files ) ) { return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) ); } $uncompressed_size = 0; // Determine any children directories needed (From within the archive). foreach ( $archive_files as $file ) { if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. continue; } $uncompressed_size += $file['size']; $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) ); } // 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.Hook Information
File Location |
wp-admin/includes/file.php
View on GitHub
|
Hook Type | Filter |
Line Number | 1990 |
Hook Parameters
Type | Name | Description |
---|---|---|
string
|
$file
|
Full path and filename of ZIP archive. |
string
|
$to
|
Full path on the filesystem to extract archive to. |
string[]
|
$needed_dirs
|
A partial list of required folders needed to be created. |
Usage Examples
Basic Usage
<?php
// Hook into unzip_file
add_filter('unzip_file', 'my_custom_filter', 10, 3);
function my_custom_filter($file, $to, $needed_dirs) {
// Your custom filtering logic here
return $file;
}
Source Code Context
wp-admin/includes/file.php:1990
- How this hook is used in WordPress core
<?php
1985 return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
1986 }
1987 }
1988
1989 /** This action is documented in src/wp-admin/includes/file.php */
1990 $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space );
1991
1992 unset( $needed_dirs );
1993
1994 return $result;
1995 }
PHP Documentation
<?php
/**
* Attempts to unzip an archive using the PclZip library.
*
* This function should not be called directly, use `unzip_file()` instead.
*
* Assumes that WP_Filesystem() has already been called and set up.
*
* @since 3.0.0
* @access private
*
* @see unzip_file()
*
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*
* @param string $file Full path and filename of ZIP archive.
* @param string $to Full path on the filesystem to extract archive to.
* @param string[] $needed_dirs A partial list of required folders needed to be created.
* @return true|WP_Error True on success, WP_Error on failure.
*/
function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
global $wp_filesystem;
mbstring_binary_safe_encoding();
require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
$archive = new PclZip( $file );
$archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING );
reset_mbstring_encoding();
// Is the archive valid?
if ( ! is_array( $archive_files ) ) {
return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) );
}
if ( 0 === count( $archive_files ) ) {
return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
}
$uncompressed_size = 0;
// Determine any children directories needed (From within the archive).
foreach ( $archive_files as $file ) {
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
continue;
}
$uncompressed_size += $file['size'];
$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
}
// 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.
*/
Quick Info
- Hook Type: Filter
- Parameters: 3
- File: wp-admin/includes/file.php
Related Hooks
Related hooks will be displayed here in future updates.