Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPGangsta committed Sep 21, 2011
0 parents commit 46628b6
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SevenZipArchive PHP class
=====================

* Copyright (c) 2011, [http://www.phpgangsta.de](http://www.phpgangsta.de)
* Author: Michael Kliewe, [@PHPGangsta](http://twitter.com/PHPGangsta)
* Licensed under the BSD License.


Usage:
------
See example files

ToDo:
-----
- add multiple files support for compress method
- add update command: u
- add delete command: d
- add benchmark command: b

Notes:
------
If you like this script or have some features to add, contact me, fork this project, send pull requests, you know how it works.
135 changes: 135 additions & 0 deletions SevenZipArchive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

class SevenZipArchive
{
// formats for compression and decompression
const FORMAT_7ZIP = '7z';
const FORMAT_XZ = 'xz';
const FORMAT_BZIP2 = 'bzip2';
const FORMAT_GZIP = 'gzip';
const FORMAT_TAR = 'tar';
const FORMAT_ZIP = 'zip';
const FORMAT_WIM = 'wim';

// formats only for decompression
const FORMAT_ARJ = 'arj';
const FORMAT_CAB = 'cab';
const FORMAT_CHM = 'chm';
const FORMAT_CPIO = 'cpio';
const FORMAT_CRAM_FS = 'cramfs';
const FORMAT_DEB = 'deb';
const FORMAT_DMG = 'dmg';
const FORMAT_FAT = 'fat';
const FORMAT_HFS = 'hfs';
const FORMAT_ISO = 'iso';
const FORMAT_LZH = 'lhz';
const FORMAT_LZMA = 'lzma';
const FORMAT_MBR = 'mbr';
const FORMAT_MSI = 'msi';
const FORMAT_NSIS = 'nsis';
const FORMAT_NTFS = 'ntfs';
const FORMAT_RAR = 'rar';
const FORMAT_RPM = 'rpm';
const FORMAT_SQASH_FS = 'sqashfs';
const FORMAT_UDF = 'udf';
const FORMAT_VHD = 'vhd';
const FORMAT_XAR = 'xar';
const FORMAT_Z = 'z';

protected $_executablePath;
protected $_format;
protected $_archivePath;
protected $_filePath;
protected $_password;

public function setExecutablePath($path)
{
$this->_executablePath = $path;
return $this;
}

public function setFormat($format)
{
$this->_format = $format;
return $this;
}

public function setArchivePath($path)
{
$this->_archivePath = $path;
return $this;
}

public function setFilePath($path)
{
$this->_filePath = $path;
return $this;
}

public function setPassword($password)
{
$this->_password = $password;
return $this;
}

public function compress()
{
$command = '"' . $this->_executablePath . '"'
. ' a'
. ' -t'.$this->_format
. $this->_getPasswordParam()
. ' ' . escapeshellarg($this->_archivePath)
. ' ' . escapeshellarg($this->_filePath);

$ret = shell_exec($command);

if (strpos($ret, 'Everything is Ok')!==false) { // compression was successful
return true;
} else {
return false;
}
}

public function decompress()
{
$command = '"' . $this->_executablePath . '"'
. ' e'
. ' -y'
. $this->_getPasswordParam()
. ' ' . escapeshellarg($this->_archivePath)
. ' -o' . escapeshellarg($this->_filePath);

$ret = shell_exec($command);

if (strpos($ret, 'Everything is Ok')!==false) { // decompression was successful
return true;
} else { // password wrong or bad integrity
return false;
}
}

public function verify()
{
$command = '"' . $this->_executablePath . '"'
. ' t'
. $this->_getPasswordParam()
. ' '.escapeshellarg($this->_archivePath);

$ret = shell_exec($command);

if (strpos($ret, 'Everything is Ok')!==false) { // verification was successful
return true;
} else { // password wrong or bad integrity
return false;
}
}

protected function _getPasswordParam()
{
$pwd = '';
if (!empty($this->_password)) {
$pwd = ' -p' . escapeshellarg($this->_password);
}
return $pwd;
}
}
14 changes: 14 additions & 0 deletions example_compress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once 'SevenZipArchive.php';
$sevenZipArchive = new SevenZipArchive();
$sevenZipArchive->setExecutablePath('C:/Program Files (x86)/7-Zip/7za.exe')
->setArchivePath('C:/Temp/test.tar')
->setFormat(SevenZipArchive::FORMAT_TAR)
->setFilePath('C:/Temp/Angeln.gif');

if ($sevenZipArchive->compress()) {
echo 'compression successful';
} else {
echo 'compression failed';
}
13 changes: 13 additions & 0 deletions example_decompress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require_once 'SevenZipArchive.php';
$sevenZipArchive = new SevenZipArchive();
$sevenZipArchive->setExecutablePath('C:/Program Files (x86)/7-Zip/7za.exe')
->setArchivePath('C:/Temp/test.tar')
->setFilePath('C:/Temp/C');

if ($sevenZipArchive->decompress()) {
echo 'decompression successful';
} else {
echo 'decompression failed';
}
12 changes: 12 additions & 0 deletions example_verify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once 'SevenZipArchive.php';
$sevenZipArchive = new SevenZipArchive();
$sevenZipArchive->setExecutablePath('C:/Program Files (x86)/7-Zip/7za.exe')
->setArchivePath('C:/Temp/test.tar');

if ($sevenZipArchive->verify()) {
echo 'verification successful';
} else {
echo 'verification failed';
}

0 comments on commit 46628b6

Please sign in to comment.