Add some basic archive functionality

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@40772 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew O'Neil 2007-08-23 23:07:58 +00:00
parent bcb4abd61b
commit 0793806405
2 changed files with 95 additions and 0 deletions

19
filesystem/Archive.php Normal file
View File

@ -0,0 +1,19 @@
<?php
abstract class Archive extends Object {
static function open($filename) {
if(substr($filename, strlen($filename) - strlen('.tar.gz')) == '.tar.gz' ||
substr($filename, strlen($filename) - strlen('.tar.bz2')) == '.tar.bz2' ||
substr($filename, strlen($filename) - strlen('.tar')) == '.tar') {
return new TarballArchive($filename);
}
}
function extractTo($destination, $entries = null) {
}
function listing($path) {
}
}
?>

View File

@ -0,0 +1,76 @@
<?php
class TarballArchive extends Archive {
private $filename = '';
private $compressionModifiers = '';
function __construct($filename) {
$this->filename = $filename;
if(substr($filename, strlen($filename) - strlen('.gz')) == '.gz' ||
substr($filename, strlen($filename) - strlen('.tgz')) == '.tgz') {
$this->compressionModifiers = 'z';
} else if(substr($filename, strlen($filename) - strlen('.bz2')) == '.bz2') {
$compressionModifiers = 'j';
}
}
function listing() {
// Call tar on the command line to get the info we need
$command = "tar -tv{$this->compressionModifiers}f ../$this->filename";
$consoleList = `$command`;
$listing = array();
// Seperate into an array of lines
$listItems = explode("\n", $consoleList);
foreach($listItems as $listItem) {
// The path is the last thing on the line
$fullpath = substr($listItem, strrpos($listItem, ' ') + 1);
$path = explode('/', $fullpath);
$item = array();
// The first part of the line is the permissions - the first character will be d if it is a directory
$item['type'] = (substr($listItem, 0, 1) == 'd') ? 'directory' : 'file';
if($item['type'] == 'directory') {
$item['listing'] = array();
// If it's a directory, the path will have a slash on the end, so get rid of it.
array_pop($path);
}
// The name of the file/directory is the last item on the path
$name = array_pop($path);
if($name == '') {
continue;
}
$item['path'] = implode('/', $path);
// Put the item in the right place
$dest = &$listing;
foreach($path as $folder) {
// If the directory doesn't exist, create it
if(!isset($dest[$folder])) {
$dest[$folder] = array();
$dest[$folder]['listing'] = array();
$dest[$folder]['type'] = 'directory';
}
$dest = &$dest[$folder]['listing'];
}
// If this is a directory and it's listing has already been created, copy the the listing
if($item['type'] == 'directory' && isset($dest[$name]['listing'])) {
$item['listing'] = $dest[$name]['listing'];
}
$dest[$name] = $item;
}
return $listing;
}
}
?>