PageRenderTime 1328ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/SevenZipArchive.php

https://github.com/PHPGangsta/SevenZipArchive
PHP | 169 lines | 135 code | 21 blank | 13 comment | 10 complexity | a9f7c4cb576aeb8783b44797db8273c6 MD5 | raw file
  1. <?php
  2. /**
  3. * This class helps working with the command line version of 7zip (http://www.7-zip.org) to
  4. * compress, decompress and verify archives of different formats.
  5. *
  6. * @author Michael Kliewe
  7. * @author Robert Gnuschke, Alpha Team Systems & Consulting GmbH
  8. * @copyright Michael Kliewe
  9. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  10. * @link http://www.phpgangsta.de
  11. * @link http://www.rgshops.de
  12. */
  13. class SevenZipArchive
  14. {
  15. // formats for compression and decompression
  16. const FORMAT_7ZIP = '7z';
  17. const FORMAT_XZ = 'xz';
  18. const FORMAT_BZIP2 = 'bzip2';
  19. const FORMAT_GZIP = 'gzip';
  20. const FORMAT_TAR = 'tar';
  21. const FORMAT_ZIP = 'zip';
  22. const FORMAT_WIM = 'wim';
  23. // formats only for decompression
  24. const FORMAT_ARJ = 'arj';
  25. const FORMAT_CAB = 'cab';
  26. const FORMAT_CHM = 'chm';
  27. const FORMAT_CPIO = 'cpio';
  28. const FORMAT_CRAM_FS = 'cramfs';
  29. const FORMAT_DEB = 'deb';
  30. const FORMAT_DMG = 'dmg';
  31. const FORMAT_FAT = 'fat';
  32. const FORMAT_HFS = 'hfs';
  33. const FORMAT_ISO = 'iso';
  34. const FORMAT_LZH = 'lhz';
  35. const FORMAT_LZMA = 'lzma';
  36. const FORMAT_MBR = 'mbr';
  37. const FORMAT_MSI = 'msi';
  38. const FORMAT_NSIS = 'nsis';
  39. const FORMAT_NTFS = 'ntfs';
  40. const FORMAT_RAR = 'rar';
  41. const FORMAT_RPM = 'rpm';
  42. const FORMAT_SQASH_FS = 'sqashfs';
  43. const FORMAT_UDF = 'udf';
  44. const FORMAT_VHD = 'vhd';
  45. const FORMAT_XAR = 'xar';
  46. const FORMAT_Z = 'z';
  47. protected $_executablePath;
  48. protected $_format;
  49. protected $_archivePath;
  50. protected $_filePath;
  51. protected $_password;
  52. protected $_debug;
  53. public function checkExecutable()
  54. {
  55. if (!function_exists('shell_exec')) {
  56. return false;
  57. }
  58. $ret = shell_exec($this->_executablePath);
  59. if (strpos($ret, "7-Zip") === false) {
  60. return false;
  61. } else {
  62. return true;
  63. }
  64. }
  65. public function getDebug()
  66. {
  67. return $this->_debug;
  68. }
  69. public function setExecutablePath($path)
  70. {
  71. $this->_executablePath = $path;
  72. return $this;
  73. }
  74. public function setFormat($format)
  75. {
  76. $this->_format = $format;
  77. return $this;
  78. }
  79. public function setArchivePath($path)
  80. {
  81. $this->_archivePath = $path;
  82. return $this;
  83. }
  84. public function setFilePath($path)
  85. {
  86. $this->_filePath = $path;
  87. return $this;
  88. }
  89. public function setPassword($password)
  90. {
  91. $this->_password = $password;
  92. return $this;
  93. }
  94. public function compress()
  95. {
  96. $command = '"' . $this->_executablePath . '"'
  97. . ' a'
  98. . ' -t'.$this->_format
  99. . $this->_getPasswordParam()
  100. . ' ' . escapeshellarg($this->_archivePath)
  101. . ' ' . escapeshellarg($this->_filePath);
  102. $ret = shell_exec($command);
  103. if (strpos($ret, 'Everything is Ok')!==false) { // compression was successful
  104. return true;
  105. } else {
  106. $this->_debug = $ret;
  107. return false;
  108. }
  109. }
  110. public function decompress()
  111. {
  112. $command = '"' . $this->_executablePath . '"'
  113. . ' e'
  114. . ' -y'
  115. . $this->_getPasswordParam()
  116. . ' ' . escapeshellarg($this->_archivePath)
  117. . ' -o' . escapeshellarg($this->_filePath);
  118. $ret = shell_exec($command);
  119. if (strpos($ret, 'Everything is Ok')!==false) { // decompression was successful
  120. return true;
  121. } else { // password wrong or bad integrity
  122. $this->_debug = $ret;
  123. return false;
  124. }
  125. }
  126. public function verify()
  127. {
  128. $command = '"' . $this->_executablePath . '"'
  129. . ' t'
  130. . $this->_getPasswordParam()
  131. . ' '.escapeshellarg($this->_archivePath);
  132. $ret = shell_exec($command);
  133. if (strpos($ret, 'Everything is Ok')!==false) { // verification was successful
  134. return true;
  135. } else { // password wrong or bad integrity
  136. $this->_debug = $ret;
  137. return false;
  138. }
  139. }
  140. protected function _getPasswordParam()
  141. {
  142. $pwd = '';
  143. if (!empty($this->_password)) {
  144. $pwd = ' -p' . escapeshellarg($this->_password);
  145. }
  146. return $pwd;
  147. }
  148. }