Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

ZIP_Util   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1
1
<?php
2
/**
3
 * Gestion des fichiers ZIP.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 0.1.0
7
 * @version 1.0.0
8
 * @copyright 2015-2018 Eoxia
9
 * @package EO_Framework\Core\Util
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( '\eoxia\ZIP_Util' ) ) {
19
	/**
20
	 * Gestion des fichiers ZIP
21
	 */
22
	class ZIP_Util extends \eoxia\Singleton_Util {
23
24
		/**
25
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
26
		 *
27
		 * @since 0.1.0
28
		 * @version 1.0.0
29
		 *
30
		 * @return void
31
		 */
32
		protected function construct() {}
33
34
		/**
35
		 * Dézippes l'archive $zip_path dans $destination_path
36
		 * Retournes tous les noms des fichiers contenus dans l'archive
37
		 *
38
		 * @since 0.1.0
39
		 * @version 1.0.0
40
		 *
41
		 * @param  string $zip_path         Le chemin vers l'archive.
42
		 * @param  string $destination_path Le chemin d'extraction des fichiers.
43
		 * @return array {
44
		 * 			Les propriétés du tableau retourné.
45
		 *
46
		 * 			@type boolean state True ou False.
47
		 * 			@type array $list_file Contenant plusieurs index avec le nom des fichiers dézippés.
48
		 * }
49
		 */
50
		public function unzip( $zip_path, $destination_path ) {
51
			$zip = new \ZipArchive;
52
			$data = array( 'state' => true, 'list_file' => array() );
53
54
			if ( $zip->open( $zip_path ) === true ) {
55
				if ( ! $zip->extractTo( $destination_path ) ) {
56
					$data['state'] = false;
57
				}
58
59
				// Récupérations de tous les fichiers.
60
				for ( $i = 0; $i < $zip->numFiles; $i++ ) {
61
					$filename = $zip->getNameIndex( 0 );
62
63
					if ( isset( $filename ) ) {
64
						$data['list_file'][] = $filename;
65
					}
66
				}
67
68
				$zip->close();
69
			} else {
70
				$data['state'] = false;
71
			}
72
73
			return $data;
74
		}
75
	}
76
} // End if().
77