RE: [PHP] Extracting Compressed Files

From: Date: Tue, 05 Aug 2003 16:34:24 +0000
Subject: RE: [PHP] Extracting Compressed Files
Groups: php.general 
Request: Send a blank email to php-general+get-158311@lists.php.net to get a copy of this message
Hey Matt, Below is a cut down version of how I'm doing it. I lifted most of it right out of the manual examples. It assumes a single entry in each compressed file. function uncompress($filepath, $new_filename, $compression) { if($compression == COMPRESSION_GZIP) { $zp = gzopen( $filepath, "r" ); $fp = fopen( $new_filename, "w"); if(! $fp ) { return BDBERR_TITLE_GZ_FILE_COPY_FAILED; } $buffer = ''; if( $zp ) { while(! gzeof($zp)) { $buffer = gzgets ($zp, 4096); if( $buffer === FALSE) { fclose($fp); unlink($new_filename); return BDBERR_TITLE_GZ_FILE_COPY_FAILED; } fputs($fp, $buffer); } gzclose( $zp ); fclose( $fp ); } else { return BDBERR_TITLE_GZ_FILE_COPY_FAILED; } return SUCCESS; } else if($compression == COMPRESSION_BZIP2) { $bz_fp = fopen( $filepath, "r"); $bzipped_contents = fread ($bz_fp, filesize($filepath) ); fclose($bz_fp); $uncompressed = bzdecompress( $bzipped_contents ); $fp = fopen( $new_filename, "w"); if(! $fp ) { return BDBERR_TITLE_BZ2_FILE_COPY_FAILED; } if( ! fwrite($fp, $uncompressed) ) { return BDBERR_TITLE_BZ2_FILE_COPY_FAILED; } fclose($fp); return SUCCESS; } else if($compression == COMPRESSION_ZIP) { $zip = zip_open($filepath); if ($zip) { $zip_entry = zip_read($zip); if (zip_entry_open($zip, $zip_entry, "r")) { $unzipped_contents = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } zip_close($zip); $fp = fopen( $new_filename, "w"); if(! $fp ) { return BDBERR_TITLE_ZIP_FILE_COPY_FAILED; } if( ! fwrite($fp, $unzipped_contents) ) { return BDBERR_TITLE_ZIP_FILE_COPY_FAILED; } fclose($fp); } else { return BDBERR_TITLE_ZIP_FILE_COPY_FAILED; } } else { return BDBERR_UNKNOWN_COMPRESSION_METHOD; } } -----Original Message----- From: Matt Palermo [mailto:mpalermo@vt.edu] Sent: Monday, August 04, 2003 7:33 PM To: php-general@lists.php.net Subject: [PHP] Extracting Compressed Files Does anyone know of easy ways to be able to extract all files/folders/subfolders from different types of compressed files (.zip, .tar.gz, etc.)? If anyone could help me out with this, I would really appreciate it. Thanks, Matt

« previous php.general (#158311) next »