Home > Backend Development > PHP Tutorial > Examples of PclZip compression and decompression class

Examples of PclZip compression and decompression class

WBOY
Release: 2016-07-25 09:11:03
Original
970 people have browsed it
  1. require_once('pclzip.lib.php');
  2. $zip = new PclZip("archive.zip");
  3. $v_list = $zip->create($_SERVER[' DOCUMENT_ROOT'] ,PCLZIP_OPT_REMOVE_PATH,
  4. $_SERVER['DOCUMENT_ROOT']);
  5. if($v_list == 0){ echo 'Exception:'.$z->errorInfo(true); }
  6. else { echo 'Backup successful '; }
  7. ?>
Copy code

Other usage examples:

  1. //Extract to the directory extract/folder/

  2. $list = $archive->extract(PCLZIP_OPT_PATH, "extract/folder/");< ;/p>
  3. //Add this directory to the compressed file. After completion, there will be a backup directory in the compressed file, and there will be these two files in the backup

  4. $list = $archive->create(" file.txt,image.gif",PCLZIP_OPT_ADD_PATH, "backup");

  5. //Remove part of the path, it will become test/file.txt after completion

  6. $list = $ archive->add("/usr/local/user/test/file.txt",PCLZIP_OPT_REMOVE_PATH,
  7. "/usr/local/user");

  8. //Add all paths Remove it. After this compressed file is created, it will only contain file.txt and image.gif, and there will be no directory.

  9. $list = $archive->create("data/file.txt images/image.gif" ,PCLZIP_OPT_REMOVE_ALL_PATH);

  10. //Set the CHMOD of the decompressed file to 0777

  11. $list = $archive->extract(PCLZIP_OPT_SET_CHMOD, 0777);

  12. < ;p>//Decompress part of the file, this parameter is determined by the file name
  13. //The argument can use the following array
  14. $rule_list[0] = 'test/aaa.txt';
  15. $rule_list[1] = 'test/ddd.txt';
  16. //Or as follows, in a string, use commas to separate each file to be decompressed
  17. $rule_list = "test/aaa.txt,test/ddd.txt";
  18. $list = $archive->extract(PCLZIP_OPT_BY_NAME,$rule_list);

  19. //Extract some files, use php’s ereg() function, and the file names are compared Successful ones will be decompressed

  20. $list = $archive->extract(PCLZIP_OPT_BY_EREG, "aa");

  21. //To decompress some files, use PHP's preg_match() function formula, file names that are successfully compared will be decompressed

  22. $list = $archive->extract(PCLZIP_OPT_BY_PREG, "/^bb/");
  23. //If you don’t understand the above two functions, please first Study regular expression (Regular Expression)

  24. //Decompress according to the index of the elements in the array, but I don’t quite understand what index is = =a

  25. $list = $archive->extract( PCLZIP_OPT_BY_INDEX, array('0-1','6-7'));

  26. //Extract the contents of an archive into a string

  27. $list = $archive->extract (PCLZIP_OPT_BY_NAME, "data/readme.txt",
  28. PCLZIP_OPT_EXTRACT_AS_STRING);

  29. //Decompress the contents of a file and output it directly (echo)

  30. $list = $archive->extract (PCLZIP_OPT_BY_NAME, "data/readme.txt",
  31. PCLZIP_OPT_EXTRACT_IN_OUTPUT);

  32. //Add a file to a compressed file, but the file will not be compressed

  33. $list = $archive- >add("data/file.txt", PCLZIP_OPT_NO_COMPRESSION);

  34. //Add a comment to this compressed file. If there is an comment originally, it will be overwritten

  35. $list = $ archive->create("data", PCLZIP_OPT_COMMENT, "Add a comment");

  36. //Add a comment to this compressed file. If there is already a comment, it will be followed

  37. $list = $archive->add("data", PCLZIP_OPT_ADD_COMMENT, "Add a comment after
  38. the existing one");

  39. //Add a comment to this compressed file, if the original If there are comments, they will be placed in front of the original comments

  40. $list = $archive->add("data", PCLZIP_OPT_PREPEND_COMMENT, "Add a comment before
  41. the existing one");
  42. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template