Skip to content

Commit

Permalink
Fixed tar files with long filenames, create now return number of file…
Browse files Browse the repository at this point in the history
…s added
  • Loading branch information
javiermarinros committed Jun 21, 2013
1 parent af22ba9 commit 904d941
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
17 changes: 17 additions & 0 deletions Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ abstract class Archive_Base {
*/
public $log;

public function __construct() {

}

/**
* Añade un fichero(s) al archivo dada su ruta
* @param string|string[] $path
Expand Down Expand Up @@ -79,6 +83,18 @@ protected function _get_files() {

//Incluir archivos e información sobre ellos
foreach ($this->_data as $info) {
//Comprobar si el archivo ya existe
$found = FALSE;
foreach ($files as $f) {
if ($f == $info['name']) {
$found = TRUE;
break;
}
}
if ($found)
continue;

//Crear descriptor de archivo
$file = new Archive_File();
$file->path = $info['name'];

Expand Down Expand Up @@ -128,6 +144,7 @@ function _archive_sort_files(Archive_File $a, Archive_File $b) {

/**
* Crea el archivo comprimido en la ruta indicada
* @return int Número de archivos añadidos
*/
public abstract function create($path);
}
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Example of use:

```<?php
```php
<?php

require 'Base.php';
require 'Tar.php';
Expand Down Expand Up @@ -33,7 +34,8 @@ $compressor->add_data('long/path/to/file/long/path/to/file/long/path/to/file/lon

$compressor->create(dirname(__FILE__) . '/created.zip');

?>````
?>
````



Expand Down
12 changes: 10 additions & 2 deletions Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class Archive_Tar extends Archive_Base {
const COMPRESS_GZIP = 2;
const COMPRESS_BZIP2 = 2;

public function __construct($mode = self::COMPRESS_GZIP) {
$this->mode = $mode;
parent::__construct();
}

/**
* Indica el modo en el que se creará el fichero (tar, tgz, tarbz, etc.)
* @var int
Expand Down Expand Up @@ -54,11 +59,12 @@ public function create($path) {
throw new RuntimeException("File '$path' cannot be opened");

//Escribir fichero TAR
$file_count = 0;
foreach ($files as $file) {
$path = str_replace('\\', '/', $file->path);
if (strlen($path) > 99) {
//Dividir la ruta en dos debido a las limitaciones de TAR
$prefix = substr($path, 0, strrpos($path, '/', 154) + 1);
$prefix = substr($path, 0, strrpos(substr($path, 0, min(154, strlen($path))), '/') + 1);
$path = substr($path, strlen($prefix));

if (strlen($prefix) > 154 || strlen($path) > 99) {
Expand Down Expand Up @@ -122,6 +128,8 @@ public function create($path) {
if ($file->real_path)
fclose($fileh);
}

$file_count++;
}

//Cerrar fichero
Expand All @@ -139,7 +147,7 @@ public function create($path) {
break;
}

return TRUE;
return $file_count;
}

}
4 changes: 3 additions & 1 deletion Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ public function create($dest_path) {
if (!empty($this->comment))
fwrite($archiveh, $this->comment);

return fclose($archiveh);
fclose($archiveh);

return $file_count;
}

/**
Expand Down

0 comments on commit 904d941

Please sign in to comment.