| | | | |
| |||||||
| PHP-Fortgeschrittene Arbeiten mit PHP ohne Einschränkungen |
|
| | LinkBack | Themen-Optionen | Thema bewerten |
| | |
| Erfahrener Benutzer Registriert seit: 14.07.2005
Beiträge: 157
![]() | So. Mein Problem gilt nun endgültig als gelöst ![]() Ich hab die ZIP Klasse (zipfile.php) von phpmyadmin genommen und modifziert. Meine Klasse arbeitet auch speicherschonender als deren Originalklasse. Anstatt die Daten und Dir-Einträge in ein Array zu packen, werden diese in eine temporäre Datei, getrennt voneinander, geschrieben. Am Ende wird die temporäre Datendatei umbenannt (rename) und der Inhalt der "Dir" Datei einfach per fopen(..., 'a') an das Archiv gehangen. Muß nur noch meine archiv-split-Funktion einbauen und natürlich nen Export. Aber das jetztige Ergebnis falls einer Interesse hat Syntax: $zip = new zipfile; $zip->root = '/var/images/'; $zip->create('/tmp/bilder.zip'); $zip->addDir('small/'); $zip->addDir('large/'); $zip->close(); root ab diesem Pfad werden die Verzeichnisse addDir relativ eingebunden. bsp an 'small/' absolut: /var/images/small/ in zip: small/ create Archivdatei close abschließen der Archivdatei. Temporäre Daten+Dir Dateien werden zur Archivdatei zusammengefügt. zipfile.php Code: <?php
/**
* Zip file creation class.
* Makes zip files.
*
* Holger Müller
*
* Originally Based on :
*
* http://www.zend.com/codex.php?id=535&single=1
* By Eric Mueller <eric@themepark.com>
*
* http://www.zend.com/codex.php?id=470&single=1
* by Denis125 <webmaster@atlant.ru>
*
* a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
* date and time of the compressed file
*
* Official ZIP file format: http://www.pkware.com/appnote.txt
*/
class zipfile
{
/**
* Zähler für Verzeichniseinträge
*
* @var int
* @access private
*/
private $index_dir;
/**
* End of central directory record
*
* @var string
* @access public
*/
public $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
/**
* Last offset position
*
* @var int
* @access public
*/
public $old_offset = 0;
/**
* setze root Verzeichnis. Ab dort werden die Pfade
* relativ eingebunden
*
* @var string
* @access public
*/
public $root;
/**
* Zip Archiv Name
*
* @var string
* @access private
*/
private $archive;
/**
* tmp archive name data
*
* @var string
* @access private
*/
private $tmp_archive_data;
/**
* tmp archive name dir
*
* @var string
* @access private
*/
private $tmp_archive_dir;
/**
* constructor
*
* @access public
*/
public function __construct()
{
$this->archive = (string) NULL;
$this->root = (string) NULL;
}
/**
* erstelle zip Datei
*
* @param string $archive absoluter Pfad wo die ZIP Datei erstellt werden soll
* @param int $split_size (bytes) erstelle neues Archiv bevor Archiv diese Größe überschreitet, 0 bedeutet unbegrenzt
* @param bool $overwrite bestehendes Archiv überschreiben, default true
* @return bool true|false
* @access public
*/
public function create($archive)
{
$tmp = rand(1000,9999);
$this->index_dir = (int) 0;
$this->archive = (string) $archive;
$this->tmp_archive_data = (string) preg_replace('/(.*)\.(.*)/U', '$1.data'.$tmp.'.$2', $this->archive);
$this->tmp_archive_dir = (string) preg_replace('/(.*)\.(.*)/U', '$1.dir'.$tmp.'.$2', $this->archive);
return (bool)true;
}
/**
* Converts an Unix timestamp to a four byte DOS date and time format (date
* in high two bytes, time in low two bytes allowing magnitude comparison).
*
* @param integer the current Unix timestamp
* @return integer the current date in a four byte DOS format
* @access private
*/
public function unix2DosTime($unixtime = 0)
{
$timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
if ($timearray['year'] < 1980)
{
$timearray['year'] = 1980;
$timearray['mon'] = 1;
$timearray['mday'] = 1;
$timearray['hours'] = 0;
$timearray['minutes'] = 0;
$timearray['seconds'] = 0;
}
return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
}
/**
* Adds "file" to archive
*
* @param string file contents
* @param string name of the file in the archive (may contains the path)
* @param integer the current timestamp
*
* @access public
*/
public function addFile($data, $name, $time = 0)
{
$name = str_replace('\\', '/', $name);
$dtime = dechex($this->unix2DosTime($time));
$hexdtime = '\x' . $dtime[6] . $dtime[7] .
'\x' . $dtime[4] . $dtime[5] .
'\x' . $dtime[2] . $dtime[3] .
'\x' . $dtime[0] . $dtime[1];
eval('$hexdtime = "' . $hexdtime . '";');
$fr = "\x50\x4b\x03\x04";
$fr .= "\x14\x00"; // ver needed to extract
$fr .= "\x00\x00"; // gen purpose bit flag
$fr .= "\x08\x00"; // compression method
$fr .= $hexdtime; // last mod time and date
// "local file header" segment
$unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data, 9);
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
$c_len = strlen($zdata);
$fr .= pack('V', $crc); // crc32
$fr .= pack('V', $c_len); // compressed filesize
$fr .= pack('V', $unc_len); // uncompressed filesize
$fr .= pack('v', strlen($name)); // length of filename
$fr .= pack('v', 0); // extra field length
$fr .= $name;
// "file data" segment
$fr .= $zdata;
// "data descriptor" segment (optional but necessary if archive is not
// served as file)
$fr .= pack('V', $crc); // crc32
$fr .= pack('V', $c_len); // compressed filesize
$fr .= pack('V', $unc_len); // uncompressed filesize
// add this entry to file
$fh = fopen($this->tmp_archive_data, 'a');
fwrite($fh, $fr);
fclose($fh);
// now add to central directory record
$cdrec = "\x50\x4b\x01\x02";
$cdrec .= "\x00\x00"; // version made by
$cdrec .= "\x14\x00"; // version needed to extract
$cdrec .= "\x00\x00"; // gen purpose bit flag
$cdrec .= "\x08\x00"; // compression method
$cdrec .= $hexdtime; // last mod time & date
$cdrec .= pack('V', $crc); // crc32
$cdrec .= pack('V', $c_len); // compressed filesize
$cdrec .= pack('V', $unc_len); // uncompressed filesize
$cdrec .= pack('v', strlen($name) ); // length of filename
$cdrec .= pack('v', 0 ); // extra field length
$cdrec .= pack('v', 0 ); // file comment length
$cdrec .= pack('v', 0 ); // disk number start
$cdrec .= pack('v', 0 ); // internal file attributes
$cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
$cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
$this -> old_offset += strlen($fr);
$cdrec .= $name;
// optional extra field, file comment goes here
// save to central directory
$this->index_dir++;
$fh = fopen($this->tmp_archive_dir, 'a');
fwrite($fh, $cdrec);
fclose($fh);
}
/**
* erstellt aus den temporären Daten das Archive
*
* @access public
*/
public function close()
{
$filesize_data = (int) filesize($this->tmp_archive_data);
$filesize_dir = (int) filesize($this->tmp_archive_dir);
// benenne temporäre Datendatei in Archivdatei um
rename($this->tmp_archive_data, $this->archive);
// öffne Archivdatei
$fh = fopen($this->archive, 'a');
// füge Verzeichnisinformation an Archivdatei
fwrite($fh, file_get_contents($this->tmp_archive_dir));
// füge Archivendinformation hinzu
fwrite($fh, $this -> eof_ctrl_dir .
pack('v', $this->index_dir) . // total # of entries "on this disk"
pack('v', $this->index_dir) . // total # of entries overall
pack('V', $filesize_dir) . // size of central dir
pack('V', $filesize_data) . // offset to start of central dir
"\x00\x00" // .zip file comment length
);
fclose($fh);
// lösche temporäre Dateien
unlink($this->tmp_archive_dir);
}
/**
* füge Verzeichnis/Dateien rekursiv hinzu
*
* @param string $dir
* @access public
*/
public function addDir($dir)
{
if(substr($dir, -1)!='/') $dir.= '/';
if(is_dir($this->root.$dir))
{
$dh = opendir($this->root.$dir);
while($row = readdir($dh))
{
if($row!='.' && $row!='..')
{
if(is_dir($this->root.$dir.$row))
{
$this->addDir($dir.$row.'/');
} elseif (is_file($this->root.$dir.$row)) {
/* prüfe ob Archiv gesplittet werden muß */
//$this->split_archive($this->root.$dir.$row);
$this->addFile(file_get_contents($this->root.$dir.$row), $dir.$row);
//$this->current_archive_size+= filesize($this->root.$dir.$row);
}
}
}
}
closedir($dh);
}
}
?>
|
| | |
|
| Themen-Optionen | |
| Thema bewerten | |
|
|
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| PHP Dateien verschlüsseln | GSJLink | PHP-Fortgeschrittene | 2 | 26.04.2008 12:29 |
| [Logik] Mehrere Dateien als Anhang per Formular versenden | PsychoEagle | PHP Tipps 2008 | 2 | 27.08.2007 08:58 |
| [Erledigt] Dateien aus Verzeichnissen vom Webserver auslesen und linken | PHP Tipps 2004 | 3 | 08.09.2004 10:07 | |