PHP - Multi Compression ( ZIP )

Votes : 3.9
2759

Already PHP had files compressing and extraction methods and also easy to use them. But if you want to more than one file or directories to compressing, it is not simple. You have to prepare some extra script ( Multi Compression ) to capture files and sub folders inside of compressing folder and also binding into a compressing file. Now are days it's not a big deal.

Extremely easy to compress and extract multi directories or files in PHP by using this source script.

Nothing complex to attache this script plugin into your php projects and code implementation very easily to understand. We found most common some mistakes, most real-time software developers made in their codings. We will try to fix most of them automatically in our source script. You do not worry about them anymore.

Finally, You can modify this plugin, If you want. We explain A to Z how work with this plugin in this documentation.

In this php script ,

  1. Plug-in into your site, very easily. This php script compatible with PHP 4+
  2. Compression and extraction multi directories or files.
  3. Can be added to compressed directory's files recursively.
  4. Override or Clear before extraction's directory.
  5. Errors auto handling.

So, Why do you wasting time. Read this documentation step by step. It would be more helpful.

Download ES Multi Compression ( ZIP ) - PHP source script.

Contributing

ES Multi Compression ( ZIP ) - PHP source script free way to download. click here.

Code Documentation

There is two kind of files contain in ES Multi Compression ( ZIP ) source script zip file.

  1. compress.php
  2. extract.php

How to compress file or directories?

Copy compress.php into your project. Then attached compress.php file into your project file by using include or require method. My recommendation is require_once method.

require_once 'compress.php'; //or
include_once 'compress.php';

Initialize compression object in PHP.

$zip = new ESCompress(); /* initialized ESCompress compression object. */

Use add_file ( ) function to attache a file into compression by giving absolute or relative file path.

$result = $zip->add_file('sample1.txt');  // relative file path
$result = $zip->add_file('E:\folder\sample3.txt','re_sample1.txt');  // sample3.txt file attache into compression file with new file name.

$result = $zip->add_file('E:\sample2.txt'); // absolute file path
$result = $zip->add_file('E:\folder\sample4.txt','sample/re_sample2.txt'); //sample4.txt file attache into compression file with new file path.

Use add_folder ( ) function to attache a directory into compression by using absolute or relative folder path.

$result = $zip->add_folder('sample1');   // relative folder path
$result = $zip->add_folder('E:\folder\sample3','re_sample1');   // sample3 folder attache into compression file with new folder name.

$result = $zip->add_folder('E:\sample2');  // absolute folder path
$result = $zip->add_folder('E:\folder\sample4','folder/re_sample2');   // sample4 folder attache into  compression file with new folder path.

How can i overwrite compression file, If the compression zip file already exist?

Normally, If compression file will be exist already, overwrite it. But if you change compression type, You can use overwrite ( ) function to reverse again to overwrite .

$zip->overwrite();

If already exist compressed zip file. How can i remove it, before compression ?

Sometimes, do not need to overwrite compression file, maybe want to clear before compression, if compression file will be exist. Use as_new ( ) function to change compression type.

$zip->as_new();

Finally how can compressed attached files and folder ?

It's very simple,Use compress ( ) function to save into given absolute or relative file path.

$zip->compress('test.zip');

/* And also can be attached file or folder as secondary parameter. */
$zip->compress('test.zip','E:\folder\sample5'); // Attached single folder by using absolute or relative path as string
$zip->compress('test.zip','E:\folder\sample6.txt'); // Attached single file by using absolute or relative path as  string
$zip->compress('test.zip',array('E:\folder\sample6','E:\sample5.txt'));// Attached files or folders  as string path array.

How to extract zip file?

Copy compress.php file into your project. Best way to attached compress.php file into your project file by using require method or include method.

require_once 'extract.php'; //or
include_once 'extract.php';

Initialize extraction object in PHP.

$extract = new ESextract(); /* initialized ESextract extracted object. */

How can be extracted given compression file.

It's not a big deal. But do you need to know before extract compression file. If already exist extracted folder, overwrite some files and sub-folders when they have same paths inside of both extracted folder and compression file. and others are copy into extracted folder. other files and sub-folders, already exist inside of extracted folder nothing happen.

if you do not care about overwrite, It's ok. You can use basic function, But if you want clear before extraction. you must need to give extra parameter into this function.

$extract->extract('compress.zip'); // Extract compression file into   extrac.php file's location.
$extract->extract(array('sample/compress.zip','test.zip'),'E:\sample');  //Multipal compression file  extract into a same given folder path.
$extract->extract('E:\sample/compress.zip','sample',true); // clear all files and sub-folders inside sample folder ( extraction folder ), before extraction.

Summery

Compress attached file and folders

Need to file and folders into compression file and also if compression file exist, do not overwrite.

By default if compression file already exist, overwrite exist file. In this case, Overwrite exist file from new file inside of compression file, When some file paths are match both existing file and new file.

But compression file should be has only currently attached folders and files only.

require_once 'compress.php'; //Include source script library compression file into your project.
$zip = new ESCompress(); // initialized ESCompress compression object.
$result = $zip->add_file('sample1.txt');  // sample1.txt file attached into compression file.
if(!$result){
    echo 'file not exist.';
}
$result = $zip->add_folder('E:\sample2'); //sample2 folder attached into compression file.
if(!$result){
    echo 'Not exist or cannot read sub-folder and files inside of sample2 folder.';
}
$zip->as_new(); // If compression file exist, clear all content of compression file.
$zip->compress('test.zip'); // Compress by using given path.

Extract compression file into given folder

Eq: E:\sample\test.zip compression file need to extract into E:\sample2 folder.

E:\sample2 folder and also contained sub-folder and files, If those are exist before extraction.

By default, If already exist extracted folder, overwrite some files and sub-folders when they have same paths inside of both extracted folder and compression file. other files and sub-folders, inside of extracted folder nothing happen.

But need to clear, if given extraction folder will be exist before extraction.

require_once 'extract.php'; // Include source script library extraction file into your project.
$extract = new ESextract(); // initialized ESextract extraction object.
$extract->extract(array('sample/compress.zip'), 'sample', true);// compression.zip file extract into sample folder.

Be carefully, for mulitifal compression. Look this example

require_once 'compress.php'; //Include source script library compression file into your project.
$zip = new ESCompress(); // initialized ESCompress compression object.
$zip->add_file('sample\sample.txt'); // This attched file path inside of compression file as 'sample\sample.txt'.
$zip->add_file('E:\sample.txt','sample\sample.txt'); // This attched file path inside of compression file as 'sample\sample.txt'.
$result = $zip->add_folder('E:\sample2','sample'); // All sub-folders and files attached into 'sample' inside of compression file. 
$zip->compress('test.zip'); // Compress by using given path.

Try to compress,

.1 'E:\sample.txt' file
.2 'sample\sample.txt' file
.3 'E:\sample2' folder

'sample2' folder has 'sample.txt' file. This file will be attached into compression file too.

Let see, What happen, after compression.

You can see, 'sample' folder already created but inside of 'sample' folder. There has only one file. But there need contain 3 file. What happen to others?

.1 'E:\sample.txt' file - 'sample\sample.txt'
.2 'sample\sample.txt' file - 'sample\sample.txt'
.3 'E:\sample2' folder - 'sample'
.4 'sample.txt' file, Inside of 'sample2' folder - 'sample\sample.txt'

Lets see highlighted paths, Those are attached files and sub-folders path inside of compression file. All file paths are same. Then overwrite each other, when attached into compression file, try to get 'sample\sample.txt' path.

asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila