Blame view
sources/lib/private/archive/zip.php
4.67 KB
|
03e52840d
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_Archive_ZIP extends OC_Archive{
/**
* @var ZipArchive zip
*/
private $zip=null;
private $path;
|
|
6d9380f96
|
15 16 17 |
/** * @param string $source */ |
|
03e52840d
|
18 19 20 21 22 23 24 25 26 27 |
function __construct($source) {
$this->path=$source;
$this->zip=new ZipArchive();
if($this->zip->open($source, ZipArchive::CREATE)) {
}else{
OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, OCP\Util::WARN);
}
}
/**
* add an empty folder to the archive
|
|
6d9380f96
|
28 |
* @param string $path |
|
03e52840d
|
29 30 31 32 33 34 35 |
* @return bool
*/
function addFolder($path) {
return $this->zip->addEmptyDir($path);
}
/**
* add a file to the archive
|
|
6d9380f96
|
36 37 |
* @param string $path * @param string $source either a local file or string data |
|
03e52840d
|
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
* @return bool
*/
function addFile($path, $source='') {
if($source and $source[0]=='/' and file_exists($source)) {
$result=$this->zip->addFile($source, $path);
}else{
$result=$this->zip->addFromString($path, $source);
}
if($result) {
$this->zip->close();//close and reopen to save the zip
$this->zip->open($this->path);
}
return $result;
}
/**
* rename a file or folder in the archive
|
|
6d9380f96
|
54 55 56 |
* @param string $source * @param string $dest * @return boolean|null |
|
03e52840d
|
57 58 59 60 61 62 63 64 |
*/
function rename($source, $dest) {
$source=$this->stripPath($source);
$dest=$this->stripPath($dest);
$this->zip->renameName($source, $dest);
}
/**
* get the uncompressed size of a file in the archive
|
|
6d9380f96
|
65 |
* @param string $path |
|
03e52840d
|
66 67 68 69 70 71 72 73 |
* @return int
*/
function filesize($path) {
$stat=$this->zip->statName($path);
return $stat['size'];
}
/**
* get the last modified time of a file in the archive
|
|
6d9380f96
|
74 |
* @param string $path |
|
03e52840d
|
75 76 77 78 79 80 81 |
* @return int
*/
function mtime($path) {
return filemtime($this->path);
}
/**
* get the files in a folder
|
|
6d9380f96
|
82 |
* @param string $path |
|
03e52840d
|
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
* @return array
*/
function getFolder($path) {
$files=$this->getFiles();
$folderContent=array();
$pathLength=strlen($path);
foreach($files as $file) {
if(substr($file, 0, $pathLength)==$path and $file!=$path) {
if(strrpos(substr($file, 0, -1), '/')<=$pathLength) {
$folderContent[]=substr($file, $pathLength);
}
}
}
return $folderContent;
}
/**
|
|
31b7f2792
|
99 |
* get all files in the archive |
|
03e52840d
|
100 101 102 103 104 105 106 107 108 109 110 111 |
* @return array
*/
function getFiles() {
$fileCount=$this->zip->numFiles;
$files=array();
for($i=0;$i<$fileCount;$i++) {
$files[]=$this->zip->getNameIndex($i);
}
return $files;
}
/**
* get the content of a file
|
|
6d9380f96
|
112 |
* @param string $path |
|
03e52840d
|
113 114 115 116 117 118 119 |
* @return string
*/
function getFile($path) {
return $this->zip->getFromName($path);
}
/**
* extract a single file from the archive
|
|
6d9380f96
|
120 121 122 |
* @param string $path * @param string $dest * @return boolean|null |
|
03e52840d
|
123 124 125 126 127 128 129 |
*/
function extractFile($path, $dest) {
$fp = $this->zip->getStream($path);
file_put_contents($dest, $fp);
}
/**
* extract the archive
|
|
6d9380f96
|
130 |
* @param string $dest |
|
03e52840d
|
131 132 133 134 135 136 137 |
* @return bool
*/
function extract($dest) {
return $this->zip->extractTo($dest);
}
/**
* check if a file or folder exists in the archive
|
|
6d9380f96
|
138 |
* @param string $path |
|
03e52840d
|
139 140 141 142 143 144 145 |
* @return bool
*/
function fileExists($path) {
return ($this->zip->locateName($path)!==false) or ($this->zip->locateName($path.'/')!==false);
}
/**
* remove a file or folder from the archive
|
|
6d9380f96
|
146 |
* @param string $path |
|
03e52840d
|
147 148 149 150 151 152 153 154 155 156 157 |
* @return bool
*/
function remove($path) {
if($this->fileExists($path.'/')) {
return $this->zip->deleteName($path.'/');
}else{
return $this->zip->deleteName($path);
}
}
/**
* get a file handler
|
|
6d9380f96
|
158 159 |
* @param string $path * @param string $mode |
|
03e52840d
|
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
* @return resource
*/
function getStream($path, $mode) {
if($mode=='r' or $mode=='rb') {
return $this->zip->getStream($path);
} else {
//since we cant directly get a writable stream,
//make a temp copy of the file and put it back
//in the archive when the stream is closed
if(strrpos($path, '.')!==false) {
$ext=substr($path, strrpos($path, '.'));
}else{
$ext='';
}
$tmpFile=OCP\Files::tmpFile($ext);
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if($this->fileExists($path)) {
$this->extractFile($path, $tmpFile);
}
self::$tempFiles[$tmpFile]=$path;
return fopen('close://'.$tmpFile, $mode);
}
}
private static $tempFiles=array();
/**
* write back temporary files
*/
function writeBack($tmpFile) {
if(isset(self::$tempFiles[$tmpFile])) {
$this->addFile(self::$tempFiles[$tmpFile], $tmpFile);
unlink($tmpFile);
}
}
|
|
6d9380f96
|
194 195 196 197 |
/** * @param string $path * @return string */ |
|
03e52840d
|
198 199 200 201 202 203 204 205 |
private function stripPath($path) {
if(!$path || $path[0]=='/') {
return substr($path, 1);
}else{
return $path;
}
}
}
|