Skip to content

Commit

Permalink
Fixed format and indentations
Browse files Browse the repository at this point in the history
  • Loading branch information
javiermarinros committed Apr 24, 2014
1 parent 904d941 commit 19ab93b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 96 deletions.
71 changes: 42 additions & 29 deletions Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
}
}
}

Expand All @@ -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 {
Expand All @@ -173,4 +186,4 @@ public function stats() {
}
}

}
}
67 changes: 38 additions & 29 deletions Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -38,7 +41,8 @@ private function _write($fileh, $data) {
}
}

public function create($path) {
public function create($path)
{
$files = $this->_get_files();

//Abrir fichero
Expand All @@ -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;
Expand Down Expand Up @@ -84,38 +89,41 @@ 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);

//Escibir cabecera y contenido
$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);
}
Expand All @@ -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++;
}

Expand All @@ -150,4 +159,4 @@ public function create($path) {
return $file_count;
}

}
}
Loading

0 comments on commit 19ab93b

Please sign in to comment.