About Zip file in PHP

How to create zip file using PHP
// Creating object of the ZipArchive
$zip = new ZipArchive();

$ow = 1;
$file= “master.zip”;
if($zip->open($file,$ow?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE)
{
// Add the files to the .zip file
$zip->addFile(“master.css”);

// This means style.css will be added as new_style.css
$zip->addFile(“style.css”, “new_style.css”);

$zip->addFile(“fonts.css”);

// Closing the zip file
$zip->close();

// Above code will generate master.zip
// containing master.css, new_style.css, fonts.css
}

Extract Zip file using PHP
// Creating object of the ZipArchive
$zip = new ZipArchive();

// Open master.zip for extracting all files
if ($zip->open(“master.zip”) === TRUE)
{
// Will extract all files from master.zip to given path.
$zip->extractTo(“/path/to/folder/”);
$zip->close();
}

// Extract only several files

// Open master.zip for extracting single files
if ($zip->open(“master.zip”) === TRUE)
{
// Will extract only fonts.css from master.zip to given path.
$zip->extractTo(“/path/to/folder/”,”fonts.css”);
$zip->close();
}

// Open master.zip for extracting multiple files
if ($zip->open(“master.zip”) === TRUE)
{
// Will extract only fonts.css and master.css
// from master.zip to given path.
$files = array(“fonts.css”,”master.css”);
$zip->extractTo(“/path/to/folder/”,$files);
$zip->close();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s