-
Notifications
You must be signed in to change notification settings - Fork 9
/
SevenZipArchive.php
169 lines (148 loc) · 4.46 KB
/
SevenZipArchive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* This class helps working with the command line version of 7zip (http://www.7-zip.org) to
* compress, decompress and verify archives of different formats.
*
* @author Michael Kliewe
* @author Robert Gnuschke, Alpha Team Systems & Consulting GmbH
* @copyright Michael Kliewe
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpgangsta.de
* @link http://www.rgshops.de
*/
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;
protected $_debug;
public function checkExecutable()
{
if (!function_exists('shell_exec')) {
return false;
}
$ret = shell_exec($this->_executablePath);
if (strpos($ret, "7-Zip") === false) {
return false;
} else {
return true;
}
}
public function getDebug()
{
return $this->_debug;
}
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 {
$this->_debug = $ret;
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
$this->_debug = $ret;
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
$this->_debug = $ret;
return false;
}
}
protected function _getPasswordParam()
{
$pwd = '';
if (!empty($this->_password)) {
$pwd = ' -p' . escapeshellarg($this->_password);
}
return $pwd;
}
}