PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/extrass/kits/pack.php

http://arrowplatform.googlecode.com/
PHP | 158 lines | 135 code | 16 blank | 7 comment | 29 complexity | 48e0119390c1dd0bd1fd8c2e2def0663 MD5 | raw file
  1. <?php
  2. class Packer{
  3. private $fileDescriptorCount;
  4. private $fileDescriptorLimit;
  5. private $zipArchive;
  6. private $archiveName;
  7. public function __construct(){
  8. $this->fileDescriptorCount = 0;
  9. $this->fileDescriptorLimit = 200;
  10. $this->zipArchive = NULL;
  11. $this->archiveName = '';
  12. }
  13. private function openArchive($archiveName){
  14. if($this->zipArchive = new ZipArchive()) {
  15. if($this->zipArchive->open($archiveName, ZIPARCHIVE::OVERWRITE) === true){
  16. $this->fileDescriptorCount = 0;
  17. $this->archiveName = $archiveName;
  18. }
  19. else{
  20. print_r('Nie mo?na otworzy? archiwum');
  21. exit;
  22. }
  23. }
  24. else{
  25. print_r('Nie mo?na utworzy? archiwum');
  26. exit;
  27. }
  28. }
  29. private function reopenArchive(){
  30. $this->zipArchive->close();
  31. if($this->zipArchive = new ZipArchive()) {
  32. $this->zipArchive->open($this->archiveName);
  33. $this->fileDescriptorCount = 0;
  34. }
  35. else{
  36. print_r('Nie mo?na odtworzy? archiwum');
  37. exit;
  38. }
  39. }
  40. private function addFolderToZip($dir, $zipdir = ''){
  41. if (is_dir($dir)) {
  42. if ($dh = opendir($dir)) {
  43. //Add the directory
  44. if ($dir!='.' && $dir!='./')
  45. $this->zipArchive->addEmptyDir($dir);
  46. if (substr($dir,-1) !='/')
  47. $dir = $dir.'/';
  48. // Loop through all the files
  49. while (($file = readdir($dh)) !== false) {
  50. //If it's a folder, run the function again!
  51. if(!is_file($dir . $file)){
  52. // Skip parent and root directories
  53. if( ($file !== ".") && ($file !== "..")){
  54. $this->addFolderToZip($dir . $file . "/", $zipdir . $file . "/");
  55. }
  56. }else{
  57. // Add the files
  58. $this->zipArchive->addFile($dir . $file, $zipdir . $file);
  59. $this->fileDescriptorCount++;
  60. if ($this->fileDescriptorCount > $this->fileDescriptorLimit )
  61. $this->reopenArchive();
  62. }
  63. }
  64. }
  65. }
  66. }
  67. private function extractZip( $zipFile = '', $zipDirectory="." ){
  68. define(DIRECTORY_SEPARATOR, '/');
  69. $zipDir = getcwd() . DIRECTORY_SEPARATOR . $zipDirectory. DIRECTORY_SEPARATOR;
  70. $zip = zip_open($zipDir.$zipFile);
  71. if ($zip){
  72. while ($zip_entry = zip_read($zip)){
  73. $completePath = $zipDir . dirname(zip_entry_name($zip_entry));
  74. $completeName = $zipDir . zip_entry_name($zip_entry);
  75. if(!file_exists($completePath)){
  76. $tmp = '';
  77. foreach(explode('/',$completePath) AS $k){
  78. $tmp .= $k.'/';
  79. if(!file_exists($tmp) ){
  80. mkdir($tmp, 0777);
  81. }
  82. }
  83. }
  84. if (zip_entry_open($zip, $zip_entry, "r")){
  85. if ($fd = @fopen($completeName, 'w+')){
  86. fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
  87. fclose($fd);
  88. }
  89. else{
  90. @mkdir($completeName, 0777);
  91. }
  92. zip_entry_close($zip_entry);
  93. }
  94. }
  95. zip_close($zip);
  96. }
  97. return true;
  98. }
  99. public function extractArchive($archiveFilename = 'serwis.zip', $directory=".")
  100. {
  101. //extractZip u?ywa starego interfejsu (proceduralnego)
  102. $this->extractZip($archiveFilename, $directory);
  103. }
  104. public function createArchive($archiveFilename='serwis.zip', $directory=".")
  105. {
  106. $this->openArchive($archiveFilename);
  107. $this->addFolderToZip($directory);
  108. }
  109. }
  110. //---------------------------------------------------------------------------------
  111. if ( isset($_GET['zip']) && $_GET['zip']==1){
  112. $packer = new Packer();
  113. $packer->createArchive();
  114. }
  115. else if (isset($_GET['unzip']) && $_GET['unzip']==1){
  116. $packer = new Packer();
  117. $packer->extractArchive();
  118. }
  119. ?>
  120. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  121. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  122. <head>
  123. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  124. <title>zip utility</title>
  125. </head>
  126. <body>
  127. <div>
  128. <?php
  129. if (isset($_GET['zip']) && $_GET['zip']==1){
  130. print '<h1>Serwis zosta? spakowany</h1>';
  131. }
  132. else if (isset($_GET['unzip']) && $_GET['unzip']==1){
  133. print '<h1>Serwis zosta? rozpakowany</h1>';
  134. }
  135. else{
  136. print '<a href="pack.php?zip=1">Spakuj serwis</a>';
  137. print '<a href="pack.php?unzip=1">Rozpakuj serwis</a>';
  138. }
  139. ?>
  140. </div>
  141. </body>
  142. </html>