| 
	
 | 
 Posted by Steve on 04/08/07 15:05 
"Sam Jost" <radeldudel@gmail.com> wrote in message  
news:1175979197.336542.15970@n76g2000hsh.googlegroups.com... 
|I want to add a script to a website to backup the files. 
| 
| So I'd like to compress all the files of the website in one zip archiv 
| for the user to download. 
| 
| Problem is, exec is disabled for security reasons. 
| 
| Is there any way to create *any* kind of single archive for my website 
| using php without exec? It does not need to be zip, any single file 
| would do nicely. 
 
you can use this class with your own function that recurses  
directories...here's how you would do a single dir: 
 
 
$archive        = new zipArchive(); 
$localDirectory = '/path/to/some/dir'; 
while (($fileName = readdir($localDirectory)) !== false) 
{ 
  if ($fileName == '.' || $fileName == '..'){ continue; } 
  $archive->addFile(basename($fileName), file_get_contents($fileName)); 
} 
file_put_contents($fileName, $archive->getArchive()); 
 
 
 
and here's the class: 
 
 
<? 
class zipArchive 
{ 
  public $zipData       = array(); 
  public $archive       = array(); 
  public $endOfArchive  = "\x50\x4b\x05\x06\x00\x00\x00\x00"; 
  public $oldOffset     = 0; 
 
  public function addDirectory($directoryName) 
  { 
    $directoryName    = str_replace('\\', '/', $directoryName); 
    $header           = "\x50\x4b\x03\x04"; 
    $header          .= "\x0a\x00"; 
    $header          .= "\x00\x00"; 
    $header          .= "\x00\x00"; 
    $header          .= "\x00\x00\x00\x00"; 
    $header          .= pack('V', 0); 
    $header          .= pack('V', 0); 
    $header          .= pack('V', 0); 
    $header          .= pack('v', strlen($directoryName)); 
    $header          .= pack('v', 0 ); 
    $header          .= $directoryName; 
    $header          .= pack('V', 0); 
    $header          .= pack('V', 0); 
    $header          .= pack('V', 0); 
    $this->zipData[]  = $header; 
    $newOffset        = strlen(implode('', $this->zipData)); 
    $directory        = "\x50\x4b\x01\x02"; 
    $directory       .= "\x00\x00"; 
    $directory       .= "\x0a\x00"; 
    $directory       .= "\x00\x00"; 
    $directory       .= "\x00\x00"; 
    $directory       .= "\x00\x00\x00\x00"; 
    $directory       .= pack('V', 0); 
    $directory       .= pack('V', 0); 
    $directory       .= pack('V', 0); 
    $directory       .= pack('v', strlen($directoryName)); 
    $directory       .= pack('v', 0 ); 
    $directory       .= pack('v', 0 ); 
    $directory       .= pack('v', 0 ); 
    $directory       .= pack('v', 0 ); 
    $directory       .= pack('V', 16 ); 
    $directory       .= pack('V', $this->oldOffset); 
    $directory       .= $directoryName; 
    $this->oldOffset  = $newOffset; 
    $this->archive[]  = $directory; 
  } 
 
  public function addFile($fileName, $data) 
  { 
    $fileName             = str_replace('\\', '/', $fileName); 
    $header               = "\x50\x4b\x03\x04"; 
    $header              .= "\x14\x00"; 
    $header              .= "\x00\x00"; 
    $header              .= "\x08\x00"; 
    $header              .= "\x00\x00\x00\x00"; 
    $uncompressedLength   = strlen($data); 
    $compression          = crc32($data); 
    $gZippedData          = gzcompress($data); 
    $gZippedData          = substr(substr($gZippedData, 0,  
strlen($gZippedData) - 4), 2); 
    $compressedLength     = strlen($gZippedData); 
    $header              .= pack('V', $compression); 
    $header              .= pack('V', $compressedLength); 
    $header              .= pack('V', $uncompressedLength); 
    $header              .= pack('v', strlen($fileName) ); 
    $header              .= pack('v', 0 ); 
    $header              .= $fileName; 
    $header              .= $gZippedData; 
    $header              .= pack('V', $compression); 
    $header              .= pack('V', $compressedLength); 
    $header              .= pack('V', $uncompressedLength); 
    $this->zipData[]      = $header; 
    $newOffset            = strlen(implode('', $this->zipData)); 
    $fileData             = "\x50\x4b\x01\x02"; 
    $fileData            .= "\x00\x00"; 
    $fileData            .= "\x14\x00"; 
    $fileData            .= "\x00\x00"; 
    $fileData            .= "\x08\x00"; 
    $fileData            .= "\x00\x00\x00\x00"; 
    $fileData            .= pack('V', $compression); 
    $fileData            .= pack('V', $compressedLength); 
    $fileData            .= pack('V', $uncompressedLength); 
    $fileData            .= pack('v', strlen($fileName)); 
    $fileData            .= pack('v', 0 ); 
    $fileData            .= pack('v', 0 ); 
    $fileData            .= pack('v', 0 ); 
    $fileData            .= pack('v', 0 ); 
    $fileData            .= pack('V', 32 ); 
    $fileData            .= pack('V', $this->oldOffset); 
    $fileData            .= $fileName; 
    $this->oldOffset      = $newOffset; 
    $this->archive[]      = $fileData; 
  } 
 
  public function getArchive() 
  { 
    $data             = implode('', $this->zipData); 
    $controlDirectory = implode('', $this->archive); 
    return  $data                                 . 
            $controlDirectory                     . 
            $this->endOfArchive                   . 
            pack('v', sizeof($this->archive))     . 
            pack('v', sizeof($this->archive))     . 
            pack('V', strlen($controlDirectory))  . 
            pack('V', strlen($data))              . 
            "\x00\x00"; 
  } 
} 
?>
 
  
Navigation:
[Reply to this message] 
 |