ini_set(’memory_limit’,'800M’);
set_time_limit(36000);
/**
* @Module to Extract folder in one directory
* @package general
* @Author: Mr. Manoj M.Ninave
**/
//create media/import directory in root dir
$dir = $_SERVER['DOCUMENT_ROOT'].”/media/import”;
$filename = $dir.”/ABC.zip”;//destination
$zip_file = “http://www.yourssite.com/ABC.zip”;//source
$contents = file_get_contents($zip_file);
//echo $contents;
$handle = fopen($filename, “w+”);
fwrite($handle,$contents);
fclose($handle);
$zip = zip_open($filename);
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$zipEntry = zip_entry_name($zip_entry);
$zipEntryFile = substr($zipEntry,strrpos($zipEntry,”/”)+1);
$dirFile = $zipEntryFile;//unzipped directory
$dirPath = $dir.”/”.$dirFile;
$fp = fopen($dirPath, “w+”);
if (!$fp)
continue;
if(zip_entry_open($zip,$zip_entry, “r”))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,$buf);
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
unlink($filename);
//unlink($dirPath);
echo “Unzipped successfully.”;
First it will copy zip file from source to destination.
All the files in subdirectory will extract in one directory i.e. in media/import directory.
Regard
Manoj Ninave
Software Engineer,
set_time_limit(36000);
/**
* @Module to Extract folder in one directory
* @package general
* @Author: Mr. Manoj M.Ninave
**/
//create media/import directory in root dir
$dir = $_SERVER['DOCUMENT_ROOT'].”/media/import”;
$filename = $dir.”/ABC.zip”;//destination
$zip_file = “http://www.yourssite.com/ABC.zip”;//source
$contents = file_get_contents($zip_file);
//echo $contents;
$handle = fopen($filename, “w+”);
fwrite($handle,$contents);
fclose($handle);
$zip = zip_open($filename);
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$zipEntry = zip_entry_name($zip_entry);
$zipEntryFile = substr($zipEntry,strrpos($zipEntry,”/”)+1);
$dirFile = $zipEntryFile;//unzipped directory
$dirPath = $dir.”/”.$dirFile;
$fp = fopen($dirPath, “w+”);
if (!$fp)
continue;
if(zip_entry_open($zip,$zip_entry, “r”))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,$buf);
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
unlink($filename);
//unlink($dirPath);
echo “Unzipped successfully.”;
First it will copy zip file from source to destination.
All the files in subdirectory will extract in one directory i.e. in media/import directory.
Regard
Manoj Ninave
Software Engineer,
This comment has been removed by the author.
ReplyDeleteMy version in python
ReplyDeleteimport os
def unzip(path):
command = 'tar -xjvf %s' % path
try:
x = os.system(command)
except:
print "The path is not correct"
print "Extracted in the same path."
PS: Ideally, i would have done this in 1 line on bash itself.
$ tar -xjvf zip_file
That is very great Atif.Thanks for comment.Be connect.Share you knowledge here.
ReplyDelete