A class for php to generate zip files

Source: Internet
Author: User
Tags ziparchive
A class for php to generate zip files

  1. /*
  2. By: Matt Ford
  3. Purpose: Basic class to create zipfiles
  4. */
  5. Class zipFile {
  6. Public $ files = array ();
  7. Public $ settings = NULL;
  8. Public $ fileInfo = array (
  9. "Name" => "",
  10. "NumFiles" => 0,
  11. "FullFilePath" => ""
  12. );
  13. Private $ fileHash = "";
  14. Private $ zip = "";
  15. Public function _ construct ($ settings ){
  16. $ This-> zipFile ($ settings );
  17. }
  18. Public function zipFile ($ settings ){
  19. $ This-> zip = new ZipArchive ();
  20. $ This-> settings = new stdClass ();
  21. Foreach ($ settings as $ k => $ v ){
  22. $ This-> settings-> $ k = $ v;
  23. }
  24. }
  25. Public function create (){
  26. $ This-> fileHash = md5 (implode (",", $ this-> files ));
  27. $ This-> fileInfo ["name"] = $ this-> fileHash. ". zip ";
  28. $ This-> fileInfo ["numFiles"] = count ($ this-> files );
  29. $ This-> fileInfo ["fullFilePath"] = $ this-> settings-> path. "/". $ this-> fileInfo ["name"];
  30. If (file_exists ($ this-> fileInfo ["fullFilePath"]) {
  31. Return array (
  32. False,
  33. "Already created:". $ this-> fileInfo ["fullFilePath"]
  34. );
  35. }
  36. Else {
  37. $ This-> zip-> open ($ this-> fileInfo ["fullFilePath"], ZIPARCHIVE: CREATE );
  38. $ This-> addFiles ();
  39. $ This-> zip-> close ();
  40. Return array (
  41. True,
  42. "New file created:". $ this-> fileInfo ["fullFilePath"]
  43. );
  44. }
  45. }
  46. Private function addFiles (){
  47. Foreach ($ this-> files as $ k ){
  48. $ This-> zip-> addFile ($ k, basename ($ k ));
  49. }
  50. }
  51. }
  52. $ Settings = array (
  53. "Path" => dirname (_ FILE __)
  54. );
  55. $ ZipFile = new zipFile ($ settings );
  56. $ ZipFile-> files = array (
  57. "./Images/navoff.jpg ",
  58. "./Images/navon.jpg"
  59. );
  60. List ($ success, $ error) = $ zipFile-> create ();
  61. If ($ success === true ){
  62. // Success
  63. }
  64. Else {
  65. // Error because: $ error
  66. }
  67. ?>


Php, zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.