From 19ab93b189461d6da777e5d650c499737d5b9e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mar=C3=ADn?= Date: Thu, 24 Apr 2014 09:57:09 +0200 Subject: [PATCH] Fixed format and indentations --- Base.php | 71 +++++++++++++++++++++++++----------------- Tar.php | 67 +++++++++++++++++++++++----------------- Zip.php | 94 +++++++++++++++++++++++++++++++++----------------------- 3 files changed, 136 insertions(+), 96 deletions(-) diff --git a/Base.php b/Base.php index 8a1b129..8e146e5 100644 --- a/Base.php +++ b/Base.php @@ -3,7 +3,8 @@ /** * Representa la interfaz común de todos los compresores de archivos */ -abstract class Archive_Base { +abstract class Archive_Base +{ protected $_data = array(); @@ -15,20 +16,32 @@ abstract class Archive_Base { /** * Mensajes de adventencia o error obtenidos durante la creación del fichero - * @var string[] + * @var string[] */ public $log; - public function __construct() { - + public function __construct() + { + $this->clear(); + } + + /** + * Limpia el archivo actual, comenzando uno nuevo + */ + public function clear() + { + $this->_data = array(); + $this->log = array(); } /** * Añade un fichero(s) al archivo dada su ruta + * * @param string|string[] $path - * @param string $local_name Nombre que tendrá el archivo en el fichero creado + * @param string $local_name Nombre que tendrá el archivo en el fichero creado */ - public function add_file($path, $local_name = NULL) { + public function add_file($path, $local_name = null) + { if (is_array($path)) { foreach ($path as $p) { $this->_data[] = array( @@ -47,7 +60,8 @@ public function add_file($path, $local_name = NULL) { /** * Añade un fichero al archivo dado su contenido */ - public function add_data($local_name, $content) { + public function add_data($local_name, $content) + { $this->_data[] = array( 'content' => $content, @@ -58,41 +72,36 @@ public function add_data($local_name, $content) { /** * Añade un directorio al archivo */ - public function add_folder($local_name) { + public function add_folder($local_name) + { $this->_data[] = array( - 'is_dir' => TRUE, + 'is_dir' => true, 'content' => '', 'name' => $local_name ); } - /** - * Limpia el archivo actual, comenzando uno nuevo - */ - public function clear() { - $this->_data = array(); - $this->log = array(); - } - /** * Prepara la lista y jerarquía de los archivos a comprimir * @return Archive_File[] */ - protected function _get_files() { + protected function _get_files() + { $files = array(); //Incluir archivos e información sobre ellos foreach ($this->_data as $info) { //Comprobar si el archivo ya existe - $found = FALSE; + $found = false; foreach ($files as $f) { if ($f == $info['name']) { - $found = TRUE; + $found = true; break; } } - if ($found) + if ($found) { continue; + } //Crear descriptor de archivo $file = new Archive_File(); @@ -118,7 +127,8 @@ protected function _get_files() { //Ordenar archivos (según carpeta, tipo, etc.) if (!function_exists('_archive_sort_files')): - function _archive_sort_files(Archive_File $a, Archive_File $b) { + function _archive_sort_files(Archive_File $a, Archive_File $b) + { //Order por carpetas, extensiones y nombre $a_folder = dirname($a->path); $b_folder = dirname($b->path); @@ -128,10 +138,11 @@ function _archive_sort_files(Archive_File $a, Archive_File $b) { //Archivos juntos del mismo tipo se comprimen mejor (compresión sólida) $a_ext = pathinfo($a->path, PATHINFO_EXTENSION); $b_ext = pathinfo($b->path, PATHINFO_EXTENSION); - if ($a_ext != $b_ext) + if ($a_ext != $b_ext) { return strcmp($a_ext, $b_ext); - else + } else { return strcmp($a->path, $b->path); + } } } @@ -152,14 +163,16 @@ public abstract function create($path); /** * @access private */ -class Archive_File { +class Archive_File +{ public $path; - public $real_path = FALSE; - public $content = FALSE; + public $real_path = false; + public $content = false; public $is_directory; - public function stats() { + public function stats() + { if ($this->real_path) { return stat($this->real_path); } else { @@ -173,4 +186,4 @@ public function stats() { } } -} \ No newline at end of file +} diff --git a/Tar.php b/Tar.php index cce86a1..2345bea 100644 --- a/Tar.php +++ b/Tar.php @@ -2,16 +2,18 @@ /** * Utilidad para la creación de archivos TAR, TGZ y TAR.BZ2 - * + * * Basada en TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1 de Devin Doucette */ -class Archive_Tar extends Archive_Base { +class Archive_Tar extends Archive_Base +{ const NO_COMPRESS = 1; const COMPRESS_GZIP = 2; const COMPRESS_BZIP2 = 2; - public function __construct($mode = self::COMPRESS_GZIP) { + public function __construct($mode = self::COMPRESS_GZIP) + { $this->mode = $mode; parent::__construct(); } @@ -22,7 +24,8 @@ public function __construct($mode = self::COMPRESS_GZIP) { */ public $mode = self::COMPRESS_GZIP; - private function _write($fileh, $data) { + private function _write($fileh, $data) + { switch ($this->mode) { case self::COMPRESS_GZIP: gzwrite($fileh, $data); @@ -38,7 +41,8 @@ private function _write($fileh, $data) { } } - public function create($path) { + public function create($path) + { $files = $this->_get_files(); //Abrir fichero @@ -55,8 +59,9 @@ public function create($path) { $archiveh = fopen($path, 'w'); break; } - if (!$archiveh) + if (!$archiveh) { throw new RuntimeException("File '$path' cannot be opened"); + } //Escribir fichero TAR $file_count = 0; @@ -84,29 +89,31 @@ public function create($path) { continue; } - $block = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", //Formato - $file->path, //0 100 File name - sprintf("%07o", $stats['mode']), //100 8 File mode - sprintf("%07o", $stats['uid']), //108 8 Owner's numeric user ID - sprintf("%07o", $stats['gid']), //116 8 Group's numeric user ID - sprintf("%011o", $stats['size']), //124 12 File size in bytes (octal basis) - sprintf("%011o", $stats['mtime']), //136 12 Last modification time in numeric Unix time format (octal) - " ", //148 8 Checksum for header record - $link ? 2 : ($file->is_directory ? 5 : 0), //156 1 Type flag - $link ? readlink($file->real_path) : "", //157 100 Name of linked file - "ustar ", //257 6 UStar indicator "ustar" - " ", //263 2 UStar version "00" - "Unknown", //265 32 Owner user name - "Unknown", //297 32 Owner group name - "", //329 8 Device major number - "", //337 8 Device minor number - $prefix, //345 155 Filename prefix - "" + $block = pack( + "a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", //Formato + $file->path, //0 100 File name + sprintf("%07o", $stats['mode']), //100 8 File mode + sprintf("%07o", $stats['uid']), //108 8 Owner's numeric user ID + sprintf("%07o", $stats['gid']), //116 8 Group's numeric user ID + sprintf("%011o", $stats['size']), //124 12 File size in bytes (octal basis) + sprintf("%011o", $stats['mtime']), //136 12 Last modification time in numeric Unix time format (octal) + " ", //148 8 Checksum for header record + $link ? 2 : ($file->is_directory ? 5 : 0), //156 1 Type flag + $link ? readlink($file->real_path) : "", //157 100 Name of linked file + "ustar ", //257 6 UStar indicator "ustar" + " ", //263 2 UStar version "00" + "Unknown", //265 32 Owner user name + "Unknown", //297 32 Owner group name + "", //329 8 Device major number + "", //337 8 Device minor number + $prefix, //345 155 Filename prefix + "" ); $checksum = 0; - for ($i = 0; $i < 512; $i++) + for ($i = 0; $i < 512; $i++) { $checksum += ord(substr($block, $i, 1)); + } $checksum = pack("a8", sprintf("%07o", $checksum)); $block = substr_replace($block, $checksum, 148, 8); @@ -114,8 +121,9 @@ public function create($path) { $this->_write($archiveh, $block); if (!$link && $stats['size'] != 0) { if ($file->real_path) { - while ($temp = fread($fileh, 1048576)) + while ($temp = fread($fileh, 1048576)) { $this->_write($archiveh, $temp); + } } else { $this->_write($archiveh, $file->content); } @@ -125,10 +133,11 @@ public function create($path) { $this->_write($archiveh, str_pad('', 512 - $stats['size'] % 512), "\0"); } - if ($file->real_path) + if ($file->real_path) { fclose($fileh); + } } - + $file_count++; } @@ -150,4 +159,4 @@ public function create($path) { return $file_count; } -} \ No newline at end of file +} diff --git a/Zip.php b/Zip.php index d7e1579..9ca9f31 100644 --- a/Zip.php +++ b/Zip.php @@ -2,10 +2,11 @@ /** * Utilidad para la creación de archivos comprimidos ZIP. - * - * Basada en la clase CI_Zip de Codeigniter y TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1 de Devin Doucette + * + * Basada en la clase CI_Zip de Codeigniter y TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1 de Devin Doucette */ -class Archive_Zip extends Archive_Base { +class Archive_Zip extends Archive_Base +{ /** * Comentario incluido en el archivo @@ -15,15 +16,17 @@ class Archive_Zip extends Archive_Base { /** * Si se activa, los ficheros se almacenarán sin comprimir - * @var boolean + * @var boolean */ - public $store_only = FALSE; + public $store_only = false; - public function create($dest_path) { + public function create($dest_path) + { $archiveh = fopen($dest_path, 'w+'); - if ($archiveh === FALSE) + if ($archiveh === false) { throw new Archive_Exception("Cannot create zip file on '$path'"); + } $files = $this->_get_files(); @@ -43,7 +46,7 @@ public function create($dest_path) { //Añadir bloque local $timedate = explode(" ", date("Y n j G i s", $stats['mtime'])); $timedate = ($timedate[0] - 1980 << 25) | ($timedate[1] << 21) | ($timedate[2] << 16) | - ($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]); + ($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]); $block = pack("VvvvV", 0x04034b50, 0x000A, 0x0000, $this->store_only ? 0x0000 : 0x0008, $timedate); $crc32 = 0; @@ -57,7 +60,8 @@ public function create($dest_path) { } else { if ($file->real_path) { //Escribir bloque dummy (ya que no disponemos de toda la información aún) - $dummy_block = $block . pack("VVVvv", $crc32, $compressed_size, $stats['size'], strlen($path), 0x0000) . $path; + $dummy_block = + $block . pack("VVVvv", $crc32, $compressed_size, $stats['size'], strlen($path), 0x0000) . $path; fwrite($archiveh, $dummy_block); //Escribir archivo @@ -91,37 +95,47 @@ public function create($dest_path) { //Añadir información central $central .= - pack('V', 0x02014b50)//0 4 Central directory file header signature = 0x02014b50 - . pack('v', 0x0014)// 4 2 Version made by - . pack('v', $this->store_only ? 0x0000 : 0x000A)// 6 2 Version needed to extract (minimum) - . pack('v', 0x0000)// 8 2 General purpose bit flag - . pack('v', $this->store_only ? 0x0000 : 0x0008)//10 2 Compression method - . pack('V', $timedate)//12 2 File last modification time 14 2 File last modification date - . pack('V', $crc32)//16 4 CRC-32 - . pack('V', $compressed_size)// compressed filesize - . pack('V', $stats['size']) // uncompressed filesize - . pack('v', strlen($path)) // length of filename - . pack('v', 0) // extra field length - . pack('v', 0) // file comment length - . pack('v', 0) // disk number start - . pack('v', 0) // internal file attributes - . pack('V', $file->is_directory ? 16 : 32) // external file attributes - 'archive' bit set - . pack('V', $file_offset) // relative offset of local header - . $path; + pack('V', 0x02014b50) //0 4 Central directory file header signature = 0x02014b50 + . pack('v', 0x0014) // 4 2 Version made by + . pack('v', $this->store_only ? 0x0000 : 0x000A) // 6 2 Version needed to extract (minimum) + . pack('v', 0x0000) // 8 2 General purpose bit flag + . pack('v', $this->store_only ? 0x0000 : 0x0008) //10 2 Compression method + . pack('V', $timedate) //12 2 File last modification time 14 2 File last modification date + . pack('V', $crc32) //16 4 CRC-32 + . pack('V', $compressed_size) // compressed filesize + . pack('V', $stats['size']) // uncompressed filesize + . pack('v', strlen($path)) // length of filename + . pack('v', 0) // extra field length + . pack('v', 0) // file comment length + . pack('v', 0) // disk number start + . pack('v', 0) // internal file attributes + . pack('V', $file->is_directory ? 16 : 32) // external file attributes - 'archive' bit set + . pack('V', $file_offset) // relative offset of local header + . $path; } //Escribir información central y finalizar fwrite($archiveh, $central); - fwrite($archiveh, pack("VvvvvVVv", 0x06054b50, 0x0000, 0x0000, $file_count, // total # of entries "on this disk" - $file_count, // total # of entries "on this disk" - strlen($central), // size of central dir - $current_offset, // offset to start of central dir - strlen($this->comment)// .zip file comment length - )); - - if (!empty($this->comment)) + fwrite( + $archiveh, + pack( + "VvvvvVVv", + 0x06054b50, + 0x0000, + 0x0000, + $file_count, // total # of entries "on this disk" + $file_count, // total # of entries "on this disk" + strlen($central), // size of central dir + $current_offset, // offset to start of central dir + strlen($this->comment) + // .zip file comment length + ) + ); + + if (!empty($this->comment)) { fwrite($archiveh, $this->comment); + } fclose($archiveh); @@ -132,11 +146,14 @@ public function create($dest_path) { * @param $fileh * @param $archiveh * @param $gz_level + * * @return number */ - private function _write_file($fileh, $archiveh, $gz_level) { - if (!$this->store_only) + private function _write_file($fileh, $archiveh, $gz_level) + { + if (!$this->store_only) { $filter = stream_filter_append($archiveh, 'zlib.deflate', STREAM_FILTER_WRITE, array('level' => $gz_level)); + } $crc = hash_init("crc32b"); while ($temp = fread($fileh, 1048576)) { @@ -145,10 +162,11 @@ private function _write_file($fileh, $archiveh, $gz_level) { } $crc32 = hexdec(hash_final($crc)); - if (!$this->store_only) + if (!$this->store_only) { stream_filter_remove($filter); + } fclose($fileh); return $crc32; } -} \ No newline at end of file +}