<?php
class Archivator
{
public function createArchive( $inputFolder , $outputFile )
{
$data = $this -> readFolder ( $inputFolder ) ;
$outputStream = fopen ( $outputFile , 'x' ) ; fwrite ( $outputStream , $converted ) ; }
public function extract ( $inputFile , $outputFolder ) {
$this -> writeData ( $data , $outputFolder ) ;
}
private function writeData( $arr , $outputFolder )
{
foreach ( $arr as $key => $value ) {
if ( isset ( $value [ 'name' ] ) && $value [ 'type' ] == 'file' ) { } else {
if ( $value [ 'type' ] ) {
$this -> writeData ( $value , $outputFolder . $value [ 'name' ] ) ;
} else {
echo "Something went wrong in writeData.\n " ;
}
}
}
}
private function readFolder( $path )
{
while ( false !== ( $file = readdir ( $handler ) ) ) { if ( $file != "." && $file != ".." ) {
$result [ $file ] = $this -> readFolder ( $path . $file . "\\ " ) ;
} else {
$result [ ] = $this -> readFile ( $path . $file ) ; }
}
}
return $result ;
} else {
echo "Something went wrong in function readFolder. Sorry. \n " ;
}
}
{
$inputStream = fopen ( $path , 'rb' ) ; if ( $inputStream != false ) {
$value = "" ;
while ( $byte = fread ( $inputStream , 1024 * 1024 ) ) { $value .= $byte ;
}
$file [ 'data' ] = base64_encode ( $value ) ; // It's one of possible ways to do it but filesize will be biger(~33%), in some cases it's a problem.
return $file ;
} else {
echo "No such file or directory" . "\n " ;
}
}
}
$arch = new Archivator( ) ;
$arch -> createArchive ( 'd:\\ok\\' , 'dat' ) ;
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7CgpjbGFzcyBBcmNoaXZhdG9yIAp7CglwdWJsaWMgZnVuY3Rpb24gY3JlYXRlQXJjaGl2ZSgkaW5wdXRGb2xkZXIsICRvdXRwdXRGaWxlKQoJewkKCQkkZGF0YSA9ICR0aGlzLT5yZWFkRm9sZGVyKCRpbnB1dEZvbGRlcik7CgkJCgkJJGNvbnZlcnRlZCA9IGpzb25fZW5jb2RlKCRkYXRhKTsKCgkJJG91dHB1dFN0cmVhbSA9IGZvcGVuKCRvdXRwdXRGaWxlLCAneCcpOwoJCW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