php 服务器中的ZIP和UNZIP。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 服务器中的ZIP和UNZIP。相关的知识,希望对你有一定的参考价值。
?php
/**
* zipCF - Create Zip File with directory content
* Author : Abdul Awal
* Article Url: http://go.abdulawal.com/1
* Version: 1.0
* Released on: February 26 2016
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ZipCF PHP - Create a Zip with contents in the current Direcory (php script)</title>
<style type="text/css">
body{
font-family: arial;
font-size: 14px;
padding: 0;
margin: 0;
text-align: center;
}
h3{
text-align: center;
}
.container{
width: 600px;
margin: 100px auto 0 auto;
max-width: 100%;
}
label{
font-weight: bold;
margin: 10px 0;
}
input[type="text"]{
border: 1px solid #eee;
padding: 10px;
display: block;
margin: 10px auto;
}
input[type="submit"]{
padding: 10px 20px;
display: block;
margin: 10px auto;
border: 2px solid green;
background: #fff;
}
.copyright{
position: fixed;
bottom:0;
background: #333;
color: #fff;
width: 100%;
padding: 10px 20px;
text-align: center;
}
.copyright a{
color: #eee;
}
</style>
</head>
<body>
<div class="container">
<h3>ZipCF - Make zip file with current directory!</h3>
<form action="" method="POST">
<label for="zip-file-name">Zip File Name</label> <br>
<input type="text" id="zip-file-name" name="zip_file_name" value="" placeholder="Name of the zip file" />
<input type="submit" value="Create Zip File" />
</form>
<?php
if(isset($_POST['zip_file_name'])){
if(!empty($_POST['zip_file_name'])){
ini_set('max_execution_time', 10000);
/* creates a compressed zip file */
function generate_zip_file($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
function getDirItems($dir, &$results = array()){
$files = scandir($dir);
foreach($files as $key => $value){
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
list($unused_path, $used_path) = explode(basename(__DIR__).'/', $path);
$file_name = $dir.DIRECTORY_SEPARATOR.$value;
if(!is_dir($path)) {
$results[] = $used_path;
} else if($value != "." && $value != "..") {
getDirItems($path, $results);
$results[] = $value.'/';
}
}
return $results;
}
$get_name = $_POST['zip_file_name'];
$get_ext = '.zip';
$final_name = $get_name.$get_ext;
//if true, good; if false, zip creation failed
$result = generate_zip_file(getDirItems(dirname(__FILE__)),$final_name);
if($result){
echo "Successfully Created Zip file $final_name";
} else {
echo "Failed to create zip file, Please try again";
}
} else {
echo "Please provide a name for the zip file";
}
}
?>
</div>
<div class="copyright">Copyright © <?php echo date("Y"); ?> . All rights Reserved by <a href="http://abdulawal.com/" target="_blank">Abdul Awal Uzzal</a></div>
</body>
</html>
<?php
/**
* The Unzipper extracts .zip or .rar archives and .gz files on webservers.
* It's handy if you do not have shell access. E.g. if you want to upload a lot
* of files (php framework or image collection) as an archive to save time.
* As of version 0.1.0 it also supports creating archives.
*
* @author Andreas Tasch, at[tec], attec.at
* @license GNU GPL v3
* @package attec.toolbox
* @version 0.1.0
*/
define('VERSION', '0.1.0');
$timestart = microtime(TRUE);
$GLOBALS['status'] = array();
$unzipper = new Unzipper;
if (isset($_POST['dounzip'])) {
//check if an archive was selected for unzipping
$archive = isset($_POST['zipfile']) ? strip_tags($_POST['zipfile']) : '';
$destination = isset($_POST['extpath']) ? strip_tags($_POST['extpath']) : '';
$unzipper->prepareExtraction($archive, $destination);
}
if (isset($_POST['dozip'])) {
$zippath = !empty($_POST['zippath']) ? strip_tags($_POST['zippath']) : '.';
// Resulting zipfile e.g. zipper--2016-07-23--11-55.zip
$zipfile = 'zipper-' . date("Y-m-d--H-i") . '.zip';
Zipper::zipDir($zippath, $zipfile);
}
$timeend = microtime(TRUE);
$time = $timeend - $timestart;
/**
* Class Unzipper
*/
class Unzipper {
public $localdir = '.';
public $zipfiles = array();
public function __construct() {
//read directory and pick .zip and .gz files
if ($dh = opendir($this->localdir)) {
while (($file = readdir($dh)) !== FALSE) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
|| pathinfo($file, PATHINFO_EXTENSION) === 'gz'
|| pathinfo($file, PATHINFO_EXTENSION) === 'rar'
) {
$this->zipfiles[] = $file;
}
}
closedir($dh);
if (!empty($this->zipfiles)) {
$GLOBALS['status'] = array('info' => '.zip or .gz or .rar files found, ready for extraction');
}
else {
$GLOBALS['status'] = array('info' => 'No .zip or .gz or rar files found. So only zipping functionality available.');
}
}
}
/**
* Prepare and check zipfile for extraction.
*
* @param $archive
* @param $destination
*/
public function prepareExtraction($archive, $destination) {
// Determine paths.
if (empty($destination)) {
$extpath = $this->localdir;
}
else {
$extpath = $this->localdir . '/' . $destination;
// todo move this to extraction function
if (!is_dir($extpath)) {
mkdir($extpath);
}
}
//allow only local existing archives to extract
if (in_array($archive, $this->zipfiles)) {
self::extract($archive, $extpath);
}
}
/**
* Checks file extension and calls suitable extractor functions.
*
* @param $archive
* @param $destination
*/
public static function extract($archive, $destination) {
$ext = pathinfo($archive, PATHINFO_EXTENSION);
switch ($ext) {
case 'zip':
self::extractZipArchive($archive, $destination);
break;
case 'gz':
self::extractGzipFile($archive, $destination);
break;
case 'rar':
self::extractRarArchive($archive, $destination);
break;
}
}
/**
* Decompress/extract a zip archive using ZipArchive.
*
* @param $archive
* @param $destination
*/
public static function extractZipArchive($archive, $destination) {
// Check if webserver supports unzipping.
if (!class_exists('ZipArchive')) {
$GLOBALS['status'] = array('error' => 'Error: Your PHP version does not support unzip functionality.');
return;
}
$zip = new ZipArchive;
// Check if archive is readable.
if ($zip->open($archive) === TRUE) {
// Check if destination is writable
if (is_writeable($destination . '/')) {
$zip->extractTo($destination);
$zip->close();
$GLOBALS['status'] = array('success' => 'Files unzipped successfully');
}
else {
$GLOBALS['status'] = array('error' => 'Error: Directory not writeable by webserver.');
}
}
else {
$GLOBALS['status'] = array('error' => 'Error: Cannot read .zip archive.');
}
}
/**
* Decompress a .gz File.
*
* @param $archive
* @param $destination
*/
public static function extractGzipFile($archive, $destination) {
// Check if zlib is enabled
if (!function_exists('gzopen')) {
$GLOBALS['status'] = array('error' => 'Error: Your PHP has no zlib support enabled.');
return;
}
$filename = pathinfo($archive, PATHINFO_FILENAME);
$gzipped = gzopen($archive, "rb");
$file = fopen($filename, "w");
while ($string = gzread($gzipped, 4096)) {
fwrite($file, $string, strlen($string));
}
gzclose($gzipped);
fclose($file);
// Check if file was extracted.
if (file_exists($destination . '/' . $filename)) {
$GLOBALS['status'] = array('success' => 'File unzipped successfully.');
}
else {
$GLOBALS['status'] = array('error' => 'Error unzipping file.');
}
}
/**
* Decompress/extract a Rar archive using RarArchive.
*
* @param $archive
* @param $destination
*/
public static function extractRarArchive($archive, $destination) {
// Check if webserver supports unzipping.
if (!class_exists('RarArchive')) {
$GLOBALS['status'] = array('error' => 'Error: Your PHP version does not support .rar archive functionality. <a class="info" href="http://php.net/manual/en/rar.installation.php" target="_blank">How to install RarArchive</a>');
return;
}
// Check if archive is readable.
if ($rar = RarArchive::open($archive)) {
// Check if destination is writable
if (is_writeable($destination . '/')) {
$entries = $rar->getEntries();
foreach ($entries as $entry) {
$entry->extract($destination);
}
$rar->close();
$GLOBALS['status'] = array('success' => 'Files extracted successfully.');
}
else {
$GLOBALS['status'] = array('error' => 'Error: Directory not writeable by webserver.');
}
}
else {
$GLOBALS['status'] = array('error' => 'Error: Cannot read .rar archive.');
}
}
}
/**
* Class Zipper
*
* Copied and slightly modified from http://at2.php.net/manual/en/class.ziparchive.php#110719
* @author umbalaconmeogia
*/
class Zipper {
/**
* Add files and sub-directories in a folder to zip file.
*
* @param string $folder
* Path to folder that should be zipped.
*
* @param ZipArchive $zipFile
* Zipfile where files end up.
*
* @param int $exclusiveLength
* Number of text to be exclusived from the file path.
*/
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir($folder);
while (FALSE !== $f = readdir($handle)) {
// Check for local/parent path or zipping file itself and skip.
if ($f != '.' && $f != '..' && $f != basename(__FILE__)) {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);
if (is_file($filePath)) {
$zipFile->addFile($filePath, $localPath);
}
elseif (is_dir($filePath)) {
// Add sub-directory.
$zipFile->addEmptyDir($localPath);
self::folderToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
/**
* Zip a folder (including itself).
* Usage:
* Zipper::zipDir('path/to/sourceDir', 'path/to/out.zip');
*
* @param string $sourcePath
* Relative path of directory to be zipped.
*
* @param string $outZipPath
* Relative path of the resulting output zip file.
*/
public static function zipDir($sourcePath, $outZipPath) {
$pathInfo = pathinfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$z = new ZipArchive();
$z->open($outZipPath, ZipArchive::CREATE);
$z->addEmptyDir($dirName);
if ($sourcePath == $dirName) {
self::folderToZip($sourcePath, $z, 0);
}
else {
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
}
$z->close();
$GLOBALS['status'] = array('success' => 'Successfully created archive ' . $outZipPath);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>File Unzipper + Zipper</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {
font-family: Arial, sans-serif;
line-height: 150%;
}
label {
display: block;
margin-top: 20px;
}
fieldset {
border: 0;
background-color: #EEE;
margin: 10px 0 10px 0;
}
.select {
padding: 5px;
font-size: 110%;
}
.status {
margin: 0;
margin-bottom: 20px;
padding: 10px;
font-size: 80%;
background: #EEE;
border: 1px dotted #DDD;
}
.status--ERROR {
background-color: red;
color: white;
font-size: 120%;
}
.status--SUCCESS {
background-color: green;
font-weight: bold;
color: white;
font-size: 120%
}
.small {
font-size: 0.7rem;
font-weight: normal;
}
.version {
font-size: 80%;
}
.form-field {
border: 1px solid #AAA;
padding: 8px;
width: 280px;
}
.info {
margin-top: 0;
font-size: 80%;
color: #777;
}
.submit {
background-color: #378de5;
border: 0;
color: #ffffff;
font-size: 15px;
padding: 10px 24px;
margin: 20px 0 20px 0;
text-decoration: none;
}
.submit:hover {
background-color: #2c6db2;
cursor: pointer;
}
-->
</style>
</head>
<body>
<p class="status status--<?php echo strtoupper(key($GLOBALS['status'])); ?>">
Status: <?php echo reset($GLOBALS['status']); ?><br/>
<span class="small">Processing Time: <?php echo $time; ?> seconds</span>
</p>
<form action="" method="POST">
<fieldset>
<h1>Archive Unzipper</h1>
<label for="zipfile">Select .zip or .rar archive or .gz file you want to extract:</label>
<select name="zipfile" size="1" class="select">
<?php foreach ($unzipper->zipfiles as $zip) {
echo "<option>$zip</option>";
}
?>
</select>
<label for="extpath">Extraction path (optional):</label>
<input type="text" name="extpath" class="form-field" />
<p class="info">Enter extraction path without leading or trailing slashes (e.g. "mypath"). If left empty current directory will be used.</p>
<input type="submit" name="dounzip" class="submit" value="Unzip Archive"/>
</fieldset>
<fieldset>
<h1>Archive Zipper</h1>
<label for="zippath">Path that should be zipped (optional):</label>
<input type="text" name="zippath" class="form-field" />
<p class="info">Enter path to be zipped without leading or trailing slashes (e.g. "zippath"). If left empty current directory will be used.</p>
<input type="submit" name="dozip" class="submit" value="Zip Archive"/>
</fieldset>
</form>
<p class="version">Unzipper version: <?php echo VERSION; ?></p>
</body>
</html>
以上是关于php 服务器中的ZIP和UNZIP。的主要内容,如果未能解决你的问题,请参考以下文章