noalyss Version-9
zip_extended.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21
22/*!
23 * \file
24 * \brief extends the Zip object
25 */
26/*!
27 * \class Zip_Extended
28 * \brief extends the Zip object
29 */
30
32{
33 /**
34 * Function to recursively add a directory,
35 * sub-directories and files to a zip archive
36 *@note
37 * ODS format expect unix / instead of DIRECTORY_SEPARATOR
38 * otherwise, new file can not be read by OpenOffice
39 * see PHP Bug #48763 ZipArchive produces corrupt OpenOffice.org files
40 */
41 function add_recurse_folder($dir,$zipdir='')
42 {
43 if (is_dir($dir))
44 {
45 if ($dh = opendir($dir))
46 {
47 // Loop through all the files
48 $filenct = 0;
49 while (($file = readdir($dh)) !== false)
50 {
51 //If it's a folder, run the function again!
52 if(!is_file($dir . $file))
53 {
54 // Skip parent and root directories
55 if( ($file !== ".") && ($file !== ".."))
56 {
57 $this->add_recurse_folder($dir . $file . '/', $zipdir . $file . '/');
58 }
59 }
60 else
61 {
62 // Add the files
63 $this->addFile($dir . $file, $zipdir . $file);
64 $filenct +=1;
65 }
66 }
67 //Add the directory when folder was empty
68 if( (!empty($zipdir)) && ($filenct==0))
69 {
70 // remove directory separator before addEmptyDir
71 // otherwhisen create double folder in zip
72 $this->addEmptyDir(substr($zipdir, 0, -1));
73 }
74 }
75 }
76 }
77
78 /**
79 * @brief add file to the current file from a folder matching the pattern $p_pattern
80 * @param $p_folder string folder where are the file to add
81 * @param $p_pattern string pattern syntax preg_match
82 * @return int number of files added
83 * @exception throw an exception err message is ERR-EZ95 if file cannot be added
84 */
85 function add_file_pattern( $p_folder,$p_pattern):int
86 {
87 if (!is_dir($p_folder)) {
88 throw new \Exception("ERR-ZE87");
89
90 }
91 $dir=opendir($p_folder);
92 if( $dir==false) throw new \Exception("ERR-ZE90");
93 $added=0;
94 while( ($entry=readdir($dir)) != false) {
95 if ( preg_match($p_pattern, $entry)) {
96 if ( ! $this->addFile($p_folder . "/" . $entry,$entry) )
97 throw new \Exception("ERR-EZ95 : cannot add $p_folder/$entry");
98 $added++;
99 }
100 }
101 return $added;
102 }
103
104}
extends the Zip object
add_file_pattern( $p_folder, $p_pattern)
add file to the current file from a folder matching the pattern $p_pattern
add_recurse_folder($dir, $zipdir='')
Function to recursively add a directory, sub-directories and files to a zip archive.
$dir
Definition: file-dir.php:3