ZIP.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Stefan Weil <sw@weilnetz.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Archive;
  31. use Icewind\Streams\CallbackWrapper;
  32. use OCP\ILogger;
  33. class ZIP extends Archive{
  34. /**
  35. * @var \ZipArchive zip
  36. */
  37. private $zip=null;
  38. private $path;
  39. /**
  40. * @param string $source
  41. */
  42. public function __construct($source) {
  43. $this->path=$source;
  44. $this->zip=new \ZipArchive();
  45. if($this->zip->open($source, \ZipArchive::CREATE)) {
  46. }else{
  47. \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
  48. }
  49. }
  50. /**
  51. * add an empty folder to the archive
  52. * @param string $path
  53. * @return bool
  54. */
  55. public function addFolder($path) {
  56. return $this->zip->addEmptyDir($path);
  57. }
  58. /**
  59. * add a file to the archive
  60. * @param string $path
  61. * @param string $source either a local file or string data
  62. * @return bool
  63. */
  64. public function addFile($path, $source='') {
  65. if($source and $source[0]=='/' and file_exists($source)) {
  66. $result=$this->zip->addFile($source, $path);
  67. }else{
  68. $result=$this->zip->addFromString($path, $source);
  69. }
  70. if($result) {
  71. $this->zip->close();//close and reopen to save the zip
  72. $this->zip->open($this->path);
  73. }
  74. return $result;
  75. }
  76. /**
  77. * rename a file or folder in the archive
  78. * @param string $source
  79. * @param string $dest
  80. * @return boolean|null
  81. */
  82. public function rename($source, $dest) {
  83. $source=$this->stripPath($source);
  84. $dest=$this->stripPath($dest);
  85. $this->zip->renameName($source, $dest);
  86. }
  87. /**
  88. * get the uncompressed size of a file in the archive
  89. * @param string $path
  90. * @return int
  91. */
  92. public function filesize($path) {
  93. $stat=$this->zip->statName($path);
  94. return $stat['size'];
  95. }
  96. /**
  97. * get the last modified time of a file in the archive
  98. * @param string $path
  99. * @return int
  100. */
  101. public function mtime($path) {
  102. return filemtime($this->path);
  103. }
  104. /**
  105. * get the files in a folder
  106. * @param string $path
  107. * @return array
  108. */
  109. public function getFolder($path) {
  110. $files=$this->getFiles();
  111. $folderContent=array();
  112. $pathLength=strlen($path);
  113. foreach($files as $file) {
  114. if(substr($file, 0, $pathLength)==$path and $file!=$path) {
  115. if(strrpos(substr($file, 0, -1), '/')<=$pathLength) {
  116. $folderContent[]=substr($file, $pathLength);
  117. }
  118. }
  119. }
  120. return $folderContent;
  121. }
  122. /**
  123. * get all files in the archive
  124. * @return array
  125. */
  126. public function getFiles() {
  127. $fileCount=$this->zip->numFiles;
  128. $files=array();
  129. for($i=0;$i<$fileCount;$i++) {
  130. $files[]=$this->zip->getNameIndex($i);
  131. }
  132. return $files;
  133. }
  134. /**
  135. * get the content of a file
  136. * @param string $path
  137. * @return string
  138. */
  139. public function getFile($path) {
  140. return $this->zip->getFromName($path);
  141. }
  142. /**
  143. * extract a single file from the archive
  144. * @param string $path
  145. * @param string $dest
  146. * @return boolean|null
  147. */
  148. public function extractFile($path, $dest) {
  149. $fp = $this->zip->getStream($path);
  150. file_put_contents($dest, $fp);
  151. }
  152. /**
  153. * extract the archive
  154. * @param string $dest
  155. * @return bool
  156. */
  157. public function extract($dest) {
  158. return $this->zip->extractTo($dest);
  159. }
  160. /**
  161. * check if a file or folder exists in the archive
  162. * @param string $path
  163. * @return bool
  164. */
  165. public function fileExists($path) {
  166. return ($this->zip->locateName($path)!==false) or ($this->zip->locateName($path.'/')!==false);
  167. }
  168. /**
  169. * remove a file or folder from the archive
  170. * @param string $path
  171. * @return bool
  172. */
  173. public function remove($path) {
  174. if($this->fileExists($path.'/')) {
  175. return $this->zip->deleteName($path.'/');
  176. }else{
  177. return $this->zip->deleteName($path);
  178. }
  179. }
  180. /**
  181. * get a file handler
  182. * @param string $path
  183. * @param string $mode
  184. * @return resource
  185. */
  186. public function getStream($path, $mode) {
  187. if($mode=='r' or $mode=='rb') {
  188. return $this->zip->getStream($path);
  189. } else {
  190. //since we can't directly get a writable stream,
  191. //make a temp copy of the file and put it back
  192. //in the archive when the stream is closed
  193. if(strrpos($path, '.')!==false) {
  194. $ext=substr($path, strrpos($path, '.'));
  195. }else{
  196. $ext='';
  197. }
  198. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  199. if($this->fileExists($path)) {
  200. $this->extractFile($path, $tmpFile);
  201. }
  202. $handle = fopen($tmpFile, $mode);
  203. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  204. $this->writeBack($tmpFile, $path);
  205. });
  206. }
  207. }
  208. /**
  209. * write back temporary files
  210. */
  211. public function writeBack($tmpFile, $path) {
  212. $this->addFile($path, $tmpFile);
  213. unlink($tmpFile);
  214. }
  215. /**
  216. * @param string $path
  217. * @return string
  218. */
  219. private function stripPath($path) {
  220. if(!$path || $path[0]=='/') {
  221. return substr($path, 1);
  222. }else{
  223. return $path;
  224. }
  225. }
  226. }