• php解压zip文件


    php实现解压zip文件

     1 function zip($filename, $path)
     2 {
     3 //        $filename = 'test.zip';
     4 //        $path = './document/2016-05-11/test.zip';
     5     $path_all = $_SERVER['DOCUMENT_ROOT'] . sp_get_asset_upload_path(mb_substr($path, 2));//think_cmf的获取文件路劲
     6     $file_name_head = mb_substr($path, 0, strrpos($path, "/"));
     7     if (!file_exists($path_all)) {
     8         echo '文件不存在';
     9     }
    10     //z转码  将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到
    11     $filename = iconv('utf-8', 'gb2312', $filename);
    12     $path_all = iconv('utf-8', 'gb2312', $path_all);
    13     //打开压缩包
    14     $resource = zip_open($path_all);
    15     $i = 0;
    16     while ($dir_resource = zip_read($resource)) {
    17         if (zip_entry_open($resource, $dir_resource)) {
    18             $file_name = substr($path_all, 0, strrpos($path_all, "/")) . '/' . zip_entry_name($dir_resource);
    19             $dir_name = zip_entry_name($dir_resource);
    20             $file_path = substr($file_name, 0, strrpos($file_name, "/"));
    21         }
    22         if (!is_dir($file_path)) {
    23             mkdir($file_path, 0777, true);
    24         }
    25         if (!is_dir($file_name)) {
    26             $file_size = zip_entry_filesize($dir_resource);
    27             //最大读取10M,如果文件过大,跳过解压,继续下一个
    28             if ($file_size < (1024 * 1024 * 10)) {
    29                 $file_content = zip_entry_read($dir_resource, $file_size);
    30                 file_put_contents($file_name, $file_content);
    31             }
    32             //关闭当前
    33             zip_entry_close($dir_resource);
    34         }
    35         $dir_name = iconv('gb2312', 'utf-8', $dir_name);
    36         $data[$i][filename] = $file_name_head . '/' . $dir_name;
    37         $data[$i]['size'] = $file_size;
    38         $i++;
    39     }
    40     zip_close($resource);
    41     return $data;
    42 }
  • 相关阅读:
    DGL学习(六): GCN实现
    DGL学习(五): DGL构建异质图
    DGL学习(四): 图分类教程
    LuoGuP2495:[SDOI2011]消耗战
    LuoGuP1121:环状最大两段子段和
    LuoGuP3177:[HAOI2015]树上染色
    LuoGuP2607:[ZJOI2008]骑士
    HDU4283:You Are the One
    LuoGuP4294:[WC2008]游览计划
    LuoGuP4127:[AHOI2009]同类分布
  • 原文地址:https://www.cnblogs.com/dreamysky/p/5905991.html
Copyright © 2020-2023  润新知