Php zip Application-php Tutorial

Source: Internet
Author: User
Tags ziparchive
Php: use *** to create a zip package * @ paramarray $ arr to compress partial paths and file names, such as array (* fileupload1.php, * fileupload20111.jpg ,*) ** @ returnstring: part of the path and file name of the compressed package. php uses zip.
/***** Create a compressed package * @ param array $ arr partial paths and file names to be compressed, such as array (* '/file/upload/1. php ', *'/file/upload/2011/1 .jpg ', *) ** @ return part of the path and file name of the string compressed package, for example,/file/uplaod/zip/123.zip */public static function zip ($ arr) {// first, I create a directory $ save_path = '/file/upload/zip '. dat: getHourPath (); $ path = APPLICATION_PATH. '/destoon '. $ save_path; Sys: createDir ($ path); $ zipname = strval (time ()). mt_rand (100000,999 9 99 ). '.zip '; $ zip_fullname = $ path. '/'. $ zipname; $ zip_obj = new ZipArchive (); if ($ zip_obj-> open ($ zip_fullname, ZIPARCHIVE: CREATE )! = True) {return false;} foreach ($ arr as $ value) {// $ zip-> addFile ($ path, mb_convert_encoding (basename ($ path ), 'gbk', 'utf-8'); $ zip_obj-> addFile (APPLICATION_PATH. '/destoon '. $ value, basename ($ value);} $ zip_obj-> close (); return $ save_path. '/'. $ zipname;}/*** decompress ** @ param string $ zipname part of the path and file name of the compressed package, like '/file/upload/1.zip' * @ param string $ path: the full path of the decompressed folder ** @ return array Array, partial path and file name, such as array (* '/file/1. php ', *'/file/12.png ', *) */public static function unzip ($ zipname, $ path = '') {if (! $ Path) {$ path = '/file/tempzip_folder /'. strval (time ()). mt_rand (100000,999 999); $ newpath = APPLICATION_PATH. '/destoon '. $ path;} else {$ newpath = $ path; $ path = preg_replace ('# ^. +/destoon (. +) $ # ',' $ 1', $ newpath);} Sys: createDir ($ newpath); $ filename = APPLICATION_PATH. '/destoon '. $ zipname; // Extract $ zip = new ZipArchive (); $ arr = array (); if ($ zip-> open ($ filename) === true) {$ zip-> e Xtraceid ($ newpath); $ zip-> close (); $ temp = scandir ($ newpath); foreach ($ temp as $ value) {if ($ value! = '.' & $ Value! = '..') {$ Arr [] = $ path. '/'. basename ($ value) ;}} return $ arr ;}

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.