PHP generated zip compression package ____php

Source: Internet
Author: User
Compress a file

We build a file into a compressed package.

<?php
$path = "C:/wamp/www/log.txt";
$filename = "Test.zip";
$zip = new Ziparchive ();
$zip->open ($filename, ziparchive::create);   Open the compression pack
$zip->addfile ($path, basename ($path));   Add a file to the compressed package
$zip->close ();  Turn off compression packs

The code above generates the C:/wamp/www/log.txt file compression Test.zip and saves it in the current directory. Compress multiple Files

Compressing multiple files, in fact, is addfile execution many times, can be achieved through the traversal of the array.

<?php
$fileList = Array (
    "C:/wamp/www/log.txt",
    "c:/wamp/www/weixin.class.php"
);
$filename = "Test.zip";
$zip = new Ziparchive ();
$zip->open ($filename, ziparchive::create);   Open the compression bundle
foreach ($fileList as $file) {
    $zip->addfile ($file, basename ($file));   Add files to the compressed package
$zip->close ();  Turn off compression packs
Compress a directory
<?php
function Addfiletozip ($path, $zip) {
    $handler =opendir ($path);//Open current folder specified by $path. While
    (($filename =readdir ($handler))!==false) {
        if ($filename!= "." && $filename!= "...") The {//folder file name is '. ' and '.. ', do not manipulate them
            if (Is_dir ($path.) /". $filename)) {//If one of the objects read is a folder, recursion
                addfiletozip ($path." /". $filename, $zip);
            } else{//Add the file to the Zip object
                $zip->addfile ($path.) /". $filename);
    }} @closedir ($path);
}
$zip =new ziparchive ();
if ($zip->open (' Rsa.zip ', ziparchive::overwrite) = = TRUE) {
    addfiletozip (' rsa/', $zip);//calling methods, manipulating the root directory to be packaged, and passes the Ziparchive object to the method
    $zip->close ();//Close the processed zip file
}
Compress and download zip packages

My time, we need to pack, provide downloads, and then delete the zip package.

Can be divided into the following steps: To determine the given path, is a folder, or a file. Folders also need to traverse the add file. Set the related file headers and use the ReadFile function to provide downloads. To delete a compressed package using the Unlink function

<?php function Addfiletozip ($path, $zip) {$handler =opendir ($path);//Open current folder specified by $path. while (($filename =readdir ($handler))!==false) {if ($filename!= "." && $filename!= "...") The {//folder file name is '. ' and '.. ', do not manipulate them if (Is_dir ($path.) /". $filename)) {//If one of the objects read is a folder, recursion Addfiletozip ($path."
            /". $filename, $zip); }else{//Add the file to the Zip object $zip->addfile ($path.)
            /". $filename);
@closedir ($path)}}};
} $zip =new ziparchive ();
    if ($zip->open (' Rsa.zip ', ziparchive::overwrite) = = TRUE) {$path = ' rsa/';
    if (Is_dir ($path)) {//Give a folder, Package folder Addfiletozip ($path, $zip);
        }else if (Is_array ($path)) {//is given the file path foreach ($path as $file) {$zip->addfile ($file) in the form of an array;
    }else{//Only give a file $zip->addfile ($path); } $zip->close (); Closes the processed zip file} 

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.