Zip compression and decompression Functions

Source: Internet
Author: User
Tags ziparchive
Provides various official and user-released code examples. For code reference, you are welcome to learn about the zip compression and decompression functions.
Function ezip ($ zip, $ hedef = ''){
$ Dirname = preg_replace ('/. zip/', '', $ zip );
$ Root = $ _ SERVER ['document _ root']. '/zip /';
// Echo $ root. $ zip;
$ Zip = zip_open ($ root. $ zip );
// Var_dump ($ zip );
@ Mkdir ($ root. $ hedef. $ dirname. '/'. $ zip_dosya );
While ($ zip_icerik = zip_read ($ zip )){

$ Zip_dosya = zip_entry_name ($ zip_icerik );
If (strpos ($ zip_dosya ,'.')){
$ Hedef_yol = $ root. $ hedef. $ dirname. '/'. $ zip_dosya;
@ Touch ($ hedef_yol );
// Echo $ hedef_yol;
$ Yeni_dosya = @ fopen ($ hedef_yol, 'W + ');
@ Fwrite ($ yeni_dosya, zip_entry_read ($ zip_icerik ));
@ Fclose ($ yeni_dosya );
// $ Yeni_dosya;

} Else {
@ Mkdir ($ root. $ hedef. $ dirname. '/'. $ zip_dosya );
// Echo $ root. $ hedef. 'x/'. $ zip_dosya;
};
};
}
// Ezip('yuol.zip ','./tr /');

Function zip ($ path ){
$ Path = preg_replace ('/\/$/', '', $ path );
Preg_match ('/\/([\ d \ D] [^ \/] *) $/', $ path, $ matches, PREG_OFFSET_CAPTURE );
$ Filename = $ matches [1] [0]. ". zip ";
// Var_dump ($ filename );
// Set_time_limit (0 );
$ Zip = new ZipArchive ();
$ Zip-> open ($ filename, ZIPARCHIVE: OVERWRITE); // return;
// Var_dump ($ path );
If (is_file ($ path )){
$ Path = preg_replace ('// \//', '/', $ path );
$ Base_dir = preg_replace ('/\/[\ d \ D] [^ \/] * $/', '/', $ path );
$ Base_dir = addcslashes ($ base_dir ,'/:');

$ Localname = preg_replace ('/'. $ base_dir. '/', '', $ path );
// Var_dump ($ localname );
$ Zip-> addFile ($ path, $ localname );
// Var_dump ($ path );
$ Zip-> close ();
Return;
} Elseif (is_dir ($ path )){
$ Path = preg_replace ('/\/[\ d \ D] [^ \/] * $/', '', $ path );
$ Base_dir = $ path. '/'; // base Directory
$ Base_dir = addcslashes ($ base_dir ,'/:');
// Var_dump ($ base_dir );
}
$ Path = preg_replace ('// \//', '/', $ path );
// Var_dump ($ path );
Function addItem ($ path, & $ zip, & $ base_dir ){
// Var_dump ($ path );
$ Handle = opendir ($ path );
// Var_dump ($ path );
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = '.') & ($ File! = '..')){
// Var_dump ($ file );
$ Ipath = $ path. '/'. $ file;
If (is_file ($ ipath) {// The entry is a file
$ Localname = preg_replace ('/'. $ base_dir. '/', '', $ ipath );
Var_dump ($ localname );
$ Zip-> addFile ($ ipath, $ localname );
// Var_dump ($ r );
} Else if (is_dir ($ ipath )){
AddItem ($ ipath, $ zip, $ base_dir );
$ Localname = preg_replace ('/'. $ base_dir. '/', '', $ ipath );
Var_dump ($ localname );
$ Zip-> addEmptyDir ($ localname );
}
// Var_dump ($ path );
}
}
}
// Var_dump ($ base_dir );
AddItem ($ path, $ zip, $ base_dir );
$ Zip-> close ();
}
Zip ('d:/wamp/www/sample/main /');

Zip.zip (1.03 KB download: 310 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.