_unzip_file_ziparchive

The timeline below displays how wordpress function _unzip_file_ziparchive has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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_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 && !in_array($parent_folder, $needed_dirs, true)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            $z->close();
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), $_dir);
        }
    }
    /**
     * Filters archive unzipping to override with a custom process.
     *
     * @since 6.4.0
     *
     * @param null|true|WP_Error $result         The result of the override. True on success, otherwise WP Error. Default null.
     * @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 full list of required folders that need to be created.
     * @param float              $required_space The space required to unzip the file and copy its contents, with a 10% buffer.
     */
    $pre = apply_filters('pre_unzip_file', null, $file, $to, $needed_dirs, $required_space);
    if (null !== $pre) {
        // Ensure the ZIP file archive has been closed.
        $z->close();
        return $pre;
    }
    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_ends_with($info['name'], '/')) {
            // Directory.
            continue;
        }
        if (str_starts_with($info['name'], '__MACOSX/')) {
            // Don't extract the OS X-created __MACOSX directory files.
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            $z->close();
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            $z->close();
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    /**
     * Filters the result of unzipping an archive.
     *
     * @since 6.4.0
     *
     * @param true|WP_Error $result         The result of unzipping the archive. True on success, otherwise WP_Error. Default true.
     * @param string        $file           Full path and filename of ZIP archive.
     * @param string        $to             Full path on the filesystem the archive was extracted to.
     * @param string[]      $needed_dirs    A full list of required folders that were created.
     * @param float         $required_space The space required to unzip the file and copy its contents, with a 10% buffer.
     */
    $result = apply_filters('unzip_file', true, $file, $to, $needed_dirs, $required_space);
    unset($needed_dirs);
    return $result;
}

WordPress Version: 6.3

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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_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) {
            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);
        }
    }
    /*
     * 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 && $uncompressed_size * 2.1 > $available_space) {
            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 && !in_array($parent_folder, $needed_dirs, true)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), $_dir);
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        $info = $z->statIndex($i);
        if (!$info) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if (str_ends_with($info['name'], '/')) {
            // Directory.
            continue;
        }
        if (str_starts_with($info['name'], '__MACOSX/')) {
            // Don't extract the OS X-created __MACOSX directory files.
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 6.1

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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_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) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // 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 ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, skip it.
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) !== $parent_folder && !in_array($parent_folder, $needed_dirs, true)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), $_dir);
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        $info = $z->statIndex($i);
        if (!$info) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files.
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.8

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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_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) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // 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 ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, skip it.
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) !== $parent_folder && !in_array($parent_folder, $needed_dirs, true)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        $info = $z->statIndex($i);
        if (!$info) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files.
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.5

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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_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) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // 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 ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, skip it.
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs, true)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        $info = $z->statIndex($i);
        if (!$info) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files.
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.4

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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
 * @see unzip_file()
 * @access private
 *
 * @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_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) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // 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 ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, skip it.
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        $info = $z->statIndex($i);
        if (!$info) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // Directory.
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files.
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.3

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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
 * @see unzip_file()
 * @access private
 *
 * @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 array $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_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) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // 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 ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        $info = $z->statIndex($i);
        if (!$info) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.1

/**
 * Attempts to unzip an archive using the ZipArchive class.
 *
 * 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
 * @see unzip_file()
 * @access private
 *
 * @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 array $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_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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.9

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        // Don't extract invalid files:
        if (0 !== validate_file($info['name'])) {
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .10

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.8

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.7

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 6.7

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 6.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 6.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .10

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.6

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' === substr($info['name'], -1)) {
            // Directory.
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } elseif ('.' !== $dirname = dirname($info['name'])) {
            // Path to a file.
            $needed_dirs[] = $to . untrailingslashit($dirname);
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 5.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .10

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .11

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 3.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 3.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 3.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .12

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @global WP_Filesystem_Base $wp_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 array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 2.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 2.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 2.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .16

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 4.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        // Only check to see if the Dir exists upon creation failure. Less I/O this way.
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 1.5

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .40

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 1.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 1.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 1.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .19

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 0.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 0.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .20

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 0.2

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .19

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 8.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 8.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .22

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 7.5

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .40

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 7.4

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .30

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 7.3

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: .22

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        if (0 !== validate_file($info['name'])) {
            return new WP_Error('invalid_file_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}

WordPress Version: 3.7

/**
 * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @see unzip_file
 * @access private
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @param array $needed_dirs A partial list of required folders needed to be created.
 * @return mixed WP_Error on failure, True on success
 */
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++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Skip the OS X-created __MACOSX directory
            continue;
        }
        $uncompressed_size += $info['size'];
        if ('/' == substr($info['name'], -1)) {
            // directory
            $needed_dirs[] = $to . untrailingslashit($info['name']);
        } else {
            $needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
        }
    }
    /*
     * 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 (defined('DOING_CRON') && DOING_CRON) {
        $available_space = @disk_free_space(WP_CONTENT_DIR);
        if ($available_space && $uncompressed_size * 2.1 > $available_space) {
            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 (strpos($dir, $to) === false) {
            // If the directory is not within the working directory, Skip it
            continue;
        }
        $parent_folder = dirname($dir);
        while (!empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs)) {
            $needed_dirs[] = $parent_folder;
            $parent_folder = dirname($parent_folder);
        }
    }
    asort($needed_dirs);
    // Create those directories if need be:
    foreach ($needed_dirs as $_dir) {
        if (!$wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && !$wp_filesystem->is_dir($_dir)) {
            // Only check to see if the Dir exists upon creation failure. Less I/O this way.
            return new WP_Error('mkdir_failed_ziparchive', __('Could not create directory.'), substr($_dir, strlen($to)));
        }
    }
    unset($needed_dirs);
    for ($i = 0; $i < $z->numFiles; $i++) {
        if (!$info = $z->statIndex($i)) {
            return new WP_Error('stat_failed_ziparchive', __('Could not retrieve file from archive.'));
        }
        if ('/' == substr($info['name'], -1)) {
            // directory
            continue;
        }
        if ('__MACOSX/' === substr($info['name'], 0, 9)) {
            // Don't extract the OS X-created __MACOSX directory files
            continue;
        }
        $contents = $z->getFromIndex($i);
        if (false === $contents) {
            return new WP_Error('extract_failed_ziparchive', __('Could not extract file from archive.'), $info['name']);
        }
        if (!$wp_filesystem->put_contents($to . $info['name'], $contents, FS_CHMOD_FILE)) {
            return new WP_Error('copy_failed_ziparchive', __('Could not copy file.'), $info['name']);
        }
    }
    $z->close();
    return true;
}