Permalink
Browse files

support extract custom files or directories (add method extractEntry …

…and extract/extractTo)
  • Loading branch information...
1 parent 30ddcca commit 47c204d57c984adf4c62b42c19f2f894448c13b4 @Gemorroj committed Sep 4, 2012
Showing with 113 additions and 13 deletions.
  1. +80 −10 Archive/7z.php
  2. +22 −0 Archive/7z/Entry.php
  3. +11 −3 reame.txt
View
@@ -48,7 +48,7 @@ class Archive_7z
/**
* @var string
*/
- private $_outputDir = './';
+ private $_outputDirectory = './';
/**
* @var string
@@ -105,15 +105,15 @@ public function setFilename($filename)
/**
- * @param string $dir
+ * @param string $directory
* @throws Archive_7z_Exception
* @return Archive_7z
*/
- public function setOutputDir($dir = './')
+ public function setOutputDirectory($directory = './')
{
- $this->_outputDir = realpath($dir);
+ $this->_outputDirectory = realpath($directory);
- if (is_writable($this->_outputDir) === false) {
+ if (is_writable($this->_outputDirectory) === false) {
throw new Archive_7z_Exception('Output directory is not available');
}
@@ -160,9 +160,63 @@ public function setOverwriteMode($mode = Archive_7z::OVERWRITE_MODE_A)
/**
* @return string
*/
+ public function getCmdPath()
+ {
+ return $this->_cmdPath;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getFilename()
+ {
+ return $this->_filename;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getOutputDirectory()
+ {
+ return $this->_outputDirectory;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getPassword()
+ {
+ return $this->_password;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getOverwriteMode()
+ {
+ return $this->_overwriteMode;
+ }
+
+
+ /**
+ * @return string
+ */
private function _getCmdPrefix()
{
- $cmd = '"' . escapeshellcmd($this->_cmdPath) . '"'; // fix for windows
+ return '"' . escapeshellcmd($this->_cmdPath) . '"'; // fix for windows
+ }
+
+
+ /**
+ * @return string
+ */
+ private function _getCmdPostfix()
+ {
+ $cmd = '';
if ($this->_password !== null) {
$cmd .= ' -p' . escapeshellarg($this->_password);
}
@@ -175,9 +229,25 @@ private function _getCmdPrefix()
*/
public function extract()
{
- $cmd = $this->_getCmdPrefix() . ' ' . escapeshellcmd($this->_overwriteMode) . ' -o' . escapeshellarg($this->_outputDir) . ' x ' . escapeshellarg($this->_filename);
+ $cmd = $this->_getCmdPrefix() . ' x ' . escapeshellarg($this->_filename) . ' ' . escapeshellcmd($this->_overwriteMode) . ' -o' . escapeshellarg($this->_outputDirectory) . ' ' . $this->_getCmdPostfix();
+
+ exec($cmd, $out, $rv);
+
+ if ($rv !== 0) {
+ throw new Archive_7z_Exception('Error! Exit code: ' . $rv);
+ }
+ }
+
+
+ /**
+ * @param string $file
+ * @throws Archive_7z_Exception
+ */
+ public function extractEntry($file)
+ {
+ $cmd = $this->_getCmdPrefix() . ' x ' . escapeshellarg($this->_filename) . ' ' . escapeshellcmd($this->_overwriteMode) . ' -o' . escapeshellarg($this->_outputDirectory) . ' ' . $this->_getCmdPostfix() . ' ' . escapeshellarg($file);
- system($cmd, $rv);
+ exec($cmd, $out, $rv);
if ($rv !== 0) {
throw new Archive_7z_Exception('Error! Exit code: ' . $rv);
@@ -192,7 +262,7 @@ public function extract()
*/
public function getContent($file)
{
- $cmd = $this->_getCmdPrefix() . ' -so x ' . escapeshellarg($this->_filename) . ' ' . escapeshellarg($file);
+ $cmd = $this->_getCmdPrefix() . ' x ' . escapeshellarg($this->_filename) . ' -so ' . escapeshellarg($file) . ' ' . $this->_getCmdPostfix();
$out = shell_exec($cmd);
@@ -210,7 +280,7 @@ public function getContent($file)
*/
public function getEntries()
{
- $cmd = $this->_getCmdPrefix() . ' -slt l ' . escapeshellarg($this->_filename);
+ $cmd = $this->_getCmdPrefix() . ' l ' . escapeshellarg($this->_filename) . ' -slt ' . $this->_getCmdPostfix();
exec($cmd, $out, $rv);
View
@@ -118,6 +118,28 @@ public function getContent()
}
+ /**
+ * @throws Archive_7z_Exception
+ */
+ public function extract()
+ {
+ $this->_archive->extractEntry($this->_path);
+ }
+
+
+ /**
+ * @param string $directory
+ * @throws Archive_7z_Exception
+ */
+ public function extractTo($directory = './')
+ {
+ $oldDirectory = $this->_archive->getOutputDirectory();
+ $this->_archive->setOutputDirectory($directory);
+ $this->_archive->extractEntry($this->_path);
+ $this->_archive->setOutputDirectory($oldDirectory);
+ }
+
+
public function getAttributes()
{
return $this->_attributes;
View
@@ -1,15 +1,23 @@
Работа с 7z архивами с помощью командной строки.
-На данный момент поддерживается распаковка архива, просмотр всех файлов и директорий находящихся в архиве, получение содержимого файла в архиве.
+На данный момент, поддерживается распаковка всего архива,
+распаковка любой директории или файла в архиве,
+просмотр всех файлов и директорий находящихся в архиве,
+получение содержимого файла в архиве.
Пример:
set_include_path(__DIR__);
require 'Archive/7z.php';
$obj = new Archive_7z('./test.7z');
-$obj->setOutputDir('./test');
+$obj->setOutputDirectory('./test');
-print_r($obj->getEntries());
+foreach ($obj->getEntries() as $v) {
+ if ($v->getName() === 'test.txt') {
+ print_r($v);
+ $v->extractTo('./test2');
+ }
+}
echo $obj->getContent('test.txt');

0 comments on commit 47c204d

Please sign in to comment.