当前位置:编程学习 > > 正文

Laravel 中创建 Zip 压缩文件并提供下载的实现方法(Laravel 中创建 Zip 压缩文件并提供下载的实现方法)

时间:2022-04-01 21:34:06类别:编程学习

Laravel 中创建 Zip 压缩文件并提供下载的实现方法

Laravel 中创建 Zip 压缩文件并提供下载的实现方法

如果您需要您的用户支持多文件下载的话,最好的办法是创建一个压缩包并提供下载。下面通过本文给大家看下在 laravel 中的实现。

事实上,这不是关于 laravel 的,而是和 php 的关联更多,我们准备使用从 php 5.2 以来就存在的 ziparchive 类 ,如果要使用,需要确保php.ini 中的 ext-zip 扩展开启。

任务 1: 存储用户的发票文件到 storage/invoices/aaa001.pdf

下面是代码展示:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • $zip_file = 'invoices.zip'; // 要下载的压缩包的名称
  • // 初始化 php 类
  • $zip = new \ziparchive();
  • $zip->open($zip_file, \ziparchive::create | \ziparchive::overwrite);
  • $invoice_file = 'invoices/aaa001.pdf';
  • // 添加文件:第二个参数是待压缩文件在压缩包中的路径
  • // 所以,它将在 zip 中创建另一个名为 "storage/" 的路径,并把文件放入目录。
  • $zip->addfile(storage_path($invoice_file), $invoice_file);
  • $zip->close();
  • // 我们将会在文件下载后立刻把文件返回原样
  • return response()->download($zip_file);
  • 例子很简单,对吗?

    *

    任务 2: 压缩 全部 文件到 storage/invoices 目录中

    laravel 方面不需要有任何改变,我们只需要添加一些简单的 php 代码来迭代这些文件。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • $zip_file = 'invoices.zip';
  • $zip = new \ziparchive();
  • $zip->open($zip_file, \ziparchive::create | \ziparchive::overwrite);
  •  
  • $path = storage_path('invoices');
  • $files = new \recursiveiteratoriterator(new \recursivedirectoryiterator($path));
  • foreach ($files as $name => $file)
  • {
  •  // 我们要跳过所有子目录
  •  if (!$file->isdir()) {
  •   $filepath  = $file->getrealpath();
  •  
  •   // 用 substr/strlen 获取文件扩展名
  •   $relativepath = 'invoices/' . substr($filepath, strlen($path) + 1);
  •  
  •   $zip->addfile($filepath, $relativepath);
  •  }
  • }
  • $zip->close();
  • return response()->download($zip_file);
  • 到这里基本就算完成了。你看,你不需要任何 laravel 的扩展包来实现这个压缩方式。

    ps:下面看下laravel从入门到精通之 文件处理 压缩/解压zip

    1:将此软件包添加到所需软件包列表中composer.json

    "chumper/zipper": "1.0.x"

    2:命令行执行

    composer update

    3:配置app/config/app.php

  • ?
  • 1
  • 2
  • add to providers chumper\zipper\zipperserviceprovider::class
  • add to aliases 'zipper' => chumper\zipper\zipper::class
  • 4:遍历文件打包至压缩包

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • $files = array();
  •     foreach ($student as $key => $data) {
  •       if ($data->photopath != null) {
  •         $check = glob(storage_path('photo/' . $data->photopath));
  •         $files = array_merge($files, $check);
  •       }
  •     }
  •  zipper::make(storage_path() . '/systemimg/' . $name)->add($files)->close();
  • 5:读取压缩包文件

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • zipper::make( storage_path() . '/photo/photos')->extractto(storage_path('temp'));
  • $zip = new \ziparchive();//方法2:流处理,新建一个ziparchive的对象
  •        $logfiles = zipper::make($path)->listfiles('/\.png$/i');
  •        if ($zip->open($path) === true) {
  •          foreach ($logfiles as $key) {
  •            $stream = $zip->getstream($key);
  •            $str = stream_get_contents($stream); //这里注意获取到的文本编码
  •            $name = iconv("utf-8", "gb2312//ignore", $key);
  •            file_put_contents(storage_path() . '\temp\\' . $name, $str);
  •          }
  •        } else {
  •          return '{"statuscode":"300", "message":"上传失败,请检查照片"}';
  •        }
  • 总结

    以上所述是小编给大家介绍的laravel 中创建 zip 压缩文件并提供下载的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!

    原文链接:https://segmentfault.com/a/1190000018734385

    上一篇下一篇

    猜您喜欢

    热门推荐