+ // 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;