PHP
- 11/02/2010
Compress files with PHP
Hello, today I will show you how to compress file with PHP. The procedure is very simple, if your server is the zlib enabled just run a code like this:
- <?
- / **
- * Inserts a file within a zip.
- *
- * @ Param string $ name name_of_zip ZIP file that will be worked.
- * @ Param string $ path_file Path original file that goes to the ZIP
- * @ Param string $ file_name Name of file within the ZIP
- * @ Return Mixed
- * /
- $name_of_zip = null , $path_file = null , $file_name = null ) { fileToZip function ($ name_of_zip = null, $ path_file = null, $ file_name = null) {
- ; return 'All parameters are required';
- }
- / / Create the instance of ZIP
- ZipArchive; $ Zip = new ZipArchive;
- / / If unable to create the ZIP file
- $zip -> open ( $name_of_zip , ZIPARCHIVE:: CREATE ) === true ) { if ($ zip -> open ($ name_of_zip, ZIPARCHIVE :: CREATE) === true) {
- / / Add the file inside the zip
- ( $path_file , $file_name ) ; $ Zip -> AddFile ($ path_file, $ file_name);
- ( ) ; // fecha a conexão com o ZIP $ Zip -> close () / / close connection with the ZIP
- / / Optionally you can delete the original file, just insert a variable in the parameters
- / / Unlink ('/ path / to / file / file.php');
- ; return true;
- { Else {}
- ; return false;
- }
- }
- ?>
This post is a complement to the How to unzip files with PHP and together we can turn a very useful class.
Let's see if it works? I'll play this role and function of the previous post into a file and run to see if the file was created correctly:
- <?
- / **
- * Inserts a file within a zip.
- *
- * @ Param string $ name name_of_zip ZIP file that will be worked.
- * @ Param string $ path_file Path original file that goes to the ZIP
- * @ Param string $ file_name Name of file within the ZIP
- * @ Return Mixed
- * /
- $name_of_zip = null , $path_file = null , $file_name = null ) { fileToZip function ($ name_of_zip = null, $ path_file = null, $ file_name = null) {
- ; return 'All parameters are required';
- }
- / / Create the instance of ZIP
- ZipArchive; $ Zip = new ZipArchive;
- / / If unable to create the ZIP file
- $zip -> open ( $name_of_zip , ZIPARCHIVE:: CREATE ) === true ) { if ($ zip -> open ($ name_of_zip, ZIPARCHIVE :: CREATE) === true) {
- / / Add the file inside the zip
- ( $path_file , $file_name ) ; $ Zip -> AddFile ($ path_file, $ file_name);
- ( ) ; // fecha a conexão com o ZIP $ Zip -> close () / / close connection with the ZIP
- / / Optionally you can delete the original file, just insert a variable in the parameters
- / / Unlink ('/ path / to / file / file.php');
- ; return true;
- { Else {}
- ; return false;
- }
- }
- / **
- * This function decompresses files
- * A zip
- *
- * @ Param string $ path File Location. Zip
- * @ Param string $ pathunzip folder where files should be unpacked
- * /
- $path , $pathunzip = '.' ) { function unzip ($ path, $ pathunzip = '.') {
- # === - Instantiates the class of Zip
- ZipArchive; $ Zip = new ZipArchive;
- # === - Try to open the zip
- $zip -> open ( $path ) ) { if ($ zip -> open ($ path)) {
- -> extractTo ( $pathunzip ) ; // executa o unzip $ Return = $ zip -> extractTo ($ pathunzip) / / run the unzip
- ( ) ; // fecha a coneção com o .zip $ Zip -> close () / / close the connection or with. Zip
- { Else {}
- }
- }
- / **
- * This function returns an array with
- * File names that are
- * In the zip.
- *
- * @ Param string $ path File Location. Zip
- * @ Return array or false on error
- * /
- $path ) { zipToArray function ($ path) {
- # === - Instantiates the class
- ZipArchive; $ Zip = new ZipArchive;
- # ==== - Try to open the zip
- $zip -> open ( $path ) ) { if ($ zip -> open ($ path)) {
- # === - Retrieves the number of zip files
- -> numFiles ; Num_files $ = $ zip -> numFiles;
- # === - Runs the files taking names and putting in an array
- $i = 0 ; $i <= ( $num_files ) -1 ; $i ++ ) { for ($ i = 0; $ i <= ($ num_files) -1; $ i + +) {
- = $zip -> getNameIndex ( $i ) ; $ Output [] = $ zip -> getNameIndex ($ i);
- }
- # ==== - Closes the connection
- ( ) ; $ Zip -> close ();
- # ==== - Retora the array to be manipulated
- ; return $ output;
- }
- ; return false;
- }
- / / Adding files
- , './texto.txt' , 'texto.txt' ) ; fileToZip ('. / nome_do_zip.zip', '. / text.txt', 'text.txt');
- , './texto.txt' , 'texto2.txt' ) ; fileToZip ('. / nome_do_zip.zip', '. / text.txt', 'texto2.txt');
- / / Reading the zip file created to see if there is any file within
- ?>
See the result:
I'll do a post on how to create a class with these features soon.
For today it is ...




4 Comments to "Compress files with PHP"
Marcelão good ... as always blasting in posts!
So, had spoken to had a generic class, but is not .. lol .. not generic but as it is a little different than yours, follow the code there for suggestions and improvements ... is working fine ... but I want to work with the correct ideology OO ... so help me! : P
<Php
/ **
* Class Zip - To zip pictures of a river diretï ¿½
*
* @ Author Rafael Ortega Bueno
* @ Parameter $ path = folder path
* @ Parameter name = $ name the new file
** /
{class Zipador
public function zip ($ path, $ folder) {
$ Path = $ path. "/";
$ Size = strlen ("$ folder");
for ($ i = 0; $ i <$ size, $ i + +) {
if (substr ($ folder, $ i, 1) == "/") {
$ FileName = substr ($ folder ($ i +1), $ size);
}
}
/ / - Above Jhonatan
$ Dir = opendir ($ path);
$ X = Array ();
while ($ file = readdir ($ dir)) {
array_push ($ x, $ file);
}
for ($ i = 0; iopen $ ("$ {new_name}. zip" ZIPARCHIVE :: CREATE) === true) {
$ Zip-> AddFile ($ path. $ X [$ i], $ x [$ i]);
$ Zip-> close ();
print "File successfully zipped";
}
else {
print "Problems zip your file";
}
}
}
return " Download File ";
/ / Echo "location.href = '$ new_name.zip'";
}
/ / ------------
public function buscaNomePasta ($ id) {
$ Database = mysql_query ("SELECT FROM file sending
WHERE idenvio = $ id ");
while ($ row = mysql_fetch_array ($ database)) {
$ Folder = $ row ['file'];
}
return $ folder;
}
}
?>
By Rafael Ortega Bueno (6 comments) on 12/02/2010
Can you clarify a question?
How do I create the file in multiple parts?
I have a cron that generates a backup (bd) and then send package was working normally ... ...
but now the backup is approximately 200MB.
and time to compress the error, it does not compress ...
thought to compress into several parts ... do not know if you can ..
thank you
By Laureano (1 comments) on 09/03/2011
guy liked simple and straightforward.
Thank you.
By Joshua (4 comments) on 27/08/2011
<Php
/ / Author: Luis Estelvio
/ / Date: 08/12/2011
/ / Build the path
$ Dwloaddir = "path";
/ / Check if the folder exists, otherwise creates
! Is_dir ($ dwloaddir)? mkdir ($ dwloaddir, 0777): "";
$ File = fopen ($ dwloaddir. "/". "Teste_1.txt", "w");
for ($ i = 0; $ i <10; $ i + +) fwrite ($ file, "testing testing testing testing \ n");
fclose ($ file);
$ File = fopen ($ dwloaddir. "/". "Teste_2.txt", "w");
for ($ i = 0; $ i open ($ filename, ZIPARCHIVE :: CREATE)! == TRUE) {
exit ("Could not open file \ n");
}
$ Zip-> AddFile ($ dwloaddir. "/ Teste_1.txt", "teste_1.txt");
$ Zip-> AddFile ($ dwloaddir. "/ Teste_2.txt", "teste_2.txt");
$ Zip-> close ();
?>
For Estelvio (1 comments) in 12/06/2011