_unzip_file_pclzip

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

Description

_unzip_file_pclzip( (string) $file, (string) $to, (array) $needed_dirs = array() ); 

Attempts to unzip an archive using the PclZip library. Assumes that WP_Filesystem() has already been called and set up.

Parameters (3)

0. $file (string)
Full path and filename of zip archive
1. $to (string)
Full path on the filesystem to extract archive to
2. $needed_dirs — Optional. (array) => array()
A partial list of required folders needed to be created.

Usage

  1. if ( !function_exists( '_unzip_file_pclzip' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/file.php'; 
  3.  
  4. // Full path and filename of zip archive 
  5. $file = ''; 
  6.  
  7. // Full path on the filesystem to extract archive to 
  8. $to = ''; 
  9.  
  10. // A partial list of required folders needed to be created. 
  11. $needed_dirs = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = _unzip_file_pclzip($file, $to, $needed_dirs); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/file.php  
  1. function _unzip_file_pclzip($file, $to, $needed_dirs = array()) { 
  2. global $wp_filesystem; 
  3.  
  4.  
  5. require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); 
  6.  
  7. $archive = new PclZip($file); 
  8.  
  9. $archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING); 
  10.  
  11.  
  12. // Is the archive valid? 
  13. if ( !is_array($archive_files) ) 
  14. return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true)); 
  15.  
  16. if ( 0 == count($archive_files) ) 
  17. return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) ); 
  18.  
  19. $uncompressed_size = 0; 
  20.  
  21. // Determine any children directories needed (From within the archive) 
  22. foreach ( $archive_files as $file ) { 
  23. if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Skip the OS X-created __MACOSX directory 
  24. continue; 
  25.  
  26. $uncompressed_size += $file['size']; 
  27.  
  28. $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) ); 
  29.  
  30. /** 
  31. * disk_free_space() could return false. Assume that any falsey value is an error. 
  32. * A disk that has zero free bytes has bigger problems. 
  33. * Require we have enough space to unzip the file and copy its contents, with a 10% buffer. 
  34. */ 
  35. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
  36. $available_space = @disk_free_space( WP_CONTENT_DIR ); 
  37. if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) 
  38. 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' ) ); 
  39.  
  40. $needed_dirs = array_unique($needed_dirs); 
  41. foreach ( $needed_dirs as $dir ) { 
  42. // Check the parent folders of the folders all exist within the creation array. 
  43. if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist) 
  44. continue; 
  45. if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it 
  46. continue; 
  47.  
  48. $parent_folder = dirname($dir); 
  49. while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) { 
  50. $needed_dirs[] = $parent_folder; 
  51. $parent_folder = dirname($parent_folder); 
  52. asort($needed_dirs); 
  53.  
  54. // Create those directories if need be: 
  55. foreach ( $needed_dirs as $_dir ) { 
  56. // Only check to see if the dir exists upon creation failure. Less I/O this way. 
  57. if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) 
  58. return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) ); 
  59. unset($needed_dirs); 
  60.  
  61. // Extract the files from the zip 
  62. foreach ( $archive_files as $file ) { 
  63. if ( $file['folder'] ) 
  64. continue; 
  65.  
  66. if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files 
  67. continue; 
  68.  
  69. if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE) ) 
  70. return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] ); 
  71. return true;