douni9620 2015-09-15 01:25
浏览 41
已采纳

命令行zip(wamp / windows / php)

I'm new to php and S.O community.

I'm on : -Windows -Wamp

My problem recently is to make a password protected zip for download. As you know, Ziparchive doesn't allow to make password protected zip, function setpassword is just for decryption.

So I heard I could do that by command line, but to be honest all I understand about exec() in command line is that php will execute zip.exe and password protect file.

So please what's the steps for making this "command line" work in my case (windows/wamp) ?

Here is my script :

<?php
$ZipN='zip.zip';
$Zip = new ZipArchive();
//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';


function zipFilesAndDownload($ZipN,$Zip,$file_path){
    if ($Zip->open($ZipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$ZipN>
");
    }
$Zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.
");
$Zip->close();

//Password protect Not working !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
exec("zip -P password $file_path.$ZipN $ZipN");

    //then send the headers to foce download the Zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$ZipN");
    header("Content-length: " . filesize($ZipN));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$ZipN");

    // Delete the files from the server, even if the user cancels the download
    ignore_user_abort(true);

    unlink($file_path.$ZipN);
    exit;
}

zipFilesAndDownload($ZipN,$Zip,$file_path);
?>

Thanks

  • 写回答

1条回答 默认 最新

  • doutuan8887 2015-09-15 01:31
    关注

    7-zip has an amazing command-line based zip utility that supports passwords (-p switch). Get it here (get the Extra version for the standalone commandline). Here are some examples of its command-line usage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入