Instructions for use:
//Loading function
include_once('phpCodeZip.php');
//Create encrypted files (sourceDir the php file directory to be encrypted, targetDir the encrypted file directory )
$encryption = new PhoCodeZip('sourceDir','targetDir');
//Perform line encryption
$encryption->zip();
phpCodeZip.php source code download
phpCodeZip.rar
phpCodeZip.php source code content
Copy code The code is as follows:
/*
* @license: MIT & GPL
*/
class PhpCodeZip{
//The source folder to be compressed and encrypted
var $sourceDir = ' .';
//The folder where compression and encryption are stored
var $targetDir = 'tmp';
//Whether to encrypt
var $bcompiler = true;
//Whether it is Remove blank comments and line breaks
var $strip = true;
//Source folder file path array
var $sourcefilePaths = array();
//Destination folder file path array
var $targetPaths = array();
//The size of the folder before compression and encryption
var $sizeBeforeZip = null;
//The size of the folder after compression and encryption
var $sizeAfterZip = null ;
//Output of line break
var $newline = '';
/**
* Constructor
*
* @param string $sourceDir source folder
* @param string $targetDir destination folder
* @param boolean $bcompiler whether to encrypt
* @param boolean $strip whether to remove blank annotations and line breaks
* @return boolean
*/
public function PhpCodeZip($sourceDir='.',$targetDir=' tmp' ,$bcompiler=true,$strip=true){
//Configure initial variables
$this->sourceDir = $sourceDir;
$this->targetDir = $targetDir;
$ this->bcompiler = $bcompiler;
//Check whether the source data exists
if(!is_dir($this->sourceDir)){
die('Specified source folder '.$this->sourceDir.' does not exist, please reset');
} else {
//If the specified destination folder exists, delete it and try again
if(is_dir ( $this->targetDir)){
echo '[Initialize destination folder]'.$this->newline.$this->newline;
$this->cleanDir($this - >targetDir,true);
}
//Create a target folder with the same structure as the source folder
mkdir($this->targetDir,0777);
$dir_paths = $ this ->getPaths($this->sourceDir,'*',GLOB_ONLYDIR);
foreach($dir_paths as $key => $path){
$path = explode('/',$ path );
$path[0] = $this->targetDir;
echo '=> '.join('/',$path).$this->newline;
mkdir ( join('/',$path),0777);
}
//Get the file path list of the source folder
$this->sourcefilePaths = $this->getPaths($this - >sourceDir,'*');
//List of file paths matching the corresponding destination
foreach($this->sourcefilePaths as $key => $path){
//Settings Target folder file path
$path = explode('/',$path);
$path[0] = $this->targetDir;
$this->targetPaths[$key] = join('/',$path);
}
//Record the folder size before execution
$this->sizeBeforeZip = $this->getSizeUnit($this->getDirSize ($this->sourceDir),2);
echo $this->newline.$this->newline;
}
}
/**
* Perform compression and encryption
* @return boolean
*/
public function zip(){
$this->newline = '';
echo '[Start encryption process] (Folder size: '.$this->sizeBeforeZip.')'. $this->newline.$this->newline;
//Compress the source file
foreach($this->sourcefilePaths as $key => $path){
if( is_file($path)){
//Get file information
$pathInfo = pathInfo($path);
echo 'Read source file:'.$path.$this->newline;
//Get the compressed content
echo '=>Remove blank annotations.....';
if($this->strip && $pathInfo['extension' ] == 'php'){
$fileAterZip = php_strip_whitespace($path);
} else {
$fileAterZip = file_get_contents($path);
}
echo 'Finished'. $this->newline;
//Get the compressed content and write it to the destination location
$fp = fopen($this->targetPaths[$key],'w+');
echo '=>Write destination file.....';
fwrite($fp,$fileAterZip);
fclose($fp);
echo 'Complete' .$this->newline;
//Whether to choose to encrypt
if($this->bcompiler && $pathInfo['extension'] == 'php'){
echo '= >Encrypted original file.....';
//Copy the original file
$fh = fopen($this->targetPaths[$key].'encrypt.php', "w");
bcompiler_write_header($fh);
bcompiler_write_file($fh, $this->targetPaths[$key]);
bcompiler_write_footer($fh);
fclose($fh );
//Delete the unencrypted original file
unlink($this->targetPaths[$key]);
//Rename the encrypted file
rename($this->targetPaths[$key].'encrypt.php',$this->targetPaths[$key]);
echo 'Finished'.$this->newline;
}
echo $this->newline.$this->newline;
}
}
//Recalculate the compressed and encrypted folder size
$this->sizeAfterZip = $this->getSizeUnit($this->getDirSize($this->targetDir),2);
echo '【End encryption process】'.$this->newline.$this->newline;
echo '《Report information》'.$this->newline;
echo 'Source folder:'.$this->sourceDir.'('.$this->sizeBeforeZip.')'.$this->newline;
echo 'Destination folder:'.$this- >targetDir.'('.$this->sizeAfterZip.')'.$this->newline;
echo 'File size increase: +'.$this->getSizeUnit(($this-> ;getDirSize($this->targetDir) - $this->getDirSize($this->sourceDir))).$this->newline;
echo 'Total number of files:'.count($this- >sourcefilePaths).'piece'.$this->newline;
}
/**
* Delete all files in the directory
*
* @param string $dir The folder to be deleted
* @param boolean $deleteSelf Delete the folder at the same time
* @return void
*/
private function cleanDir($dir='.',$deleteSelf =true){
if(!$dh = @opendir($dir)) return;
while (($obj = readdir($dh))) {
if($obj=='. ' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) $this->cleanDir($dir.'/'.$obj , true);
}
if ($deleteSelf){
closedir($dh);
@rmdir($dir);
}
}
/**
* Get the total file size of the folder
*
* @param string $dir The folder to be analyzed
* @return int byte
*/
private function getDirSize($dir='.'){
//Get file path list
$filePaths = $this->getPaths($dir,'*');
//Initialize counter
$sizeCounter = 0;
foreach($filePaths as $key => $path){
$sizeCounter = $sizeCounter + filesize($path);
}
return ($sizeCounter);
}
/**
* Get all matching paths of the folder
*
* @param string $start_dir The folder to be analyzed
* @return array File path array
*/
private function getPaths($sDir, $sPattern, $nFlags = NULL){
$sDir = escapeshellcmd($sDir);
$aFiles = glob("$sDir/$sPattern", $nFlags);
foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir) {
$aSubFiles = $this->getPaths($sSubDir, $sPattern, $nFlags);
$aFiles = array_merge($aFiles, $aSubFiles);
}
return $aFiles;
}
/**
* File size unit conversion function
*
* @param int file size
* @param int number of decimal points
* @param boolean whether to cut the data into an array
* @return mix string or array
*/
public function getSizeUnit($size,$decimal=2,$split=false){
//Set the unit sequence
$unit = array(' Bytes ','KB','MB','GB','TB','PB','EB','ZB','YB');
//Initialization index
$flag = 0 ;
//Perform simplified division
while($size >= 1024){
$size = $size / 1024;
$flag++;
}
//Whether you want Separate the value from the unit
if($split){
$sizeUnit = array(
'size' => number_format($size,$decimal),
'unit' => $ unit [$flag]
);
} else {
$sizeUnit = (number_format($size,$decimal)).$unit[$flag];
}
//Return Pass size and unit
return ($sizeUnit);
}
}
http://www.bkjia.com/PHPjc/322446.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322446.htmlTechArticleInstructions for use: //Load function include_once('phpCodeZip.php'); //Create encrypted file ( sourceDir the php file directory to be encrypted, targetDir the encrypted file directory) $encryption = new PhoCo...