File.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZipStream;
  4. use Psr\Http\Message\StreamInterface;
  5. use ZipStream\Exception\EncodingException;
  6. use ZipStream\Exception\FileNotFoundException;
  7. use ZipStream\Exception\FileNotReadableException;
  8. use ZipStream\Exception\OverflowException;
  9. use ZipStream\Option\File as FileOptions;
  10. use ZipStream\Option\Method;
  11. use ZipStream\Option\Version;
  12. class File
  13. {
  14. const HASH_ALGORITHM = 'crc32b';
  15. const BIT_ZERO_HEADER = 0x0008;
  16. const BIT_EFS_UTF8 = 0x0800;
  17. const COMPUTE = 1;
  18. const SEND = 2;
  19. private const CHUNKED_READ_BLOCK_SIZE = 1048576;
  20. /**
  21. * @var string
  22. */
  23. public $name;
  24. /**
  25. * @var FileOptions
  26. */
  27. public $opt;
  28. /**
  29. * @var Bigint
  30. */
  31. public $len;
  32. /**
  33. * @var Bigint
  34. */
  35. public $zlen;
  36. /** @var int */
  37. public $crc;
  38. /**
  39. * @var Bigint
  40. */
  41. public $hlen;
  42. /**
  43. * @var Bigint
  44. */
  45. public $ofs;
  46. /**
  47. * @var int
  48. */
  49. public $bits;
  50. /**
  51. * @var Version
  52. */
  53. public $version;
  54. /**
  55. * @var ZipStream
  56. */
  57. public $zip;
  58. /**
  59. * @var resource
  60. */
  61. private $deflate;
  62. /**
  63. * @var resource
  64. */
  65. private $hash;
  66. /**
  67. * @var Method
  68. */
  69. private $method;
  70. /**
  71. * @var Bigint
  72. */
  73. private $totalLength;
  74. public function __construct(ZipStream $zip, string $name, ?FileOptions $opt = null)
  75. {
  76. $this->zip = $zip;
  77. $this->name = $name;
  78. $this->opt = $opt ?: new FileOptions();
  79. $this->method = $this->opt->getMethod();
  80. $this->version = Version::STORE();
  81. $this->ofs = new Bigint();
  82. }
  83. public function processPath(string $path): void
  84. {
  85. if (!is_readable($path)) {
  86. if (!file_exists($path)) {
  87. throw new FileNotFoundException($path);
  88. }
  89. throw new FileNotReadableException($path);
  90. }
  91. if ($this->zip->isLargeFile($path) === false) {
  92. $data = file_get_contents($path);
  93. $this->processData($data);
  94. } else {
  95. $this->method = $this->zip->opt->getLargeFileMethod();
  96. $stream = new DeflateStream(fopen($path, 'rb'));
  97. $this->processStream($stream);
  98. $stream->close();
  99. }
  100. }
  101. public function processData(string $data): void
  102. {
  103. $this->len = new Bigint(strlen($data));
  104. $this->crc = crc32($data);
  105. // compress data if needed
  106. if ($this->method->equals(Method::DEFLATE())) {
  107. $data = gzdeflate($data);
  108. }
  109. $this->zlen = new Bigint(strlen($data));
  110. $this->addFileHeader();
  111. $this->zip->send($data);
  112. $this->addFileFooter();
  113. }
  114. /**
  115. * Create and send zip header for this file.
  116. *
  117. * @return void
  118. * @throws \ZipStream\Exception\EncodingException
  119. */
  120. public function addFileHeader(): void
  121. {
  122. $name = static::filterFilename($this->name);
  123. // calculate name length
  124. $nameLength = strlen($name);
  125. // create dos timestamp
  126. $time = static::dosTime($this->opt->getTime()->getTimestamp());
  127. $comment = $this->opt->getComment();
  128. if (!mb_check_encoding($name, 'ASCII') ||
  129. !mb_check_encoding($comment, 'ASCII')) {
  130. // Sets Bit 11: Language encoding flag (EFS). If this bit is set,
  131. // the filename and comment fields for this file
  132. // MUST be encoded using UTF-8. (see APPENDIX D)
  133. if (!mb_check_encoding($name, 'UTF-8') ||
  134. !mb_check_encoding($comment, 'UTF-8')) {
  135. throw new EncodingException(
  136. 'File name and comment should use UTF-8 ' .
  137. 'if one of them does not fit into ASCII range.'
  138. );
  139. }
  140. $this->bits |= self::BIT_EFS_UTF8;
  141. }
  142. if ($this->method->equals(Method::DEFLATE())) {
  143. $this->version = Version::DEFLATE();
  144. }
  145. $force = (boolean)($this->bits & self::BIT_ZERO_HEADER) &&
  146. $this->zip->opt->isEnableZip64();
  147. $footer = $this->buildZip64ExtraBlock($force);
  148. // If this file will start over 4GB limit in ZIP file,
  149. // CDR record will have to use Zip64 extension to describe offset
  150. // to keep consistency we use the same value here
  151. if ($this->zip->ofs->isOver32()) {
  152. $this->version = Version::ZIP64();
  153. }
  154. $fields = [
  155. ['V', ZipStream::FILE_HEADER_SIGNATURE],
  156. ['v', $this->version->getValue()], // Version needed to Extract
  157. ['v', $this->bits], // General purpose bit flags - data descriptor flag set
  158. ['v', $this->method->getValue()], // Compression method
  159. ['V', $time], // Timestamp (DOS Format)
  160. ['V', $this->crc], // CRC32 of data (0 -> moved to data descriptor footer)
  161. ['V', $this->zlen->getLowFF($force)], // Length of compressed data (forced to 0xFFFFFFFF for zero header)
  162. ['V', $this->len->getLowFF($force)], // Length of original data (forced to 0xFFFFFFFF for zero header)
  163. ['v', $nameLength], // Length of filename
  164. ['v', strlen($footer)], // Extra data (see above)
  165. ];
  166. // pack fields and calculate "total" length
  167. $header = ZipStream::packFields($fields);
  168. // print header and filename
  169. $data = $header . $name . $footer;
  170. $this->zip->send($data);
  171. // save header length
  172. $this->hlen = Bigint::init(strlen($data));
  173. }
  174. /**
  175. * Strip characters that are not legal in Windows filenames
  176. * to prevent compatibility issues
  177. *
  178. * @param string $filename Unprocessed filename
  179. * @return string
  180. */
  181. public static function filterFilename(string $filename): string
  182. {
  183. // strip leading slashes from file name
  184. // (fixes bug in windows archive viewer)
  185. $filename = preg_replace('/^\\/+/', '', $filename);
  186. return str_replace(['\\', ':', '*', '?', '"', '<', '>', '|'], '_', $filename);
  187. }
  188. /**
  189. * Convert a UNIX timestamp to a DOS timestamp.
  190. *
  191. * @param int $when
  192. * @return int DOS Timestamp
  193. */
  194. final protected static function dosTime(int $when): int
  195. {
  196. // get date array for timestamp
  197. $d = getdate($when);
  198. // set lower-bound on dates
  199. if ($d['year'] < 1980) {
  200. $d = array(
  201. 'year' => 1980,
  202. 'mon' => 1,
  203. 'mday' => 1,
  204. 'hours' => 0,
  205. 'minutes' => 0,
  206. 'seconds' => 0
  207. );
  208. }
  209. // remove extra years from 1980
  210. $d['year'] -= 1980;
  211. // return date string
  212. return
  213. ($d['year'] << 25) |
  214. ($d['mon'] << 21) |
  215. ($d['mday'] << 16) |
  216. ($d['hours'] << 11) |
  217. ($d['minutes'] << 5) |
  218. ($d['seconds'] >> 1);
  219. }
  220. protected function buildZip64ExtraBlock(bool $force = false): string
  221. {
  222. $fields = [];
  223. if ($this->len->isOver32($force)) {
  224. $fields[] = ['P', $this->len]; // Length of original data
  225. }
  226. if ($this->len->isOver32($force)) {
  227. $fields[] = ['P', $this->zlen]; // Length of compressed data
  228. }
  229. if ($this->ofs->isOver32()) {
  230. $fields[] = ['P', $this->ofs]; // Offset of local header record
  231. }
  232. if (!empty($fields)) {
  233. if (!$this->zip->opt->isEnableZip64()) {
  234. throw new OverflowException();
  235. }
  236. array_unshift(
  237. $fields,
  238. ['v', 0x0001], // 64 bit extension
  239. ['v', count($fields) * 8] // Length of data block
  240. );
  241. $this->version = Version::ZIP64();
  242. }
  243. return ZipStream::packFields($fields);
  244. }
  245. /**
  246. * Create and send data descriptor footer for this file.
  247. *
  248. * @return void
  249. */
  250. public function addFileFooter(): void
  251. {
  252. if ($this->bits & self::BIT_ZERO_HEADER) {
  253. // compressed and uncompressed size
  254. $sizeFormat = 'V';
  255. if ($this->zip->opt->isEnableZip64()) {
  256. $sizeFormat = 'P';
  257. }
  258. $fields = [
  259. ['V', ZipStream::DATA_DESCRIPTOR_SIGNATURE],
  260. ['V', $this->crc], // CRC32
  261. [$sizeFormat, $this->zlen], // Length of compressed data
  262. [$sizeFormat, $this->len], // Length of original data
  263. ];
  264. $footer = ZipStream::packFields($fields);
  265. $this->zip->send($footer);
  266. } else {
  267. $footer = '';
  268. }
  269. $this->totalLength = $this->hlen->add($this->zlen)->add(Bigint::init(strlen($footer)));
  270. $this->zip->addToCdr($this);
  271. }
  272. public function processStream(StreamInterface $stream): void
  273. {
  274. $this->zlen = new Bigint();
  275. $this->len = new Bigint();
  276. if ($this->zip->opt->isZeroHeader()) {
  277. $this->processStreamWithZeroHeader($stream);
  278. } else {
  279. $this->processStreamWithComputedHeader($stream);
  280. }
  281. }
  282. protected function processStreamWithZeroHeader(StreamInterface $stream): void
  283. {
  284. $this->bits |= self::BIT_ZERO_HEADER;
  285. $this->addFileHeader();
  286. $this->readStream($stream, self::COMPUTE | self::SEND);
  287. $this->addFileFooter();
  288. }
  289. protected function readStream(StreamInterface $stream, ?int $options = null): void
  290. {
  291. $this->deflateInit();
  292. $total = 0;
  293. $size = $this->opt->getSize();
  294. while (!$stream->eof() && ($size === 0 || $total < $size)) {
  295. $data = $stream->read(self::CHUNKED_READ_BLOCK_SIZE);
  296. $total += strlen($data);
  297. if ($size > 0 && $total > $size) {
  298. $data = substr($data, 0 , strlen($data)-($total - $size));
  299. }
  300. $this->deflateData($stream, $data, $options);
  301. if ($options & self::SEND) {
  302. $this->zip->send($data);
  303. }
  304. }
  305. $this->deflateFinish($options);
  306. }
  307. protected function deflateInit(): void
  308. {
  309. $this->hash = hash_init(self::HASH_ALGORITHM);
  310. if ($this->method->equals(Method::DEFLATE())) {
  311. $this->deflate = deflate_init(
  312. ZLIB_ENCODING_RAW,
  313. ['level' => $this->opt->getDeflateLevel()]
  314. );
  315. }
  316. }
  317. protected function deflateData(StreamInterface $stream, string &$data, ?int $options = null): void
  318. {
  319. if ($options & self::COMPUTE) {
  320. $this->len = $this->len->add(Bigint::init(strlen($data)));
  321. hash_update($this->hash, $data);
  322. }
  323. if ($this->deflate) {
  324. $data = deflate_add(
  325. $this->deflate,
  326. $data,
  327. $stream->eof()
  328. ? ZLIB_FINISH
  329. : ZLIB_NO_FLUSH
  330. );
  331. }
  332. if ($options & self::COMPUTE) {
  333. $this->zlen = $this->zlen->add(Bigint::init(strlen($data)));
  334. }
  335. }
  336. protected function deflateFinish(?int $options = null): void
  337. {
  338. if ($options & self::COMPUTE) {
  339. $this->crc = hexdec(hash_final($this->hash));
  340. }
  341. }
  342. protected function processStreamWithComputedHeader(StreamInterface $stream): void
  343. {
  344. $this->readStream($stream, self::COMPUTE);
  345. $stream->rewind();
  346. // incremental compression with deflate_add
  347. // makes this second read unnecessary
  348. // but it is only available from PHP 7.0
  349. if (!$this->deflate && $stream instanceof DeflateStream && $this->method->equals(Method::DEFLATE())) {
  350. $stream->addDeflateFilter($this->opt);
  351. $this->zlen = new Bigint();
  352. while (!$stream->eof()) {
  353. $data = $stream->read(self::CHUNKED_READ_BLOCK_SIZE);
  354. $this->zlen = $this->zlen->add(Bigint::init(strlen($data)));
  355. }
  356. $stream->rewind();
  357. }
  358. $this->addFileHeader();
  359. $this->readStream($stream, self::SEND);
  360. $this->addFileFooter();
  361. }
  362. /**
  363. * Send CDR record for specified file.
  364. *
  365. * @return string
  366. */
  367. public function getCdrFile(): string
  368. {
  369. $name = static::filterFilename($this->name);
  370. // get attributes
  371. $comment = $this->opt->getComment();
  372. // get dos timestamp
  373. $time = static::dosTime($this->opt->getTime()->getTimestamp());
  374. $footer = $this->buildZip64ExtraBlock();
  375. $fields = [
  376. ['V', ZipStream::CDR_FILE_SIGNATURE], // Central file header signature
  377. ['v', ZipStream::ZIP_VERSION_MADE_BY], // Made by version
  378. ['v', $this->version->getValue()], // Extract by version
  379. ['v', $this->bits], // General purpose bit flags - data descriptor flag set
  380. ['v', $this->method->getValue()], // Compression method
  381. ['V', $time], // Timestamp (DOS Format)
  382. ['V', $this->crc], // CRC32
  383. ['V', $this->zlen->getLowFF()], // Compressed Data Length
  384. ['V', $this->len->getLowFF()], // Original Data Length
  385. ['v', strlen($name)], // Length of filename
  386. ['v', strlen($footer)], // Extra data len (see above)
  387. ['v', strlen($comment)], // Length of comment
  388. ['v', 0], // Disk number
  389. ['v', 0], // Internal File Attributes
  390. ['V', 32], // External File Attributes
  391. ['V', $this->ofs->getLowFF()] // Relative offset of local header
  392. ];
  393. // pack fields, then append name and comment
  394. $header = ZipStream::packFields($fields);
  395. return $header . $name . $footer . $comment;
  396. }
  397. /**
  398. * @return Bigint
  399. */
  400. public function getTotalLength(): Bigint
  401. {
  402. return $this->totalLength;
  403. }
  404. }