ZipFile.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. <?php
  2. namespace Nelexa\Zip;
  3. use Nelexa\Buffer\Buffer;
  4. use Nelexa\Buffer\BufferException;
  5. use Nelexa\Buffer\MathHelper;
  6. use Nelexa\Buffer\MemoryResourceBuffer;
  7. use Nelexa\Buffer\ResourceBuffer;
  8. use Nelexa\Buffer\StringBuffer;
  9. class ZipFile
  10. {
  11. const SIGNATURE_LOCAL_HEADER = 0x04034b50;
  12. const SIGNATURE_CENTRAL_DIR = 0x02014b50;
  13. const SIGNATURE_END_CENTRAL_DIR = 0x06054b50;
  14. private static $initPwdKeys = array(305419896, 591751049, 878082192);
  15. /**
  16. * @var string
  17. */
  18. private $filename;
  19. /**
  20. * @var Buffer
  21. */
  22. private $buffer;
  23. /**
  24. * @var int
  25. */
  26. private $offsetCentralDirectory;
  27. /**
  28. * @var int
  29. */
  30. private $sizeCentralDirectory = 0;
  31. /**
  32. * @var ZipEntry[]
  33. */
  34. private $zipEntries;
  35. /**
  36. * @var string[]
  37. */
  38. private $zipEntriesIndex;
  39. /**
  40. * @var string
  41. */
  42. private $zipComment = "";
  43. /**
  44. * @var string
  45. */
  46. private $password = null;
  47. public function __construct()
  48. {
  49. }
  50. /**
  51. * Create zip archive
  52. */
  53. public function create()
  54. {
  55. $this->filename = null;
  56. $this->zipEntries = array();
  57. $this->zipEntriesIndex = array();
  58. $this->zipComment = "";
  59. $this->offsetCentralDirectory = 0;
  60. $this->sizeCentralDirectory = 0;
  61. $this->buffer = new MemoryResourceBuffer();
  62. $this->buffer->setOrder(Buffer::LITTLE_ENDIAN);
  63. $this->buffer->insertInt(self::SIGNATURE_END_CENTRAL_DIR);
  64. $this->buffer->insertString(str_repeat("\0", 18));
  65. }
  66. /**
  67. * Open exists zip archive
  68. *
  69. * @param string $filename
  70. * @throws ZipException
  71. */
  72. public function open($filename)
  73. {
  74. if (!file_exists($filename)) {
  75. throw new ZipException("Can not open file");
  76. }
  77. $this->filename = $filename;
  78. $this->openFromString(file_get_contents($this->filename));
  79. }
  80. public function openFromString($string)
  81. {
  82. $this->zipEntries = null;
  83. $this->zipEntriesIndex = null;
  84. $this->zipComment = "";
  85. $this->offsetCentralDirectory = null;
  86. $this->sizeCentralDirectory = 0;
  87. $this->password = null;
  88. $this->buffer = new StringBuffer($string);
  89. $this->buffer->setOrder(Buffer::LITTLE_ENDIAN);
  90. $this->findAndReadEndCentralDirectory();
  91. }
  92. /**
  93. * Set password
  94. *
  95. * @param string $password
  96. */
  97. public function setPassword($password)
  98. {
  99. $this->password = $password;
  100. }
  101. /**
  102. * Find end central catalog
  103. *
  104. * @throws BufferException
  105. * @throws ZipException
  106. */
  107. private function findAndReadEndCentralDirectory()
  108. {
  109. if ($this->buffer->size() < 26) {
  110. return;
  111. }
  112. $this->buffer->setPosition($this->buffer->size() - 22);
  113. $endOfCentralDirSignature = $this->buffer->getUnsignedInt();
  114. if ($endOfCentralDirSignature === self::SIGNATURE_END_CENTRAL_DIR) {
  115. $this->readEndCentralDirectory();
  116. } else {
  117. $maximumSize = 65557;
  118. if ($this->buffer->size() < $maximumSize) {
  119. $maximumSize = $this->buffer->size();
  120. }
  121. $this->buffer->skip(-$maximumSize);
  122. $bytes = 0x00000000;
  123. while ($this->buffer->hasRemaining()) {
  124. $byte = $this->buffer->getUnsignedByte();
  125. $bytes = (($bytes & 0xFFFFFF) << 8) | $byte;
  126. if ($bytes === 0x504b0506) {
  127. $this->readEndCentralDirectory();
  128. return;
  129. }
  130. }
  131. throw new ZipException("Unable to find End of Central Dir Record signature");
  132. }
  133. }
  134. /**
  135. * Read end central catalog
  136. *
  137. * @throws BufferException
  138. * @throws ZipException
  139. */
  140. private function readEndCentralDirectory()
  141. {
  142. $this->buffer->skip(4); // number of this disk AND number of the disk with the start of the central directory
  143. $countFiles = $this->buffer->getUnsignedShort();
  144. $this->buffer->skip(2); // total number of entries in the central directory
  145. $this->sizeCentralDirectory = $this->buffer->getUnsignedInt();
  146. $this->offsetCentralDirectory = $this->buffer->getUnsignedInt();
  147. $zipCommentLength = $this->buffer->getUnsignedShort();
  148. $this->zipComment = $this->buffer->getString($zipCommentLength);
  149. $this->buffer->setPosition($this->offsetCentralDirectory);
  150. $this->zipEntries = array();
  151. $this->zipEntriesIndex = array();
  152. for ($i = 0; $i < $countFiles; $i++) {
  153. $offsetOfCentral = $this->buffer->position() - $this->offsetCentralDirectory;
  154. $zipEntry = new ZipEntry();
  155. $zipEntry->readCentralHeader($this->buffer);
  156. $zipEntry->setOffsetOfCentral($offsetOfCentral);
  157. $this->zipEntries[$i] = $zipEntry;
  158. $this->zipEntriesIndex[$zipEntry->getName()] = $i;
  159. }
  160. }
  161. /**
  162. * @return int
  163. */
  164. public function getCountFiles()
  165. {
  166. return $this->zipEntries === null ? 0 : sizeof($this->zipEntries);
  167. }
  168. /**
  169. * Add empty directory in zip archive
  170. *
  171. * @param string $dirName
  172. * @return bool
  173. * @throws ZipException
  174. */
  175. public function addEmptyDir($dirName)
  176. {
  177. if ($dirName === null) {
  178. throw new ZipException("dirName null");
  179. }
  180. $dirName = rtrim($dirName, '/') . '/';
  181. if (isset($this->zipEntriesIndex[$dirName])) {
  182. return true;
  183. }
  184. $zipEntry = new ZipEntry();
  185. $zipEntry->setName($dirName);
  186. $zipEntry->setCompressionMethod(0);
  187. $zipEntry->setLastModDateTime(time());
  188. $zipEntry->setCrc32(0);
  189. $zipEntry->setCompressedSize(0);
  190. $zipEntry->setUnCompressedSize(0);
  191. $zipEntry->setOffsetOfLocal($this->offsetCentralDirectory);
  192. $this->buffer->setPosition($zipEntry->getOffsetOfLocal());
  193. $bufferLocal = $zipEntry->writeLocalHeader();
  194. $this->buffer->insert($bufferLocal);
  195. $this->offsetCentralDirectory += $bufferLocal->size();
  196. $zipEntry->setOffsetOfCentral($this->sizeCentralDirectory);
  197. $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral());
  198. $bufferCentral = $zipEntry->writeCentralHeader();
  199. $this->buffer->insert($bufferCentral);
  200. $this->sizeCentralDirectory += $bufferCentral->size();
  201. $this->zipEntries[] = $zipEntry;
  202. end($this->zipEntries);
  203. $this->zipEntriesIndex[$zipEntry->getName()] = key($this->zipEntries);
  204. $size = $this->getCountFiles();
  205. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 8);
  206. // $signature = $this->buffer->getUnsignedInt();
  207. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  208. // throw new ZipException("error position end central dir");
  209. // }
  210. // $this->buffer->skip(4);
  211. $this->buffer->putShort($size);
  212. $this->buffer->putShort($size);
  213. $this->buffer->putInt($this->sizeCentralDirectory);
  214. $this->buffer->putInt($this->offsetCentralDirectory);
  215. return true;
  216. }
  217. /**
  218. * @param string $inDirectory
  219. * @param string|null $addPath
  220. * @param array $ignoreFiles
  221. * @return bool
  222. * @throws ZipException
  223. */
  224. public function addDir($inDirectory, $addPath = null, array $ignoreFiles = array())
  225. {
  226. if ($inDirectory === null) {
  227. throw new ZipException("dirName null");
  228. }
  229. if (!file_exists($inDirectory)) {
  230. throw new ZipException("directory not found");
  231. }
  232. if (!is_dir($inDirectory)) {
  233. throw new ZipException("input directory is not directory");
  234. }
  235. if ($addPath !== null && is_string($addPath) && !empty($addPath)) {
  236. $addPath = rtrim($addPath, '/');
  237. } else {
  238. $addPath = "";
  239. }
  240. $inDirectory = rtrim($inDirectory, '/');
  241. $iterator = new FilterFileIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($inDirectory)), $ignoreFiles);
  242. $files = iterator_to_array($iterator, false);
  243. $count = $this->getCountFiles();
  244. /**
  245. * @var \SplFileInfo $file
  246. */
  247. foreach ($files as $file) {
  248. if ($file->getFilename() === '.') {
  249. $filename = dirname(str_replace($inDirectory, $addPath, $file));
  250. $this->isEmptyDir($file) && $this->addEmptyDir($filename);
  251. } else if ($file->isFile()) {
  252. $filename = str_replace($inDirectory, $addPath, $file);
  253. $this->addFile($file, $filename);
  254. }
  255. }
  256. return $this->getCountFiles() > $count;
  257. }
  258. public function addGlob($pattern, $removePath = null, $addPath = null, $recursive = true)
  259. {
  260. if ($pattern === null) {
  261. throw new ZipException("pattern null");
  262. }
  263. $glob = $this->globFileSearch($pattern, GLOB_BRACE, $recursive);
  264. if ($glob === FALSE || empty($glob)) {
  265. return false;
  266. }
  267. if (!empty($addPath) && is_string($addPath)) {
  268. $addPath = rtrim($addPath, '/');
  269. } else {
  270. $addPath = "";
  271. }
  272. if (!empty($removePath) && is_string($removePath)) {
  273. $removePath = rtrim($removePath, '/');
  274. } else {
  275. $removePath = "";
  276. }
  277. $count = $this->getCountFiles();
  278. /**
  279. * @var string $file
  280. */
  281. foreach ($glob as $file) {
  282. if (is_dir($file)) {
  283. $filename = str_replace($addPath, $removePath, $file);
  284. $this->isEmptyDir($file) && $this->addEmptyDir($filename);
  285. } else if (is_file($file)) {
  286. $filename = str_replace($removePath, $addPath, $file);
  287. $this->addFile($file, $filename);
  288. }
  289. }
  290. return $this->getCountFiles() > $count;
  291. }
  292. public function addPattern($pattern, $inDirectory, $addPath = null, $recursive = true)
  293. {
  294. if ($pattern === null) {
  295. throw new ZipException("pattern null");
  296. }
  297. $files = $this->regexFileSearch($inDirectory, $pattern, $recursive);
  298. if ($files === FALSE || empty($files)) {
  299. return false;
  300. }
  301. if (!empty($addPath) && is_string($addPath)) {
  302. $addPath = rtrim($addPath, '/');
  303. } else {
  304. $addPath = "";
  305. }
  306. $inDirectory = rtrim($inDirectory, '/');
  307. $count = $this->getCountFiles();
  308. /**
  309. * @var string $file
  310. */
  311. foreach ($files as $file) {
  312. if (is_dir($file)) {
  313. $filename = str_replace($addPath, $inDirectory, $file);
  314. $this->isEmptyDir($file) && $this->addEmptyDir($filename);
  315. } else if (is_file($file)) {
  316. $filename = str_replace($inDirectory, $addPath, $file);
  317. $this->addFile($file, $filename);
  318. }
  319. }
  320. return $this->getCountFiles() > $count;
  321. }
  322. private function globFileSearch($pattern, $flags = 0, $recursive = true)
  323. {
  324. $files = glob($pattern, $flags);
  325. if (!$recursive) return $files;
  326. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  327. $files = array_merge($files, $this->globFileSearch($dir . '/' . basename($pattern), $flags, $recursive));
  328. }
  329. return $files;
  330. }
  331. private function regexFileSearch($folder, $pattern, $recursive = true)
  332. {
  333. $dir = $recursive ? new \RecursiveDirectoryIterator($folder) : new \DirectoryIterator($folder);
  334. $ite = $recursive ? new \RecursiveIteratorIterator($dir) : new \IteratorIterator($dir);
  335. $files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH);
  336. $fileList = array();
  337. foreach ($files as $file) {
  338. $fileList = array_merge($fileList, $file);
  339. }
  340. return $fileList;
  341. }
  342. private function isEmptyDir($dir)
  343. {
  344. if (!is_readable($dir)) return false;
  345. return (count(scandir($dir)) == 2);
  346. }
  347. /**
  348. * Add file in zip archive
  349. *
  350. * @param string $filename
  351. * @param string|null $localName
  352. * @param int|null $compressionMethod
  353. * @throws ZipException
  354. */
  355. public function addFile($filename, $localName = NULL, $compressionMethod = null)
  356. {
  357. if ($filename === null) {
  358. throw new ZipException("filename null");
  359. }
  360. if (!file_exists($filename)) {
  361. throw new ZipException("file not found");
  362. }
  363. if (!is_file($filename)) {
  364. throw new ZipException("input filename is not file");
  365. }
  366. if ($localName === null) {
  367. $localName = basename($filename);
  368. }
  369. $this->addFromString($localName, file_get_contents($filename), $compressionMethod);
  370. }
  371. /**
  372. * @param string $localName
  373. * @param string $contents
  374. * @param int|null $compressionMethod
  375. * @throws ZipException
  376. */
  377. public function addFromString($localName, $contents, $compressionMethod = null)
  378. {
  379. if ($localName === null || !is_string($localName) || strlen($localName) === 0) {
  380. throw new ZipException("local name empty");
  381. }
  382. if ($contents === null) {
  383. throw new ZipException("contents null");
  384. }
  385. $unCompressedSize = strlen($contents);
  386. $compress = null;
  387. if ($compressionMethod === null) {
  388. if ($unCompressedSize === 0) {
  389. $compressionMethod = ZipEntry::COMPRESS_METHOD_STORED;
  390. } else {
  391. $compressionMethod = ZipEntry::COMPRESS_METHOD_DEFLATED;
  392. }
  393. }
  394. switch ($compressionMethod) {
  395. case ZipEntry::COMPRESS_METHOD_STORED:
  396. $compress = $contents;
  397. break;
  398. case ZipEntry::COMPRESS_METHOD_DEFLATED:
  399. $compress = gzdeflate($contents);
  400. break;
  401. default:
  402. throw new ZipException("Compression method not support");
  403. }
  404. $crc32 = sprintf('%u', crc32($contents));
  405. $compressedSize = strlen($compress);
  406. if (isset($this->zipEntriesIndex[$localName])) {
  407. /**
  408. * @var int $index
  409. */
  410. $index = $this->zipEntriesIndex[$localName];
  411. $zipEntry = &$this->zipEntries[$index];
  412. $oldCompressedSize = $zipEntry->getCompressedSize();
  413. $zipEntry->setCompressionMethod($compressionMethod);
  414. $zipEntry->setLastModDateTime(time());
  415. $zipEntry->setCompressedSize($compressedSize);
  416. $zipEntry->setUnCompressedSize($unCompressedSize);
  417. $zipEntry->setCrc32($crc32);
  418. $this->buffer->setPosition($zipEntry->getOffsetOfLocal() + 8);
  419. $this->buffer->putShort($zipEntry->getCompressionMethod());
  420. $this->buffer->putShort($zipEntry->getLastModifyDosTime());
  421. $this->buffer->putShort($zipEntry->getLastModifyDosDate());
  422. if ($zipEntry->hasDataDescriptor()) {
  423. $this->buffer->skip(12);
  424. } else {
  425. $this->buffer->putInt($zipEntry->getCrc32());
  426. $this->buffer->putInt($zipEntry->getCompressedSize());
  427. $this->buffer->putInt($zipEntry->getUnCompressedSize());
  428. }
  429. $this->buffer->skip(4 + strlen($zipEntry->getName()) + strlen($zipEntry->getExtraLocal()));
  430. $this->buffer->replaceString($compress, $oldCompressedSize);
  431. if ($zipEntry->hasDataDescriptor()) {
  432. $this->buffer->put($zipEntry->writeDataDescriptor());
  433. }
  434. $diff = $oldCompressedSize - $zipEntry->getCompressedSize();
  435. if ($diff !== 0) {
  436. $this->offsetCentralDirectory -= $diff;
  437. }
  438. $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral() + 10);
  439. $this->buffer->putShort($zipEntry->getCompressionMethod());
  440. $this->buffer->putShort($zipEntry->getLastModifyDosTime());
  441. $this->buffer->putShort($zipEntry->getLastModifyDosDate());
  442. $this->buffer->putInt($zipEntry->getCrc32());
  443. $this->buffer->putInt($zipEntry->getCompressedSize());
  444. $this->buffer->putInt($zipEntry->getUnCompressedSize());
  445. if ($diff !== 0) {
  446. $this->buffer->skip(18 + strlen($zipEntry->getName()) + strlen($zipEntry->getExtraCentral()) + strlen($zipEntry->getComment()));
  447. $size = $this->getCountFiles();
  448. /**
  449. * @var ZipEntry $entry
  450. */
  451. for ($i = $index + 1; $i < $size; $i++) {
  452. $zipEntry = &$this->zipEntries[$i];
  453. $zipEntry->setOffsetOfLocal($zipEntry->getOffsetOfLocal() - $diff);
  454. $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral() + 42);
  455. // $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral());
  456. // $sig = $this->buffer->getUnsignedInt();
  457. // if ($sig !== self::SIGNATURE_CENTRAL_DIR) {
  458. // $this->buffer->skip(-4);
  459. // throw new ZipException("Signature central dir corrupt. Bad signature = 0x" . dechex($sig) . "; Current entry: " . $entry->getName());
  460. // }
  461. // $this->buffer->skip(38);
  462. $this->buffer->putInt($zipEntry->getOffsetOfLocal());
  463. }
  464. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 12);
  465. // $signature = $this->buffer->getUnsignedInt();
  466. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  467. // throw new ZipException("error position end central dir");
  468. // }
  469. // $this->buffer->skip(8);
  470. $this->buffer->putInt($this->sizeCentralDirectory);
  471. $this->buffer->putInt($this->offsetCentralDirectory);
  472. }
  473. } else {
  474. $zipEntry = new ZipEntry();
  475. // if ($flagBit > 0) $zipEntry->setFlagBit($flagBit);
  476. $zipEntry->setName($localName);
  477. $zipEntry->setCompressionMethod($compressionMethod);
  478. $zipEntry->setLastModDateTime(time());
  479. $zipEntry->setCrc32($crc32);
  480. $zipEntry->setCompressedSize($compressedSize);
  481. $zipEntry->setUnCompressedSize($unCompressedSize);
  482. $zipEntry->setOffsetOfLocal($this->offsetCentralDirectory);
  483. $bufferLocal = $zipEntry->writeLocalHeader();
  484. $bufferLocal->insertString($compress);
  485. if ($zipEntry->hasDataDescriptor()) {
  486. $bufferLocal->insert($zipEntry->writeDataDescriptor());
  487. }
  488. $this->buffer->setPosition($zipEntry->getOffsetOfLocal());
  489. $this->buffer->insert($bufferLocal);
  490. $this->offsetCentralDirectory += $bufferLocal->size();
  491. $zipEntry->setOffsetOfCentral($this->sizeCentralDirectory);
  492. $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral());
  493. $bufferCentral = $zipEntry->writeCentralHeader();
  494. $this->buffer->insert($bufferCentral);
  495. $this->sizeCentralDirectory += $bufferCentral->size();
  496. $this->zipEntries[] = $zipEntry;
  497. end($this->zipEntries);
  498. $this->zipEntriesIndex[$zipEntry->getName()] = key($this->zipEntries);
  499. $size = $this->getCountFiles();
  500. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 8);
  501. // $signature = $this->buffer->getUnsignedInt();
  502. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  503. // throw new ZipException("error position end central dir");
  504. // }
  505. // $this->buffer->skip(4);
  506. $this->buffer->putShort($size);
  507. $this->buffer->putShort($size);
  508. $this->buffer->putInt($this->sizeCentralDirectory);
  509. $this->buffer->putInt($this->offsetCentralDirectory);
  510. }
  511. }
  512. /**
  513. * Update timestamp archive for all files
  514. *
  515. * @param int|null $timestamp
  516. * @throws BufferException
  517. */
  518. public function updateTimestamp($timestamp = null)
  519. {
  520. if ($timestamp === null || !is_int($timestamp)) {
  521. $timestamp = time();
  522. }
  523. foreach ($this->zipEntries AS $entry) {
  524. $entry->setLastModDateTime($timestamp);
  525. $this->buffer->setPosition($entry->getOffsetOfLocal() + 10);
  526. $this->buffer->putShort($entry->getLastModifyDosTime());
  527. $this->buffer->putShort($entry->getLastModifyDosDate());
  528. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() + 12);
  529. $this->buffer->putShort($entry->getLastModifyDosTime());
  530. $this->buffer->putShort($entry->getLastModifyDosDate());
  531. }
  532. }
  533. public function deleteGlob($pattern)
  534. {
  535. if ($pattern === null) {
  536. throw new ZipException("pattern null");
  537. }
  538. $pattern = '~' . $this->convertGlobToRegEx($pattern) . '~si';
  539. return $this->deletePattern($pattern);
  540. }
  541. public function deletePattern($pattern)
  542. {
  543. if ($pattern === null) {
  544. throw new ZipException("pattern null");
  545. }
  546. $offsetLocal = 0;
  547. $offsetCentral = 0;
  548. $modify = false;
  549. foreach ($this->zipEntries AS $index => &$entry) {
  550. if (preg_match($pattern, $entry->getName())) {
  551. $this->buffer->setPosition($entry->getOffsetOfLocal() - $offsetLocal);
  552. $lengthLocal = $entry->getLengthOfLocal();
  553. $this->buffer->remove($lengthLocal);
  554. $offsetLocal += $lengthLocal;
  555. $this->offsetCentralDirectory -= $lengthLocal;
  556. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() - $offsetCentral);
  557. $lengthCentral = $entry->getLengthOfCentral();
  558. $this->buffer->remove($lengthCentral);
  559. $offsetCentral += $lengthCentral;
  560. $this->sizeCentralDirectory -= $lengthCentral;
  561. unset($this->zipEntries[$index], $this->zipEntriesIndex[$entry->getName()]);
  562. $modify = true;
  563. continue;
  564. }
  565. if ($modify) {
  566. $entry->setOffsetOfLocal($entry->getOffsetOfLocal() - $offsetLocal);
  567. $entry->setOffsetOfCentral($entry->getOffsetOfCentral() - $offsetCentral);
  568. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() + 42);
  569. $this->buffer->putInt($entry->getOffsetOfLocal());
  570. }
  571. }
  572. if ($modify) {
  573. $size = $this->getCountFiles();
  574. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 8);
  575. // $signature = $this->buffer->getUnsignedInt();
  576. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  577. // throw new ZipException("error position end central dir");
  578. // }
  579. // $this->buffer->skip(4);
  580. $this->buffer->putShort($size);
  581. $this->buffer->putShort($size);
  582. $this->buffer->putInt($this->sizeCentralDirectory);
  583. $this->buffer->putInt($this->offsetCentralDirectory);
  584. return true;
  585. }
  586. return false;
  587. }
  588. /**
  589. * @param int $index
  590. * @return bool
  591. * @throws ZipException
  592. */
  593. public function deleteIndex($index)
  594. {
  595. if ($index === null || !is_numeric($index)) {
  596. throw new ZipException("index no numeric");
  597. }
  598. if (!isset($this->zipEntries[$index])) {
  599. return false;
  600. }
  601. $entry = $this->zipEntries[$index];
  602. $offsetCentral = $entry->getOffsetOfCentral();
  603. $lengthCentral = $entry->getLengthOfCentral();
  604. $offsetLocal = $entry->getOffsetOfLocal();
  605. $lengthLocal = $entry->getLengthOfLocal();
  606. unset(
  607. $this->zipEntries[$index],
  608. $this->zipEntriesIndex[$entry->getName()]
  609. );
  610. $this->zipEntries = array_values($this->zipEntries);
  611. $this->zipEntriesIndex = array_flip(array_keys($this->zipEntriesIndex));
  612. $size = $this->getCountFiles();
  613. $this->buffer->setPosition($this->offsetCentralDirectory + $offsetCentral);
  614. $this->buffer->remove($lengthCentral);
  615. $this->buffer->setPosition($offsetLocal);
  616. $this->buffer->remove($lengthLocal);
  617. $this->offsetCentralDirectory -= $lengthLocal;
  618. $this->sizeCentralDirectory -= $lengthCentral;
  619. /**
  620. * @var ZipEntry $entry
  621. */
  622. for ($i = $index; $i < $size; $i++) {
  623. $entry = &$this->zipEntries[$i];
  624. $entry->setOffsetOfLocal($entry->getOffsetOfLocal() - $lengthLocal);
  625. // $this->buffer->setPosition($entry->getOffsetOfLocal());
  626. // $sig = $this->buffer->getUnsignedInt();
  627. // if ($sig !== self::SIGNATURE_LOCAL_HEADER) {
  628. // throw new ZipException("Signature local header corrupt");
  629. // }
  630. $entry->setOffsetOfCentral($entry->getOffsetOfCentral() - $lengthCentral);
  631. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() + 42);
  632. // $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral());
  633. // $sig = $this->buffer->getUnsignedInt();
  634. // if ($sig !== self::SIGNATURE_CENTRAL_DIR) {
  635. // $this->buffer->skip(-4);
  636. // throw new ZipException("Signature central dir corrupt. Bad signature = 0x" . dechex($sig) . "; Current entry: " . $entry->getName());
  637. // }
  638. // $this->buffer->skip(38);
  639. $this->buffer->putInt($entry->getOffsetOfLocal());
  640. }
  641. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 8);
  642. // $signature = $this->buffer->getUnsignedInt();
  643. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  644. // throw new ZipException("error position end central dir");
  645. // }
  646. // $this->buffer->skip(4);
  647. $this->buffer->putShort($size);
  648. $this->buffer->putShort($size);
  649. $this->buffer->putInt($this->sizeCentralDirectory);
  650. $this->buffer->putInt($this->offsetCentralDirectory);
  651. return true;
  652. }
  653. public function deleteAll()
  654. {
  655. $this->zipEntries = array();
  656. $this->zipEntriesIndex = array();
  657. $this->offsetCentralDirectory = 0;
  658. $this->sizeCentralDirectory = 0;
  659. $this->buffer->truncate();
  660. $this->buffer->insertInt(self::SIGNATURE_END_CENTRAL_DIR);
  661. $this->buffer->insertString(str_repeat("\0", 18));
  662. }
  663. /**
  664. * @param $name
  665. * @return bool
  666. * @throws ZipException
  667. */
  668. public function deleteName($name)
  669. {
  670. if (empty($name)) {
  671. throw new ZipException("name is empty");
  672. }
  673. if (!isset($this->zipEntriesIndex[$name])) {
  674. return false;
  675. }
  676. $index = $this->zipEntriesIndex[$name];
  677. return $this->deleteIndex($index);
  678. }
  679. /**
  680. * @param string $destination
  681. * @param array $entries
  682. * @return bool
  683. * @throws ZipException
  684. */
  685. public function extractTo($destination, array $entries = null)
  686. {
  687. if ($this->zipEntries === NULL) {
  688. throw new ZipException("zip entries not initial");
  689. }
  690. if (!file_exists($destination)) {
  691. throw new ZipException("Destination " . $destination . " not found");
  692. }
  693. if (!is_dir($destination)) {
  694. throw new ZipException("Destination is not directory");
  695. }
  696. if (!is_writable($destination)) {
  697. throw new ZipException("Destination is not writable directory");
  698. }
  699. /**
  700. * @var ZipEntry[] $zipEntries
  701. */
  702. if ($entries !== null && is_array($entries) && !empty($entries)) {
  703. $flipEntries = array_flip($entries);
  704. $zipEntries = array_filter($this->zipEntries, function ($zipEntry) use ($flipEntries) {
  705. /**
  706. * @var ZipEntry $zipEntry
  707. */
  708. return isset($flipEntries[$zipEntry->getName()]);
  709. });
  710. } else {
  711. $zipEntries = $this->zipEntries;
  712. }
  713. $extract = 0;
  714. foreach ($zipEntries AS $entry) {
  715. $file = $destination . '/' . $entry->getName();
  716. $dir = dirname($file);
  717. if (!file_exists($dir)) {
  718. if (!mkdir($dir, 0755, true)) {
  719. throw new ZipException("Can not create dir " . $dir);
  720. }
  721. chmod($dir, 0755);
  722. }
  723. if ($entry->isDirectory()) {
  724. continue;
  725. }
  726. if (file_put_contents($file, $this->getEntryBytes($entry)) === FALSE) {
  727. return false;
  728. }
  729. touch($file, $entry->getLastModDateTime());
  730. $extract++;
  731. }
  732. return $extract > 0;
  733. }
  734. /**
  735. * @param ZipEntry $entry
  736. * @return string
  737. * @throws BufferException
  738. * @throws ZipException
  739. */
  740. private function getEntryBytes(ZipEntry $entry)
  741. {
  742. $this->buffer->setPosition($entry->getOffsetOfLocal() + $entry->getLengthLocalHeader());
  743. // $this->buffer->setPosition($entry->getOffsetOfLocal());
  744. // $signature = $this->buffer->getUnsignedInt();
  745. // if ($signature !== self::SIGNATURE_LOCAL_HEADER) {
  746. // throw new ZipException("Can not read entry " . $entry->getName());
  747. // }
  748. // $this->buffer->skip($entry->getLengthLocalHeader() - 4);
  749. $string = $this->buffer->getString($entry->getCompressedSize());
  750. if ($entry->isEncrypted()) {
  751. if (empty($this->password)) {
  752. throw new ZipException("need password archive");
  753. }
  754. $pwdKeys = self::$initPwdKeys;
  755. $bufPass = new StringBuffer($this->password);
  756. while ($bufPass->hasRemaining()) {
  757. $byte = $bufPass->getUnsignedByte();
  758. $pwdKeys = ZipUtils::updateKeys($byte, $pwdKeys);
  759. }
  760. unset($bufPass);
  761. $keys = $pwdKeys;
  762. $strBuffer = new StringBuffer($string);
  763. for ($i = 0; $i < ZipUtils::DECRYPT_HEADER_SIZE; $i++) {
  764. $result = $strBuffer->getUnsignedByte();
  765. $lastValue = $result ^ ZipUtils::decryptByte($keys[2]);
  766. $keys = ZipUtils::updateKeys($lastValue, $keys);
  767. }
  768. $string = "";
  769. while ($strBuffer->hasRemaining()) {
  770. $result = $strBuffer->getUnsignedByte();
  771. $result = ($result ^ ZipUtils::decryptByte($keys[2])) & 0xff;
  772. $keys = ZipUtils::updateKeys(MathHelper::castToByte($result), $keys);
  773. $string .= chr($result);
  774. }
  775. unset($strBuffer);
  776. }
  777. switch ($entry->getCompressionMethod()) {
  778. case ZipEntry::COMPRESS_METHOD_DEFLATED:
  779. $string = @gzinflate($string);
  780. break;
  781. case ZipEntry::COMPRESS_METHOD_STORED:
  782. break;
  783. default:
  784. throw new ZipException("Compression method " . $entry->compressionMethodToString() . " not support!");
  785. }
  786. $expectedCrc = sprintf('%u', crc32($string));
  787. if ($expectedCrc != $entry->getCrc32()) {
  788. if ($entry->isEncrypted()) {
  789. throw new ZipException("Wrong password");
  790. }
  791. throw new ZipException("File " . $entry->getName() . ' corrupt. Bad CRC ' . dechex($expectedCrc) . ' (should be ' . dechex($entry->getCrc32()) . ')');
  792. }
  793. return $string;
  794. }
  795. /**
  796. * @return string
  797. */
  798. public function getArchiveComment()
  799. {
  800. return $this->zipComment;
  801. }
  802. /**
  803. * @param $index
  804. * @return string
  805. * @throws ZipException
  806. */
  807. public function getCommentIndex($index)
  808. {
  809. if (!isset($this->zipEntries[$index])) {
  810. throw new ZipException("File for index " . $index . " not found");
  811. }
  812. return $this->zipEntries[$index]->getComment();
  813. }
  814. /**
  815. * @param string $name
  816. * @return string
  817. * @throws ZipException
  818. */
  819. public function getCommentName($name)
  820. {
  821. if (!isset($this->zipEntriesIndex[$name])) {
  822. throw new ZipException("File for name " . $name . " not found");
  823. }
  824. $index = $this->zipEntriesIndex[$name];
  825. return $this->getCommentIndex($index);
  826. }
  827. /**
  828. * @param int $index
  829. * @return string
  830. * @throws ZipException
  831. */
  832. public function getFromIndex($index)
  833. {
  834. if (!isset($this->zipEntries[$index])) {
  835. throw new ZipException("File for index " . $index . " not found");
  836. }
  837. return $this->getEntryBytes($this->zipEntries[$index]);
  838. }
  839. /**
  840. * @param string $name
  841. * @return string
  842. * @throws ZipException
  843. */
  844. public function getFromName($name)
  845. {
  846. if (!isset($this->zipEntriesIndex[$name])) {
  847. throw new ZipException("File for name " . $name . " not found");
  848. }
  849. $index = $this->zipEntriesIndex[$name];
  850. return $this->getEntryBytes($this->zipEntries[$index]);
  851. }
  852. /**
  853. * @param int $index
  854. * @return string
  855. * @throws ZipException
  856. */
  857. public function getNameIndex($index)
  858. {
  859. if (!isset($this->zipEntries[$index])) {
  860. throw new ZipException("File for index " . $index . " not found");
  861. }
  862. return $this->zipEntries[$index]->getName();
  863. }
  864. /**
  865. * @param string $name
  866. * @return bool|string
  867. */
  868. public function locateName($name)
  869. {
  870. return isset($this->zipEntriesIndex[$name]) ? $this->zipEntriesIndex[$name] : false;
  871. }
  872. /**
  873. * @param int $index
  874. * @param string $newName
  875. * @return bool
  876. * @throws ZipException
  877. */
  878. public function renameIndex($index, $newName)
  879. {
  880. if (!isset($this->zipEntries[$index])) {
  881. throw new ZipException("File for index " . $index . " not found");
  882. }
  883. $lengthNewName = strlen($newName);
  884. if (strlen($lengthNewName) > 0xFF) {
  885. throw new ZipException("Length new name is very long. Maximum size 255");
  886. }
  887. $entry = &$this->zipEntries[$index];
  888. if ($entry->getName() === $newName) {
  889. return true;
  890. }
  891. if (isset($this->zipEntriesIndex[$newName])) {
  892. return false;
  893. }
  894. $lengthOldName = strlen($entry->getName());
  895. $this->buffer->setPosition($entry->getOffsetOfLocal() + 26);
  896. $this->buffer->putShort($lengthNewName);
  897. $this->buffer->skip(2);
  898. if ($lengthOldName === $lengthNewName) {
  899. $this->buffer->putString($newName);
  900. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() + 46);
  901. $this->buffer->putString($newName);
  902. } else {
  903. $this->buffer->replaceString($newName, $lengthOldName);
  904. $diff = $lengthOldName - $lengthNewName;
  905. $this->offsetCentralDirectory -= $diff;
  906. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() + 28);
  907. $this->buffer->putShort($lengthNewName);
  908. $this->buffer->skip(16);
  909. $this->buffer->replaceString($newName, $lengthOldName);
  910. $this->sizeCentralDirectory -= $diff;
  911. $size = $this->getCountFiles();
  912. for ($i = $index + 1; $i < $size; $i++) {
  913. $zipEntry = &$this->zipEntries[$i];
  914. $zipEntry->setOffsetOfLocal($zipEntry->getOffsetOfLocal() - $diff);
  915. $zipEntry->setOffsetOfCentral($zipEntry->getOffsetOfCentral() - $diff);
  916. $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral() + 42);
  917. // $this->buffer->setPosition($this->offsetCentralDirectory + $zipEntry->getOffsetOfCentral());
  918. // $sig = $this->buffer->getUnsignedInt();
  919. // if ($sig !== self::SIGNATURE_CENTRAL_DIR) {
  920. // $this->buffer->skip(-4);
  921. // throw new ZipException("Signature central dir corrupt. Bad signature = 0x" . dechex($sig) . "; Current entry: " . $entry->getName());
  922. // }
  923. // $this->buffer->skip(38);
  924. $this->buffer->putInt($zipEntry->getOffsetOfLocal());
  925. }
  926. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 12);
  927. // $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory);
  928. // $signature = $this->buffer->getUnsignedInt();
  929. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  930. // throw new ZipException("error position end central dir");
  931. // }
  932. // $this->buffer->skip(8);
  933. $this->buffer->putInt($this->sizeCentralDirectory);
  934. $this->buffer->putInt($this->offsetCentralDirectory);
  935. }
  936. $entry->setName($newName);
  937. return true;
  938. }
  939. /**
  940. * @param string $name
  941. * @param string $newName
  942. * @return bool
  943. * @throws ZipException
  944. */
  945. public function renameName($name, $newName)
  946. {
  947. if (!isset($this->zipEntriesIndex[$name])) {
  948. throw new ZipException("File for name " . $name . " not found");
  949. }
  950. $index = $this->zipEntriesIndex[$name];
  951. return $this->renameIndex($index, $newName);
  952. }
  953. /**
  954. * @param string $comment
  955. * @return bool
  956. * @throws ZipException
  957. */
  958. public function setArchiveComment($comment)
  959. {
  960. if ($comment === null) {
  961. return false;
  962. }
  963. if ($comment === $this->zipComment) {
  964. return true;
  965. }
  966. $currentCommentLength = strlen($this->zipComment);
  967. $commentLength = strlen($comment);
  968. if ($commentLength > 0xffff) {
  969. $commentLength = 0xffff;
  970. $comment = substr($comment, 0, $commentLength);
  971. }
  972. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 20);
  973. // $signature = $this->buffer->getUnsignedInt();
  974. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  975. // throw new ZipException("error position end central dir");
  976. // }
  977. // $this->buffer->skip(16);
  978. $this->buffer->putShort($commentLength);
  979. $this->buffer->replaceString($comment, $currentCommentLength);
  980. $this->zipComment = $comment;
  981. return true;
  982. }
  983. /**
  984. * Set the comment of an entry defined by its index
  985. *
  986. * @param int $index
  987. * @param string $comment
  988. * @return bool
  989. * @throws ZipException
  990. */
  991. public function setCommentIndex($index, $comment)
  992. {
  993. if (!isset($this->zipEntries[$index])) {
  994. throw new ZipException("File for index " . $index . " not found");
  995. }
  996. if ($comment === null) {
  997. return false;
  998. }
  999. $newCommentLength = strlen($comment);
  1000. if ($newCommentLength > 0xffff) {
  1001. $newCommentLength = 0xffff;
  1002. $comment = substr($comment, 0, $newCommentLength);
  1003. }
  1004. $entry = &$this->zipEntries[$index];
  1005. $oldComment = $entry->getComment();
  1006. $oldCommentLength = strlen($oldComment);
  1007. $this->buffer->setPosition($this->offsetCentralDirectory + $entry->getOffsetOfCentral() + 32);
  1008. $this->buffer->putShort($newCommentLength);
  1009. $this->buffer->skip(12 + strlen($entry->getName()) + strlen($entry->getExtraCentral()));
  1010. if ($oldCommentLength === $newCommentLength) {
  1011. $this->buffer->putString($comment);
  1012. } else {
  1013. $this->buffer->replaceString($comment, $oldCommentLength);
  1014. $diff = $oldCommentLength - $newCommentLength;
  1015. $this->sizeCentralDirectory -= $diff;
  1016. $size = $this->getCountFiles();
  1017. /**
  1018. * @var ZipEntry $entry
  1019. */
  1020. for ($i = $index + 1; $i < $size; $i++) {
  1021. $zipEntry = &$this->zipEntries[$i];
  1022. $zipEntry->setOffsetOfCentral($zipEntry->getOffsetOfCentral() - $diff);
  1023. }
  1024. $this->buffer->setPosition($this->offsetCentralDirectory + $this->sizeCentralDirectory + 12);
  1025. // $signature = $this->buffer->getUnsignedInt();
  1026. // if ($signature !== self::SIGNATURE_END_CENTRAL_DIR) {
  1027. // throw new ZipException("error position end central dir");
  1028. // }
  1029. // $this->buffer->skip(8);
  1030. $this->buffer->putInt($this->sizeCentralDirectory);
  1031. }
  1032. $entry->setComment($comment);
  1033. return true;
  1034. }
  1035. /**
  1036. * @return ZipEntry[]
  1037. */
  1038. public function getZipEntries()
  1039. {
  1040. return $this->zipEntries;
  1041. }
  1042. /**
  1043. * @param int $index
  1044. * @return ZipEntry|bool
  1045. */
  1046. public function getZipEntryIndex($index)
  1047. {
  1048. return isset($this->zipEntries[$index]) ? $this->zipEntries[$index] : false;
  1049. }
  1050. /**
  1051. * @param string $name
  1052. * @return ZipEntry|bool
  1053. */
  1054. public function getZipEntryName($name)
  1055. {
  1056. return isset($this->zipEntriesIndex[$name]) ? $this->zipEntries[$this->zipEntriesIndex[$name]] : false;
  1057. }
  1058. /**
  1059. * Set the comment of an entry defined by its name
  1060. *
  1061. * @param string $name
  1062. * @param string $comment
  1063. * @return bool
  1064. * @throws ZipException
  1065. */
  1066. public function setCommentName($name, $comment)
  1067. {
  1068. if (!isset($this->zipEntriesIndex[$name])) {
  1069. throw new ZipException("File for name " . $name . " not found");
  1070. }
  1071. $index = $this->zipEntriesIndex[$name];
  1072. return $this->setCommentIndex($index, $comment);
  1073. }
  1074. /**
  1075. * @param $index
  1076. * @return array
  1077. * @throws ZipException
  1078. */
  1079. public function statIndex($index)
  1080. {
  1081. if (!isset($this->zipEntries[$index])) {
  1082. throw new ZipException("File for index " . $index . " not found");
  1083. }
  1084. $entry = $this->zipEntries[$index];
  1085. return array(
  1086. 'name' => $entry->getName(),
  1087. 'index' => $index,
  1088. 'crc' => $entry->getCrc32(),
  1089. 'size' => $entry->getUnCompressedSize(),
  1090. 'mtime' => $entry->getLastModDateTime(),
  1091. 'comp_size' => $entry->getCompressedSize(),
  1092. 'comp_method' => $entry->getCompressionMethod()
  1093. );
  1094. }
  1095. /**
  1096. * @param string $name
  1097. * @return array
  1098. * @throws ZipException
  1099. */
  1100. public function statName($name)
  1101. {
  1102. if (!isset($this->zipEntriesIndex[$name])) {
  1103. throw new ZipException("File for name " . $name . " not found");
  1104. }
  1105. $index = $this->zipEntriesIndex[$name];
  1106. return $this->statIndex($index);
  1107. }
  1108. public function getListFiles()
  1109. {
  1110. return array_flip($this->zipEntriesIndex);
  1111. }
  1112. /**
  1113. * @return array
  1114. */
  1115. public function getExtendedListFiles()
  1116. {
  1117. return array_map(function ($index, $entry) {
  1118. /**
  1119. * @var ZipEntry $entry
  1120. * @var int $index
  1121. */
  1122. return array(
  1123. 'name' => $entry->getName(),
  1124. 'index' => $index,
  1125. 'crc' => $entry->getCrc32(),
  1126. 'size' => $entry->getUnCompressedSize(),
  1127. 'mtime' => $entry->getLastModDateTime(),
  1128. 'comp_size' => $entry->getUnCompressedSize(),
  1129. 'comp_method' => $entry->getCompressionMethod()
  1130. );
  1131. }, array_keys($this->zipEntries), $this->zipEntries);
  1132. }
  1133. public function output()
  1134. {
  1135. return $this->buffer->toString();
  1136. }
  1137. /**
  1138. * @param string $file
  1139. * @return bool
  1140. */
  1141. public function saveAs($file)
  1142. {
  1143. return file_put_contents($file, $this->output()) !== false;
  1144. }
  1145. /**
  1146. * @return bool
  1147. */
  1148. public function save()
  1149. {
  1150. if ($this->filename !== NULL) {
  1151. return file_put_contents($this->filename, $this->output()) !== false;
  1152. }
  1153. return false;
  1154. }
  1155. public function close()
  1156. {
  1157. if ($this->buffer !== null) {
  1158. ($this->buffer instanceof ResourceBuffer) && $this->buffer->close();
  1159. }
  1160. $this->zipEntries = null;
  1161. $this->zipEntriesIndex = null;
  1162. $this->zipComment = null;
  1163. $this->buffer = null;
  1164. $this->filename = null;
  1165. $this->offsetCentralDirectory = null;
  1166. }
  1167. function __destruct()
  1168. {
  1169. $this->close();
  1170. }
  1171. private static function convertGlobToRegEx($pattern)
  1172. {
  1173. $pattern = trim($pattern, '*'); // Remove beginning and ending * globs because they're useless
  1174. $escaping = false;
  1175. $inCurlies = 0;
  1176. $chars = str_split($pattern);
  1177. $sb = '';
  1178. foreach ($chars AS $currentChar) {
  1179. switch ($currentChar) {
  1180. case '*':
  1181. $sb .= ($escaping ? "\\*" : '.*');
  1182. $escaping = false;
  1183. break;
  1184. case '?':
  1185. $sb .= ($escaping ? "\\?" : '.');
  1186. $escaping = false;
  1187. break;
  1188. case '.':
  1189. case '(':
  1190. case ')':
  1191. case '+':
  1192. case '|':
  1193. case '^':
  1194. case '$':
  1195. case '@':
  1196. case '%':
  1197. $sb .= '\\' . $currentChar;
  1198. $escaping = false;
  1199. break;
  1200. case '\\':
  1201. if ($escaping) {
  1202. $sb .= "\\\\";
  1203. $escaping = false;
  1204. } else {
  1205. $escaping = true;
  1206. }
  1207. break;
  1208. case '{':
  1209. if ($escaping) {
  1210. $sb .= "\\{";
  1211. } else {
  1212. $sb = '(';
  1213. $inCurlies++;
  1214. }
  1215. $escaping = false;
  1216. break;
  1217. case '}':
  1218. if ($inCurlies > 0 && !$escaping) {
  1219. $sb .= ')';
  1220. $inCurlies--;
  1221. } else if ($escaping)
  1222. $sb = "\\}";
  1223. else
  1224. $sb = "}";
  1225. $escaping = false;
  1226. break;
  1227. case ',':
  1228. if ($inCurlies > 0 && !$escaping) {
  1229. $sb .= '|';
  1230. } else if ($escaping)
  1231. $sb .= "\\,";
  1232. else
  1233. $sb = ",";
  1234. break;
  1235. default:
  1236. $escaping = false;
  1237. $sb .= $currentChar;
  1238. }
  1239. }
  1240. return $sb;
  1241. }
  1242. }