Duniaweb Blog

Your Interner Life Partner

ZIP Archive Extractor and Creator

Membahas beberapa artikel di beberapa forum dan setelah menerima beberapa email tentang cara mengextract dan membuat file ZIP menggunakan PHP, penulis berkeinginan untuk membagi file projek ini.

Berikut ini adalah script php yg gw pake. Script PHP ini dapat mengextract dan membuat file ZIP sndiri. Gw bikinin dalam format Form yak !

pertama dalam PHP kita butuh file lib untuk mengeksekusi proses ZIP ini. File Lib ini adalah PclZIP.php
Lu bisa download disini: http://www.phpconcept.net/pclzip/index.en.php

Untuk informasi PclZIP ini digunakan oleh developper Mambo pada Mambro Open Source CMS.

Wuoke, kita mulai, dengan beberapa fungsi bwatan dibwah ini:

Code:
define (“_ABSOLUTE_MENU”, “Select Menu &raquo; [ <a href=’?”.rand().”‘>HOME</a> | <a href=’?menu=extractor’>ZIP EXTRACTOR</a> | <a href=’?menu=creator’>ZIP CREATOR</a> ]”);
define (“_CREDIT”, “<p>[ <a href=’http://www.dremi.info&#8217; target=’_blank’>by dr.emi</a> ]</p>”);
define (“_MAIN_DIR”, “./”);

function redirect($delay,$goto)
{
echo”<p>Redirect progress..<br>Please Stand By.. <meta http-equiv=refresh content=$delay;URL=$goto /></p>”;
}
function clearIt($it, $defaultChar = ‘_’)
{
$it = str_replace(“‘”, “$defaultChar”, $it);
$it = str_replace(” – “, “$defaultChar”, $it);
$it = str_replace(” “, “$defaultChar”, $it);
$it = str_replace(‘”‘, “$defaultChar”, $it);
$it = str_replace(“|”, “$defaultChar”, $it);
$it = str_replace(“\'”, “$defaultChar”, $it);
$it = str_replace(“\””, “$defaultChar”, $it);
$it = str_replace(“\\”, “$defaultChar”, $it);
$it = str_replace(“/”, “$defaultChar”, $it);
$it = str_replace(“-“, “$defaultChar”, $it);
$it = str_replace(“%20”, “$defaultChar”, $it);
$it = str_replace(” “, “$defaultChar”, $it);
$it = str_replace(“(“, “$defaultChar”, $it);
$it = str_replace(“)”, “$defaultChar”, $it);
$it = str_replace(“..”, “$defaultChar”, $it);

return $it;
}
function formZIP($pointMe)
{
echo _ABSOLUTE_MENU;
if($pointMe == ‘extract’)
{
?>
<h2>ZIP Archive Extractor</h2>
<form action=”” method=”post” enctype=”multipart/form-data”>
<input type=”hidden” name=”action” value=”extractMe”>
Source FileName (filetype *.zip):<br />
<input name=”sourcename[]” type=”file” size=”35″ />
<br /><br />
Output Directory:<br />
<input name=”result” type=”text” size=”35″ value=”<? echo _MAIN_DIR; ?>” />
<br /><br />
<input name=”submit” type=”submit” value=”&plusmn; Extract ZIP” />
</form>
<?
echo _CREDIT;
}
elseif($pointMe == ‘create’)
{
?>
<h2>ZIP Archive Creator</h2>
<form action=”” method=”post”>
<input type=”hidden” name=”action” value=”createMe”>
Source FileName (can be file / folder):<br />
<input name=”sourcename” type=”text” value=”../anyfile.html” size=”35″ />
<br />
<br />
Output Directory:<br />
<input name=”result” type=”text” size=”35″ value=”<? echo _MAIN_DIR; ?>” />
<br />
<br />
<input name=”submit” type=”submit” value=”&plusmn; Packing and Create ZIP” />
</form>
<?
echo _CREDIT;
}
else
{
echo NULL;
}
}

beberapa fungsi diatas adalah fungsi fungsi standar yg gw bikin ke di tutorial tutorial PHP gw
dan ini untuk script eksekusi na:

Code:
if(isset($_POST[“submit”]))
{
include “pclzip.lib.php”;

if($_POST[‘action’] == ‘extractMe’)
{
$anyClear = clearIt($HTTP_POST_FILES[“sourcename”][“name”][0]);
$source = _MAIN_DIR.$HTTP_POST_FILES[“sourcename”][“name”][0];
//format na =>> /www/root/htdocs/sitefolder/

@copy($HTTP_POST_FILES[“sourcename”][“tmp_name”][0], $source);
$outputExtract = “$_POST[result]”; //format na =>> /www/root/htdocs/sitefolder/

//zip file to extract
$archive = new PclZip($source);

if ($archive->extract(PCLZIP_OPT_PATH, $outputExtract) == 0)
{
//failed
die(“Error : “.$archive->errorInfo(true));
}
echo “<P>It is Done !!! ENJOY BROTHER !!!</P>”;
redirect(1,$_SERVER[“PHP_SELF”]);
echo “Successfully extracted files $source into $outputExtract”;
}
elseif($_POST[‘action’] == ‘createMe’)
{
$source = “$_POST[sourcename]”; //format na =>> /www/ Atow /www/gambar.jpg
$outputArchive = “$_POST[result]”; //format na =>> /www/root/htdocs/sitefolder/

$archive = new PclZip(“$outputArchive$source.zip”);
@copy(“$outputArchive$source.zip”, “$outputArchive$source.zip”);

//zip file to create
if ($archive->create($source) == 0)
{
echo (“Error : “.$archive->errorInfo(true));
}

echo “<P>It is Done !!! ENJOY BROTHER !!!</P>”;
redirect(1,$_SERVER[“PHP_SELF”]);
echo “Successfully Create ZIP files<BR>New Archive => $source into $source.zip<BR>
and copy $source.zip to $outputArchive has done !”;
}
else
{
redirect(3,$_SERVER[“PHP_SELF”]);
echo “Are you kidding me ??”;
}
}
else
{
if($_GET[‘menu’] == ‘extractor’)
{
formZIP(‘extract’);
}
elseif($_GET[‘menu’] == ‘creator’)
{
formZIP(‘create’);
}
else
{
echo _ABSOLUTE_MENU;
echo “<h2>Welcome To ZIP Archive Extractor and Creator</h2>”;
echo _CREDIT;
}
}

Baris untuk mengekxtrak ada pada:

Code:
//zip file to extract
$archive->extract(PCLZIP_OPT_PATH, $outputExtract)

sedangkan untuk mengcreate zip na:

Code:
$archive->create($source)

It’s done broder !
Membahas beberapa artikel di beberapa forum dan setelah menerima beberapa email tentang cara mengextract dan membuat file ZIP menggunakan PHP, penulis berkeinginan untuk membagi file projek ini.

Berikut ini adalah script php yg gw pake. Script PHP ini dapat mengextract dan membuat file ZIP sndiri. Gw bikinin dalam format Form yak !

pertama dalam PHP kita butuh file lib untuk mengeksekusi proses ZIP ini. File Lib ini adalah PclZIP.php
Lu bisa download disini: http://www.phpconcept.net/pclzip/index.en.php

Untuk informasi PclZIP ini digunakan oleh developper Mambo pada Mambro Open Source CMS.

Wuoke, kita mulai, dengan beberapa fungsi bwatan dibwah ini:

Code:
define (“_ABSOLUTE_MENU”, “Select Menu &raquo; [ <a href=’?”.rand().”‘>HOME</a> | <a href=’?menu=extractor’>ZIP EXTRACTOR</a> | <a href=’?menu=creator’>ZIP CREATOR</a> ]”);
define (“_CREDIT”, “<p>[ <a href=’http://www.dremi.info&#8217; target=’_blank’>by dr.emi</a> ]</p>”);
define (“_MAIN_DIR”, “./”);

function redirect($delay,$goto)
{
echo”<p>Redirect progress..<br>Please Stand By.. <meta http-equiv=refresh content=$delay;URL=$goto /></p>”;
}
function clearIt($it, $defaultChar = ‘_’)
{
$it = str_replace(“‘”, “$defaultChar”, $it);
$it = str_replace(” – “, “$defaultChar”, $it);
$it = str_replace(” “, “$defaultChar”, $it);
$it = str_replace(‘”‘, “$defaultChar”, $it);
$it = str_replace(“|”, “$defaultChar”, $it);
$it = str_replace(“\'”, “$defaultChar”, $it);
$it = str_replace(“\””, “$defaultChar”, $it);
$it = str_replace(“\\”, “$defaultChar”, $it);
$it = str_replace(“/”, “$defaultChar”, $it);
$it = str_replace(“-“, “$defaultChar”, $it);
$it = str_replace(“%20”, “$defaultChar”, $it);
$it = str_replace(” “, “$defaultChar”, $it);
$it = str_replace(“(“, “$defaultChar”, $it);
$it = str_replace(“)”, “$defaultChar”, $it);
$it = str_replace(“..”, “$defaultChar”, $it);

return $it;
}
function formZIP($pointMe)
{
echo _ABSOLUTE_MENU;
if($pointMe == ‘extract’)
{
?>
<h2>ZIP Archive Extractor</h2>
<form action=”” method=”post” enctype=”multipart/form-data”>
<input type=”hidden” name=”action” value=”extractMe”>
Source FileName (filetype *.zip):<br />
<input name=”sourcename[]” type=”file” size=”35″ />
<br /><br />
Output Directory:<br />
<input name=”result” type=”text” size=”35″ value=”<? echo _MAIN_DIR; ?>” />
<br /><br />
<input name=”submit” type=”submit” value=”&plusmn; Extract ZIP” />
</form>
<?
echo _CREDIT;
}
elseif($pointMe == ‘create’)
{
?>
<h2>ZIP Archive Creator</h2>
<form action=”” method=”post”>
<input type=”hidden” name=”action” value=”createMe”>
Source FileName (can be file / folder):<br />
<input name=”sourcename” type=”text” value=”../anyfile.html” size=”35″ />
<br />
<br />
Output Directory:<br />
<input name=”result” type=”text” size=”35″ value=”<? echo _MAIN_DIR; ?>” />
<br />
<br />
<input name=”submit” type=”submit” value=”&plusmn; Packing and Create ZIP” />
</form>
<?
echo _CREDIT;
}
else
{
echo NULL;
}
}

beberapa fungsi diatas adalah fungsi fungsi standar yg gw bikin ke di tutorial tutorial PHP gw
dan ini untuk script eksekusi na:

Code:
if(isset($_POST[“submit”]))
{
include “pclzip.lib.php”;

if($_POST[‘action’] == ‘extractMe’)
{
$anyClear = clearIt($HTTP_POST_FILES[“sourcename”][“name”][0]);
$source = _MAIN_DIR.$HTTP_POST_FILES[“sourcename”][“name”][0];
//format na =>> /www/root/htdocs/sitefolder/

@copy($HTTP_POST_FILES[“sourcename”][“tmp_name”][0], $source);
$outputExtract = “$_POST[result]”; //format na =>> /www/root/htdocs/sitefolder/

//zip file to extract
$archive = new PclZip($source);

if ($archive->extract(PCLZIP_OPT_PATH, $outputExtract) == 0)
{
//failed
die(“Error : “.$archive->errorInfo(true));
}
echo “<P>It is Done !!! ENJOY BROTHER !!!</P>”;
redirect(1,$_SERVER[“PHP_SELF”]);
echo “Successfully extracted files $source into $outputExtract”;
}
elseif($_POST[‘action’] == ‘createMe’)
{
$source = “$_POST[sourcename]”; //format na =>> /www/ Atow /www/gambar.jpg
$outputArchive = “$_POST[result]”; //format na =>> /www/root/htdocs/sitefolder/

$archive = new PclZip(“$outputArchive$source.zip”);
@copy(“$outputArchive$source.zip”, “$outputArchive$source.zip”);

//zip file to create
if ($archive->create($source) == 0)
{
echo (“Error : “.$archive->errorInfo(true));
}

echo “<P>It is Done !!! ENJOY BROTHER !!!</P>”;
redirect(1,$_SERVER[“PHP_SELF”]);
echo “Successfully Create ZIP files<BR>New Archive => $source into $source.zip<BR>
and copy $source.zip to $outputArchive has done !”;
}
else
{
redirect(3,$_SERVER[“PHP_SELF”]);
echo “Are you kidding me ??”;
}
}
else
{
if($_GET[‘menu’] == ‘extractor’)
{
formZIP(‘extract’);
}
elseif($_GET[‘menu’] == ‘creator’)
{
formZIP(‘create’);
}
else
{
echo _ABSOLUTE_MENU;
echo “<h2>Welcome To ZIP Archive Extractor and Creator</h2>”;
echo _CREDIT;
}
}

Baris untuk mengekxtrak ada pada:

Code:
//zip file to extract
$archive->extract(PCLZIP_OPT_PATH, $outputExtract)

sedangkan untuk mengcreate zip na:

Code:
$archive->create($source)

It’s done broder !

September 19, 2008 - Posted by | Tutorial PHP

No comments yet.

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

%d bloggers like this: