php利用ZipArchive类实现文件压缩与解压

github项目

1、Linux 安装zlib库
 


cd /usr/local/src
wget https://zlib.net/current/zlib.tar.gz
tar -zxvf zlib.tar.gz
cd zlib-1.3
./configure
make && make install

2、zlib的使用

         $all_name = 'all.zip';
        // 创建ZipArchive对象
        $zip_all = new ZipArchive();
        if ($zip_all->open('all.zip', ZipArchive::CREATE) === TRUE) {
             $zip_all->addFile(dirname(dirname(__FILE__)) .'666.docx', '666.docx');
            // 关闭Zip文件
            $zip_all->close();
        }
        //下载Zip文件到本地
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $all_name . '');
        header('Content-Length: ' . filesize($all_name));
        readfile($all_name);
        unlink($all_name);
        exit;


3、方法参考链接

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了ZipOutputStream类和ZipInputStream类,用于压缩压缩文件压缩文件: ```java import java.io.*; import java.util.zip.*; public class ZipFile { public static void main(String[] args) throws Exception { // 创建压缩文件输出流 FileOutputStream fos = new FileOutputStream("test.zip"); ZipOutputStream zos = new ZipOutputStream(fos); // 创建要压缩文件输入流 FileInputStream fis = new FileInputStream("test.txt"); BufferedInputStream bis = new BufferedInputStream(fis); // 开始写入压缩文件 zos.putNextEntry(new ZipEntry("test.txt")); int len; byte[] buffer = new byte[1024]; while ((len = bis.read(buffer)) > 0) { zos.write(buffer, 0, len); } // 关闭流 bis.close(); zos.closeEntry(); zos.close(); fos.close(); } } ``` 文件: ```java import java.io.*; import java.util.zip.*; public class UnzipFile { public static void main(String[] args) throws Exception { // 创建压缩文件输入流 FileInputStream fis = new FileInputStream("test.zip"); ZipInputStream zis = new ZipInputStream(fis); // 开始压缩文件 ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); int len; byte[] buffer = new byte[1024]; while ((len = zis.read(buffer)) > 0) { bos.write(buffer, 0, len); } // 关闭流 bos.close(); fos.close(); zipEntry = zis.getNextEntry(); } // 关闭流 zis.closeEntry(); zis.close(); fis.close(); } } ``` 以上是基本的压缩压缩文件实现方法,也可以使用更高级的类库,如Apache Commons Compress。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值