fork download
  1. <?php
  2.  
  3.  
  4. class Archivator
  5. {
  6. public function createArchive($inputFolder, $outputFile)
  7. {
  8. $data = $this->readFolder($inputFolder);
  9.  
  10. $converted = json_encode($data);
  11.  
  12. $outputStream = fopen($outputFile, 'x');
  13. fwrite($outputStream, $converted);
  14. fclose($outputStream);
  15. }
  16.  
  17. public function extract($inputFile, $outputFolder)
  18. {
  19. $converted = file_get_contents($inputFile);
  20. $data = json_decode($converted, true);
  21. $this->writeData($data, $outputFolder);
  22. }
  23.  
  24. private function writeData($arr, $outputFolder)
  25. {
  26. foreach ($arr as $key => $value){
  27. if (isset($value['name']) && $value['type']=='file'){
  28. file_put_contents($outputFolder."/".$value['name'], base64_decode($value['data']));
  29. } else {
  30. if($value['type']){
  31. $this->writeData($value, $outputFolder.$value['name'] );
  32. } else {
  33. echo "Something went wrong in writeData.\n";
  34. }
  35. }
  36. }
  37. }
  38.  
  39. private function readFolder($path)
  40. {
  41. $result = array();
  42. if ($handler = opendir($path)){
  43. while (false !== ($file = readdir($handler))){
  44. if ($file != "." && $file != ".."){
  45. if (is_dir($path.$file)){
  46. $result[$file] = $this->readFolder($path.$file."\\");
  47. } else {
  48. $result[] = $this->readFile($path.$file);
  49. }
  50. }
  51. }
  52.  
  53. return $result;
  54. } else {
  55. echo "Something went wrong in function readFolder. Sorry. \n";
  56. }
  57. }
  58.  
  59. private function readFile($path)
  60. {
  61. $file = array();
  62. $inputStream = fopen($path, 'rb');
  63. if ($inputStream != false){
  64. $value = "";
  65. while ($byte = fread($inputStream, 1024*1024)){
  66. $value .= $byte;
  67. }
  68. $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.
  69. fclose($inputStream);
  70. $file['name'] = basename($path);
  71. $file['type'] = filetype($path);
  72.  
  73. return $file;
  74. } else {
  75. echo "No such file or directory"."\n";
  76. }
  77. }
  78. }
  79. $arch = new Archivator();
  80. $arch->createArchive('d:\\ok\\', 'dat');
  81. $arch->extract('dat', 'd:\\');
  82. $y = fgets(STDIN);
Success #stdin #stdout #stderr 0.01s 24448KB
stdin
Standard input is empty
stdout
Something went wrong in function readFolder. Sorry. 
stderr
PHP Warning:  opendir(d:\ok\): failed to open dir: No such file or directory in /home/2u52aO/prog.php on line 43
PHP Warning:  fopen(dat): failed to open stream: Permission denied in /home/2u52aO/prog.php on line 13
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in /home/2u52aO/prog.php on line 14
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /home/2u52aO/prog.php on line 15
PHP Warning:  file_get_contents(dat): failed to open stream: No such file or directory in /home/2u52aO/prog.php on line 20
PHP Warning:  Invalid argument supplied for foreach() in /home/2u52aO/prog.php on line 27