【问题标题】:Decompressing a file, download then remove解压文件,下载然后删除
【发布时间】:2018-06-18 14:29:28
【问题描述】:

我有压缩、加密的文件。 压缩文件虽然有其原始扩展名。他们没有 .zip 或任何其他压缩扩展名。

我具有以下功能,可以解密文件然后将其提供给下载。 所以当我现在下载文件时,我得到的是解密的文件,但仍然是压缩的。

下载前我需要帮助来实现解压。

function Download($path,$type,$key,$iv){
    if($type=="File"){
        if (file_exists($path)) {
            $fileBuffer = file_get_contents($path);
            $iv = str_pad($iv, 16, "\0");
            $cipher = base64_decode($fileBuffer);
            $plain = openssl_decrypt($cipher, "AES-256-CBC", $key, OPENSSL_RAW_DATA, $iv);
            file_put_contents($path, $plain);

            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($path).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($path));
            readfile($path);

            $fileBuffer = file_get_contents($path);
            $iv = str_pad($iv, 16, "\0");
            $cipher = openssl_encrypt($fileBuffer, "AES-256-CBC", $key, OPENSSL_RAW_DATA, $iv);
            $cipher64 = base64_encode($cipher);
            file_put_contents($path, $cipher64);
            exit;
        }

【问题讨论】:

  • 有多种压缩算法和格式。您需要准确指定用于压缩的算法和格式。
  • 它是一个简单的 .zip,但 .zip ext 已从文件名中删除,因此文件类似于 example.jpg

标签: php file encryption download unzip


【解决方案1】:

有一个名为 ZipArchive 的内置表达式。您可以在函数本身中使用它:

$zip = new ZipArchive;
$result = $zip->open('zipfile.zip');

解密后就可以放这个了。

同时参考这些文档 -- ZIPARCHIVEZIPARCHIVE 2

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多