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.

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

报告相同问题?

悬赏问题

  • ¥20 Linux上make出现错误不知道怎么解决
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码