Compress files with PHP


1 Estrelas2 Estrelas3 Estrelas4 Estrelas5 Estrelas (4 votes, average: 4.00 out of 5)
Loading ... Loading ...

Hello folks, today I will show you how to compress file with PHP. The procedure is very simple, if your server is with zlib enabled just run a code like this:

  1. <?
  2. / **
  3. * Inserts a file within a zip.
  4. *
  5. * @ Param string $ name name_of_zip ZIP file that will be worked.
  6. * @ Param string $ path_file Path original file that goes to the ZIP
  7. * @ Param string $ file_name file name inside the ZIP
  8. * @ Return Mixed
  9. * /
  10. $name_of_zip = null , $path_file = null , $file_name = null ) { fileToZip function ($ name_of_zip = null, $ path_file = null, $ file_name = null) (
  11. empty ( $name_of_zip ) || empty ( $path_file ) || empty ( $file_name ) ) { if ( empty ($ name_of_zip) | | empty ($ path_file) | | empty ($ file_name)) (
  12. ; return 'All parameters are required';
  13. )
  14. / / Create an instance of the ZIP
  15. ZipArchive; $ Zip = new ZipArchive;
  16. / / If unable to create the ZIP file
  17. $zip -> open ( $name_of_zip , ZIPARCHIVE:: CREATE ) === true ) { if ($ zip -> open ($ name_of_zip, ZIPARCHIVE:: CREATE) === true) (
  18. / / Add the file in the zip
  19. ( $path_file , $file_name ) ; $ Zip -> AddFile ($ path_file, $ file_name);
  20. ( ) ; // fecha a conexão com o ZIP $ Zip -> close () / / close the connection to the ZIP
  21. / / Optionally you can delete the original file, just insert a variable in the parameters
  22. / / Unlink ("/ path / to / file / file.php ');
  23. ; return true;
  24. { Else ()
  25. ; return false;
  26. )
  27. )
  28. ?>


This post is a complement of How to unzip files with PHP and together we can turn into a useful class.

Let's see if it works? I will play this role and function of the previous post into a file and run to see if the file was created correctly:

  1. <?
  2. / **
  3. * Inserts a file within a zip.
  4. *
  5. * @ Param string $ name name_of_zip ZIP file that will be worked.
  6. * @ Param string $ path_file Path original file that goes to the ZIP
  7. * @ Param string $ file_name file name inside the ZIP
  8. * @ Return Mixed
  9. * /
  10. $name_of_zip = null , $path_file = null , $file_name = null ) { fileToZip function ($ name_of_zip = null, $ path_file = null, $ file_name = null) (
  11. empty ( $name_of_zip ) || empty ( $path_file ) || empty ( $file_name ) ) { if ( empty ($ name_of_zip) | | empty ($ path_file) | | empty ($ file_name)) (
  12. ; return 'All parameters are required';
  13. )
  14. / / Create an instance of the ZIP
  15. ZipArchive; $ Zip = new ZipArchive;
  16. / / If unable to create the ZIP file
  17. $zip -> open ( $name_of_zip , ZIPARCHIVE:: CREATE ) === true ) { if ($ zip -> open ($ name_of_zip, ZIPARCHIVE:: CREATE) === true) (
  18. / / Add the file in the zip
  19. ( $path_file , $file_name ) ; $ Zip -> AddFile ($ path_file, $ file_name);
  20. ( ) ; // fecha a conexão com o ZIP $ Zip -> close () / / close the connection to the ZIP
  21. / / Optionally you can delete the original file, just insert a variable in the parameters
  22. / / Unlink ("/ path / to / file / file.php ');
  23. ; return true;
  24. { Else ()
  25. ; return false;
  26. )
  27. )
  28. / **
  29. * This function decompresses files
  30. * A zip
  31. *
  32. * @ Param string $ path File Location. Zip
  33. * @ Param string $ pathunzip folder where the files should be unpacked
  34. * /
  35. $path , $pathunzip = '.' ) { function unzip ($ path, $ pathunzip = '.') (
  36. ===- # Instantiates the class Zip
  37. ZipArchive; $ Zip = new ZipArchive;
  38. # Try to open the zip ===-
  39. $zip -> open ( $path ) ) { if ($ zip -> open ($ path)) (
  40. -> extractTo ( $pathunzip ) ; // executa o unzip $ Return = $ zip -> extractTo ($ pathunzip) / / run the unzip
  41. ( ) ; // fecha a coneção com o .zip $ Zip -> close () / / close the conexion with. Zip
  42. { Else ()
  43. ; echo 'The file can not be opened.';
  44. )
  45. )
  46. / **
  47. * This function returns an array with
  48. * File names that are
  49. * In the zip.
  50. *
  51. * @ Param string $ path File Location. Zip
  52. * @ Return array or false on error
  53. * /
  54. $path ) { zipToArray function ($ path) (
  55. # ===- Instantiates class
  56. ZipArchive; $ Zip = new ZipArchive;
  57. # Try to open the zip ====-
  58. $zip -> open ( $path ) ) { if ($ zip -> open ($ path)) (
  59. # ===- Retrieves the number of zip files
  60. -> numFiles ; Num_files $ = $ zip -> numFiles;
  61. # ===- Travels files taking names and putting it in an array
  62. $i = 0 ; $i <= ( $num_files ) -1 ; $i ++ ) { for ($ i = 0, $ i <= ($ num_files) -1; $ i + +) (
  63. = $zip -> getNameIndex ( $i ) ; $ Output [] = $ zip -> getNameIndex ($ i);
  64. )
  65. # ====- Closes the connection
  66. ( ) ; $ Zip -> close ();
  67. # ====- Retore the array to be manipulated
  68. ; return $ output;
  69. )
  70. ; return false;
  71. )
  72. / / Add files
  73. , './texto.txt' , 'texto.txt' ) ; fileToZip ('. / nome_do_zip.zip', '. / Text File.txt', 'Text File.txt');
  74. , './texto.txt' , 'texto2.txt' ) ; fileToZip ('. / nome_do_zip.zip', '. / Text File.txt', 'texto2.txt');
  75. / / Read the zip has created to see if any files within
  76. zipToArray ( './nome_do_zip.zip' ) ) ; print_r (zipToArray ('. / nome_do_zip.zip'));
  77. ?>

See the result:

resultado 300x235 Compactar arquivos com PHP

I'll do a post about creating a class with these features soon.

For today is that ...

  1. 1 Comment to "Compress files with PHP"

  2. Marcelão good ... as always rocking in posts!
    So he had spoken to had made a generic class, but is not generally not .. lol .. but as it is a little different than yours, follow the code and there suggestions for improvement ... is working well ... but I want to work with the ideology proper OO ... so help me! : P

    <? Php
    / **
    * Zip Class - For zip files from a direct ¿½ rio
    *
    * @ 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);
    )

    )

    / / - Up 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 zipping your file";
    )

    )
    )

    return " File Download ";

    / / Echo "location.href = '$ new_name.zip'";

    )

    //------------

    public function buscaNomePasta ($ id) (

    $ Db = mysql_query ("SELECT file FROM sending
    WHERE idenvio = $ id ");

    while ($ row = mysql_fetch_array ($ db)) (

    $ Folder = $ row ['file'];

    )

    return $ folder;

    )
    )

    ?>

    Reply Reply

    By Rafael Ortega Bueno (6 comments) on 12/02/2010

Place a comment

Links in comments should be nofollow free .

Get Adobe Flash player Plugin by wpburn.com wordpress themes