Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit a1fe3d67aa72b0d9ebd3188bb86c8d0c9ea47ea0 0 parents
@Gemorroj authored
236 Archive/7z.php
@@ -0,0 +1,236 @@
+<?php
+/**
+ *
+ * This software is distributed under the GNU GPL v3.0 license.
+ * @author Gemorroj
+ * @copyright 2012 http://wapinet.ru
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt
+ * @link http://wapinet.ru/gmanager/
+ * @version 0.1 alpha
+ *
+ */
+
+require_once '7z/Exception.php';
+require_once '7z/Entry.php';
+
+class Archive_7z
+{
+ /**
+ * @const string
+ */
+ const CMD_PATH_NIX = '/usr/local/bin/7z';
+ const CMD_PATH_WIN = 'C:\Program Files\7-Zip\7z.exe';
+ //const CMD_PATH_WIN = '%ProgramFiles%\7-Zip\7z.exe';
+
+ /**
+ * @const string
+ */
+ const OVERWRITE_MODE_A = '-aoa'; // Overwrite All existing files
+ const OVERWRITE_MODE_S = '-aos'; // Skip extracting of existing files
+ const OVERWRITE_MODE_U = '-aou'; // aUto rename extracting file (for example, name.txt will be renamed to name_1.txt)
+ const OVERWRITE_MODE_T = '-aot'; // auto rename existing file (for example, name.txt will be renamed to name_1.txt)
+
+ /**
+ * @var string
+ */
+ private $_cmdPath;
+
+ /**
+ * @var string
+ */
+ private $_filename;
+
+ /**
+ * @var string
+ */
+ private $_password;
+
+ /**
+ * @var string
+ */
+ private $_outputDir = './';
+
+ /**
+ * @var string
+ */
+ private $_overwriteMode = self::OVERWRITE_MODE_A;
+
+
+ private $_headToken = '----------';
+ private $_listToken = '';
+
+
+ /**
+ * @param string $filename 7z archive filename
+ * @throws Archive_7z_Exception
+ */
+ public function __construct($filename)
+ {
+ $this->setFilename($filename)->setCmdPath(substr(PHP_OS, 0, 3) === 'WIN' ? self::CMD_PATH_WIN : self::CMD_PATH_NIX);
+ }
+
+
+ /**
+ * @param string $path
+ * @throws Archive_7z_Exception
+ * @return Archive_7z
+ */
+ public function setCmdPath($path)
+ {
+ $this->_cmdPath = str_replace('\\', '/', realpath($path));
+
+ if (is_executable($this->_cmdPath) === false) {
+ throw new Archive_7z_Exception('Cmd path is not available');
+ }
+
+ return $this;
+ }
+
+
+ /**
+ * @param string $filename
+ * @throws Archive_7z_Exception
+ * @return Archive_7z
+ */
+ public function setFilename($filename)
+ {
+ $this->_filename = realpath($filename);
+
+ if (is_readable($this->_filename) === false) {
+ throw new Archive_7z_Exception('Filename is not available');
+ }
+
+ return $this;
+ }
+
+
+ /**
+ * @param string $dir
+ * @throws Archive_7z_Exception
+ * @return Archive_7z
+ */
+ public function setOutputDir($dir = './')
+ {
+ $this->_outputDir = realpath($dir);
+
+ if (is_writable($this->_outputDir) === false) {
+ throw new Archive_7z_Exception('Output directory is not available');
+ }
+
+ return $this;
+ }
+
+
+ /**
+ * @param string $password
+ * @throws Archive_7z_Exception
+ * @return Archive_7z
+ */
+ public function setPassword($password)
+ {
+ $this->_password = $password;
+
+ return $this;
+ }
+
+
+ /**
+ * @param string $mode
+ * @throws Archive_7z_Exception
+ * @return Archive_7z
+ */
+ public function setOverwriteMode($mode = Archive_7z::OVERWRITE_MODE_A)
+ {
+ $this->_overwriteMode = $mode;
+
+ if (in_array($this->_overwriteMode, array(
+ self::OVERWRITE_MODE_A,
+ self::OVERWRITE_MODE_S,
+ self::OVERWRITE_MODE_T,
+ self::OVERWRITE_MODE_U
+ )) === false
+ ) {
+ throw new Archive_7z_Exception('Overwrite mode is not available');
+ }
+
+ return $this;
+ }
+
+
+ /**
+ * @throws Archive_7z_Exception
+ * @return array
+ */
+ public function extract()
+ {
+ $path = '"' . escapeshellcmd($this->_cmdPath) . '"'; // fix for windows
+ $cmd = $path . ' x ' . escapeshellarg($this->_filename) . ' ' . escapeshellcmd($this->_overwriteMode) . ' -o' . escapeshellarg($this->_outputDir);
+ if ($this->_password !== null) {
+ $cmd .= ' -p' . escapeshellarg($this->_password);
+ }
+
+ exec($cmd, $out, $rv);
+
+ if ($rv !== 0) {
+ throw new Archive_7z_Exception('Error! Exit code: ' . $rv);
+ }
+
+ return $out;
+ }
+
+
+ /**
+ * @throws Archive_7z_Exception
+ * @return Archive_7z_Entry[]
+ */
+ public function getEntries()
+ {
+ $path = '"' . escapeshellcmd($this->_cmdPath) . '"'; // fix for windows
+ $cmd = $path . ' l ' . escapeshellarg($this->_filename) . ' -slt';
+
+ exec($cmd, $out, $rv);
+
+ if ($rv !== 0) {
+ throw new Archive_7z_Exception('Error! Exit code: ' . $rv);
+ }
+
+ $list = array();
+ foreach ($this->_parseEntries($out) as $v) {
+ $list[] = new Archive_7z_Entry($v);
+ }
+
+ return $list;
+ }
+
+
+ /**
+ * @param array $output
+ * @return array
+ */
+ private function _parseEntries(array $output)
+ {
+ $head = true;
+ $list = array();
+ $i = 0;
+
+ foreach ($output as $value) {
+ if ($value === $this->_headToken) {
+ $head = false;
+ continue;
+ }
+
+ if ($head === true) {
+ continue;
+ }
+
+ if ($value === $this->_listToken) {
+ $i++;
+ continue;
+ }
+
+ $list[$i][] = $value;
+ }
+
+ return $list;
+ }
+}
139 Archive/7z/Entry.php
@@ -0,0 +1,139 @@
+<?php
+/**
+ *
+ * This software is distributed under the GNU GPL v3.0 license.
+ * @author Gemorroj
+ * @copyright 2012 http://wapinet.ru
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt
+ * @link http://wapinet.ru/gmanager/
+ * @version 0.1 alpha
+ *
+ */
+
+class Archive_7z_Entry
+{
+ private $_path;
+ private $_size;
+ private $_packedSize;
+ private $_modified;
+ private $_attributes;
+ private $_crc;
+ private $_encrypted;
+ private $_method;
+ private $_block;
+
+
+ /**
+ * @param array $data
+ */
+ public function __construct(array $data)
+ {
+ $this->_parseEntry($data);
+ }
+
+
+ /**
+ * @param array $data
+ */
+ private function _parseEntry(array $data)
+ {
+ foreach ($data as $line) {
+ list($k, $v) = explode(' =', $line, 2);
+ $v = ltrim($v);
+
+ $this->_setData($k, $v);
+ }
+ }
+
+
+ /**
+ * @param string $key
+ * @param string $value
+ */
+ private function _setData($key, $value)
+ {
+ switch ($key) {
+ case 'Path':
+ $this->_path = $value;
+ break;
+
+ case 'Size':
+ $this->_size = $value;
+ break;
+
+ case 'Packed Size':
+ $this->_packedSize = $value;
+ break;
+
+ case 'Modified':
+ $this->_modified = $value;
+ break;
+
+ case 'Attributes':
+ $this->_attributes = $value;
+ break;
+
+ case 'CRC':
+ $this->_crc = $value;
+ break;
+
+ case 'Encrypted':
+ $this->_encrypted = $value;
+ break;
+
+ case 'Method':
+ $this->_method = $value;
+ break;
+
+ case 'Block':
+ $this->_block = $value;
+ break;
+ }
+ }
+
+
+ public function getAttributes()
+ {
+ return $this->_attributes;
+ }
+
+ public function getBlock()
+ {
+ return $this->_block;
+ }
+
+ public function getCrc()
+ {
+ return $this->_crc;
+ }
+
+ public function getEncrypted()
+ {
+ return $this->_encrypted;
+ }
+
+ public function getMethod()
+ {
+ return $this->_method;
+ }
+
+ public function getModified()
+ {
+ return $this->_modified;
+ }
+
+ public function getPackedSize()
+ {
+ return $this->_packedSize;
+ }
+
+ public function getPath()
+ {
+ return $this->_path;
+ }
+
+ public function getSize()
+ {
+ return $this->_size;
+ }
+}
16 Archive/7z/Exception.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ *
+ * This software is distributed under the GNU GPL v3.0 license.
+ * @author Gemorroj
+ * @copyright 2012 http://wapinet.ru
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt
+ * @link http://wapinet.ru/gmanager/
+ * @version 0.1 alpha
+ *
+ */
+
+class Archive_7z_Exception extends Exception
+{
+
+}
13 reame.txt
@@ -0,0 +1,13 @@
+Работа с 7z архивами с помощью командной строки.
+
+На данный момент поддерживается распаковка архива и просмотр всех файлов и директорий находящихся в архиве.
+
+Пример:
+require 'Archive/7z.php';
+
+$obj = new Archive_7z('./test.7z');
+$obj->setOutputDir('./test');
+
+print_r($obj->getEntries());
+
+$obj->extract();
Please sign in to comment.
Something went wrong with that request. Please try again.