1 | <?php |
||
2 | /** |
||
3 | * @name: ZipUtil |
||
4 | * @Created by IntelliJ IDEA |
||
5 | * @author: tinymeng |
||
6 | * @file: ReportListUtil.php |
||
7 | * @Date: 2018/7/4 10:15 |
||
8 | */ |
||
9 | namespace tinymeng\tools; |
||
10 | |||
11 | use ZipArchive; |
||
12 | |||
13 | class ZipTool { |
||
14 | |||
15 | |||
16 | public $fileName; |
||
17 | /** |
||
18 | * @var ZipArchive |
||
19 | */ |
||
20 | public $client; |
||
21 | |||
22 | static public function init(){ |
||
23 | $gateway = new self(); |
||
24 | |||
25 | $gateway->client = new ZipArchive(); |
||
26 | return $gateway; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * open |
||
31 | * @param $filename |
||
32 | * @return $this|string |
||
33 | * @author: Tinymeng <[email protected]> |
||
34 | * @time: 2022/4/27 9:26 |
||
35 | */ |
||
36 | public function open($filename,$Fromfilename=null){ |
||
37 | if (!empty($filename)){ |
||
38 | $this->setFileName($filename); |
||
39 | if(!$this->client->open($this->fileName, ZipArchive::CREATE)) |
||
40 | return 'File open failed'; |
||
41 | } |
||
42 | if(!empty($Fromfilename)){ |
||
43 | if(is_dir($Fromfilename)){ |
||
44 | $list_dir = scandir($Fromfilename); |
||
45 | for($i=2;$i<count($list_dir);$i++){ |
||
0 ignored issues
–
show
|
|||
46 | if(is_dir($Fromfilename.'/'.$list_dir[$i])){ |
||
47 | $this->client->addGlob($Fromfilename.'/'.$list_dir[$i].'/*.*', 0, array('add_path' => $Fromfilename.'/'.$list_dir[$i].'/', 'remove_path' => $Fromfilename.'/'.$list_dir[$i])); |
||
48 | $list_chr = scandir($Fromfilename.'/'.$list_dir[$i]); |
||
49 | for($j=2;$j<count($list_chr);$j++){ |
||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
50 | if(is_dir($Fromfilename.'/'.$list_dir[$i].'/'.$list_chr[$j])){ |
||
51 | echo $list_chr[$j]; |
||
52 | $this->open('',$Fromfilename.'/'.$list_dir[$i].'/'.$list_chr[$j]); |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | }else{ |
||
58 | $this->client->addFile ($Fromfilename); |
||
59 | } |
||
60 | } |
||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * getFileName |
||
66 | * @param string $filename |
||
67 | * @return string |
||
68 | * @author: Tinymeng <[email protected]> |
||
69 | * @time: 2022/4/27 9:26 |
||
70 | */ |
||
71 | private function setFileName($filename){ |
||
72 | if(!preg_match('/zip/m', $filename)){ |
||
73 | $filename = $filename.'-'.date('Ymd').rand(111,999).'.zip'; |
||
74 | } |
||
75 | $this->fileName = $filename; |
||
76 | return $filename; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * getFileName |
||
81 | * @return mixed |
||
82 | * @author: Tinymeng <[email protected]> |
||
83 | * @time: 2022/4/27 9:50 |
||
84 | */ |
||
85 | public function getFileName(){ |
||
86 | return $this->fileName; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * addFileContent |
||
91 | * @param $file_name |
||
92 | * @param $content |
||
93 | * @return $this |
||
94 | * @author: Tinymeng <[email protected]> |
||
95 | * @time: 2022/4/27 9:31 |
||
96 | */ |
||
97 | public function addFileContent($file_name,$content){ |
||
98 | $this->client->addFromString($file_name,$content); |
||
99 | return $this; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * addFile |
||
104 | * @param $file_name |
||
105 | * @param $content |
||
106 | * @return $this |
||
107 | * @author: Tinymeng <[email protected]> |
||
108 | * @time: 2022/4/27 9:31 |
||
109 | */ |
||
110 | public function addFile($file_name,$content){ |
||
111 | $this->client->addFromString($file_name,$content); |
||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * 文件下载 |
||
117 | * @return void |
||
118 | */ |
||
119 | public function download(){ |
||
120 | $this->client->finish(); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * unzip |
||
125 | * @param string $filename |
||
126 | * @param string $dir 解压缩所到目录 |
||
127 | * @return $this|string 返回错误原因 |
||
128 | * @author: Tinymeng <[email protected]> |
||
129 | * @time: 2022/4/27 9:38 |
||
130 | */ |
||
131 | public function unzip($filename,$dir){ |
||
132 | if(!file_exists($filename)) |
||
133 | return 'File does not exist'; |
||
134 | if(!$this->client->open($filename)) |
||
135 | return 'File open failed'; |
||
136 | if(!is_dir($dir)){ |
||
137 | mkdir($dir,775); |
||
138 | }else{ |
||
139 | return 'Dir mk failed'; |
||
140 | } |
||
141 | if(!$this->client->extractTo($dir)) |
||
142 | return 'File unzip failed'; |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * 文件存储 |
||
148 | * @return bool |
||
149 | * @author : TinyMeng <[email protected]> |
||
150 | * @time: 2023-12-07 15:38 |
||
151 | */ |
||
152 | public function save() { |
||
153 | return $this->client->close(); |
||
154 | } |
||
155 | |||
156 | |||
157 | } |
||
158 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: