Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 64
TZip
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
756
0.00% covered (danger)
0.00%
0 / 64
 __construct
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 20
 zip
0.00% covered (danger)
0.00%
0 / 1
156
0.00% covered (danger)
0.00%
0 / 31
 getFiles
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 13
1<?php
2/*
3 * Formdin Framework
4 * Copyright (C) 2012 Ministério do Planejamento
5 * Criado por Luís Eugênio Barbosa
6 * Essa versão é um Fork https://github.com/bjverde/formDin
7 *
8 * ----------------------------------------------------------------------------
9 * This file is part of Formdin Framework.
10 *
11 * Formdin Framework is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License version 3
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License version 3
21 * along with this program; if not,  see <http://www.gnu.org/licenses/>
22 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
23 * Fifth Floor, Boston, MA  02110-1301, USA.
24 * ----------------------------------------------------------------------------
25 * Este arquivo é parte do Framework Formdin.
26 *
27 * O Framework Formdin é um software livre; você pode redistribuí-lo e/ou
28 * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
29 * do Software Livre (FSF).
30 *
31 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
32 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
33 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
34 * para maiores detalhes.
35 *
36 * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
37 * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
38 * ou escreva para a Fundação do Software Livre (FSF) Inc.,
39 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
40 */
41
42class TZip extends ZipArchive
43{
44    private $files=null;
45    private $tempFile;
46    private $base;
47    public function __construct()
48    {
49        // limpar arquivos temporários
50        $e = new TElement();
51        $this->base = $e->getBase();
52        // limpa diretório temporário
53        $t=time();
54        $h=opendir($this->base."tmp");
55        while($file=readdir($h)) {
56            if(substr($file,0,3)=='tmp')
57            {
58                $path=$this->base.'tmp/'.$file;
59                if($t-filemtime($path)>300)
60                {
61                    @unlink($path);
62                }
63            }
64        }
65        closedir($h);
66        // apagar arquivos temporários do diretorio tmp/ da aplicação
67        $tempDir = './tmp/';
68        if( file_exists('./tmp/'))
69        {
70            $h=opendir($tempDir);
71            while($file=readdir($h)) {
72                if(substr($file,0,3)=='tmp')
73                {
74                    $path=$tempDir.$file;
75                    if($t-filemtime($path)>300)
76                    {
77                        @unlink($path);
78                    }
79                }
80            }
81            closedir($h);
82        }
83    }
84    public function zip($dir,$outFileName=null,$localDir=null)
85    {
86        if( !is_null($dir))
87        {
88            $this->files = $this->getFiles($dir);
89        }
90        if( ! is_array($this->files) || count($this->files)== 0 )
91        {
92            return null;
93        }
94        if( is_null( $outFileName ) )
95        {
96
97            if( !is_dir( $dir ))
98            {
99                $aFileParts = pathinfo($dir);
100                $baseName = $aFileParts['basename'];
101                $fileName = $aFileParts['filename'];
102                $this->tempFile = $aFileParts['dirname'].'/tmp'.date('dmYhis').'.zip';
103                   $outFileName = $this->tempFile;
104            }
105            else
106            {
107                $this->tempFile = preg_replace('/\/\//','/',$dir.'/tmp'.date('dmYhis').'.zip');
108                   $outFileName = $this->tempFile;
109            }
110        }
111
112        if( file_exists( $outFileName))
113        {
114            @unlik($outFileName);
115        }
116        $this->open( $outFileName,ZIPARCHIVE::CREATE);
117        {
118            /*
119            if( $x == ZipArchive::ER_MEMORY)
120            {
121                die('Está utilizando a memoria');
122            }
123            else
124            {
125                echo $x;
126            }
127            die();
128             *
129             */
130
131            $i = 1;
132            foreach($this->files as $file)
133            {
134                if( $i++==100)
135                {
136                    $i=1;
137                    $this->close();
138                    $this->open( $outFileName,ZIPARCHIVE::CREATE);
139
140                }
141                $this->addFile( str_replace('//','/'
142                    ,$file)
143                    ,( is_null($localDir) ? $file: str_replace($localDir,'', str_replace('//','/',$file ) ) ) ) ;
144            }
145            $this->close();
146        }
147        if( file_exists( $outFileName ))
148        {
149            if( !is_null($this->tempFile) )
150            {
151                return $outFileName;
152            }
153            return true;
154        }
155        return false;
156
157    }
158    protected function getFiles($dir)
159    {
160        $dir = preg_replace('/\/\//','/',$dir);
161        if( ! is_dir($dir)  )
162        {
163            if( !file_exists( $dir ))
164            {
165                return null;
166            }
167            else
168            {
169                $this->files[] = $dir;
170                return $this->files;
171            }
172        }
173        $aDir = scandir( $dir );
174        foreach($aDir as $d)
175        {
176            if( $d !='.' && $d !='..'  )
177            {
178                if( is_dir( $dir.'/'.$d ) )
179                {
180                    $this->getFiles($dir.'/'.$d);
181                }
182                else
183                {
184                    $this->files[] = str_replace('//','/',$dir.'/'.$d);
185                }
186            }
187        }
188        return $this->files;
189    }
190}
191?>