很多站长朋友们都不太清楚php创建加密zip,今天小编就来给大家整理php创建加密zip,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP-php生成zip压缩文件如何给该文件加解压缩密码 2、 zip密码 php 3、 如何用PHP创建一个加密的zip压缩文件 PHP-php生成zip压缩文件如何给该文件加解压缩密码<?php
//需开启配置 php_zip.dll
//phpinfo();
header("Content-type:text/html;charset=utf-8");
function get_zip_originalsize($filename, $path) {
//先判断待解压的文件是否存在
if(!file_exists($filename)){
die("文件 $filename 不存在!");
}
$starttime = explode(' ',microtime()); //解压开始的时间
//将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到
$filename = iconv("utf-8","gb2312",$filename);
$path = iconv("utf-8","gb2312",$path);
//打开压缩包
$resource = zip_open($filename);
$i = 1;
//遍历读取压缩包里面的一个个文件
while ($dir_resource = zip_read($resource)) {
//如果能打开则继续
if (zip_entry_open($resource,$dir_resource)) {
//获取当前项目的名称,即压缩包里面当前对应的文件名
$file_name = $path.zip_entry_name($dir_resource);
//以最后一个“/”分割,再用字符串截取出路径部分
$file_path = substr($file_name,0,strrpos($file_name, "/"));
//如果路径不存在,则创建一个目录,true表示可以创建多级目录
if(!is_dir($file_path)){
mkdir($file_path,0777,true);
}
//如果不是目录,则写入文件
if(!is_dir($file_name)){
//读取这个文件
$file_size = zip_entry_filesize($dir_resource);
//最大读取6M,如果文件过大,跳过解压,继续下一个
if($file_size<(1024*1024*6)){
$file_content = zip_entry_read($dir_resource,$file_size);
file_put_contents($file_name,$file_content);
}else{
echo "<p> ".$i++." 此文件已被跳过,原因:文件过大, -> ".iconv("gb2312","utf-8",$file_name)." </p>";
}
}
//关闭当前
zip_entry_close($dir_resource);
}
}
//关闭压缩包
zip_close($resource);
$endtime = explode(' ',microtime()); //解压结束的时间
$thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
$thistime = round($thistime,3); //保留3为小数
echo "<p>解压完毕!,本次解压花费:$thistime 秒。</p>";
}
$size = get_zip_originalsize('20131101.zip','temp/');
?>
zip密码 php用PHP的zip模块进行压缩加密.
开始
$zipArc = new \ZipArchive();if ($zipArc->open('/home/test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) { //设置密码 注意此处不是加密,仅仅是设置密码
if (!$zipArc->setPassword('password')) { throw new RuntimeException('Set password failed');
} //往压缩包内添加文件
$zipArc->addFile('/home/test.png', '1/test.png'); //加密文件 此处文件名及路径是压缩包内的
if (!$zipArc->setEncryptionName('1/test.png', ZipArchive::EM_AES_256)) { throw new RuntimeException('Set encryption failed');
}
}
$zipArc->close();
注意事项
1 PHP7.2以下不支持加密
php7.2 以下是不支持加密的,我们看一下php官方文档中的解释
从PHP 7.2.0和libzip 1.2.0开始,密码用于解压缩归档,也是ZipArchive :: setEncryptionName() 和ZipArchive :: setEncryptionIndex()的默认密码。
以前,此功能仅设置用于解压缩存档的密码; 它没有将非密码保护的ZipArchive 变成受密码保护的ZipArchive。
也就是说php7.2之前,setPassword('password')这个方法仅仅是设置setEncryptionName()和setEncryptionIndex()的默认密码,却没有进行加密操作!!!,就问你坑不坑!!
2 方法找不到
提示没有setEncryptionName和setEncryptionIndex方法时,请编译时zip模块时用以下参数
--with-libzip
--enable-zip
3 目录结构问题
待压缩的文件目录,比说说是/home/test/a.png
压缩后,你发现压缩包内的目录结构是/home/test/a.png,
也就是说压缩包原封不动的保持了原来文件的目录.可是我们想自定义压缩包目录怎么办呢?
$a = '/home/test.png';
$b = '1/test.png';//$a是待添加的文件路径 $b是压缩包内的路径$zipArc->addFile($a, $b);
如何用PHP创建一个加密的zip压缩文件/* creates a compressed zip file */function create_zip($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; }}
关于php创建加密zip的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php创建加密zip php加密文件的详细内容...