{offset of the local header, size, crc (hex), method}. * * @param string $path Part file. * @return array[] * @throws \RuntimeException When the file isn't a readable ZIP. */ public static function entries( $path ) { $size = (int) @filesize( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $fh = @fopen( $path, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! $fh || $size < 22 ) { throw new \RuntimeException( 'The archive part is missing or empty.' ); } // End of central directory: within the last 64 KB (it may be followed by a comment). $tail_len = (int) min( $size, 65557 ); fseek( $fh, $size - $tail_len ); $tail = (string) fread( $fh, $tail_len ); $pos = strrpos( $tail, pack( 'V', 0x06054b50 ) ); if ( false === $pos || strlen( $tail ) - $pos < 22 ) { fclose( $fh ); throw new \RuntimeException( 'The archive part is damaged (no central directory).' ); } $eocd = unpack( 'Vsig/vdisk/vcd_disk/vcount_disk/vcount/Vcd_size/Vcd_offset/vcomment', substr( $tail, $pos, 22 ) ); if ( $eocd['cd_offset'] + $eocd['cd_size'] > $size ) { fclose( $fh ); throw new \RuntimeException( 'The archive part is damaged (truncated).' ); } fseek( $fh, $eocd['cd_offset'] ); $cd = (string) fread( $fh, $eocd['cd_size'] ); fclose( $fh ); $out = array(); $at = 0; for ( $i = 0; $i < $eocd['count']; $i++ ) { if ( strlen( $cd ) < $at + 46 ) { throw new \RuntimeException( 'The archive part is damaged (central directory).' ); } $h = unpack( 'Vsig/vmade/vneed/vflags/vmethod/vtime/vdate/Vcrc/Vcsize/Vsize/vname_len/vextra_len/vcomment_len/vdisk/vint/Vext/Voffset', substr( $cd, $at, 46 ) ); if ( 0x02014b50 !== $h['sig'] ) { throw new \RuntimeException( 'The archive part is damaged (central directory).' ); } $name = substr( $cd, $at + 46, $h['name_len'] ); $out[ $name ] = array( 'offset' => $h['offset'], 'size' => $h['size'], 'csize' => $h['csize'], 'crc' => str_pad( dechex( $h['crc'] ), 8, '0', STR_PAD_LEFT ), 'method' => $h['method'], ); $at += 46 + $h['name_len'] + $h['extra_len'] + $h['comment_len']; } return $out; } /** * Copies one entry into a file (or appends it at an offset, for a file stored in segments). * * @param string $path Part file. * @param array $entry Entry (from entries()). * @param string $dest Destination file. * @param int $at Offset in the destination to write at. * @return string SHA-256 of the entry's data. * @throws \RuntimeException When the entry is damaged or can't be written. */ public static function extract( $path, array $entry, $dest, $at = 0 ) { if ( 0 !== (int) $entry['method'] || (int) $entry['csize'] !== (int) $entry['size'] ) { throw new \RuntimeException( 'The archive part uses compression this version cannot read.' ); } $in = @fopen( $path, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! $in ) { throw new \RuntimeException( 'Could not read the archive part.' ); } fseek( $in, (int) $entry['offset'] ); $head = (string) fread( $in, 30 ); $h = strlen( $head ) === 30 ? unpack( 'Vsig/vneed/vflags/vmethod/vtime/vdate/Vcrc/Vcsize/Vsize/vname_len/vextra_len', $head ) : null; if ( ! $h || 0x04034b50 !== $h['sig'] ) { fclose( $in ); throw new \RuntimeException( 'The archive part is damaged (entry header).' ); } fseek( $in, (int) $entry['offset'] + 30 + $h['name_len'] + $h['extra_len'] ); // Offset 0 starts the file afresh (a leftover from an earlier attempt must not add bytes). $out = @fopen( $dest, 0 === (int) $at ? 'wb' : 'c+b' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! $out ) { fclose( $in ); throw new \RuntimeException( 'Could not write the extracted file. Is the disk full?' ); } fseek( $out, (int) $at ); $crc = hash_init( 'crc32b' ); $sha = hash_init( 'sha256' ); $left = (int) $entry['size']; $ok = true; while ( $left > 0 ) { $block = fread( $in, (int) min( 1048576, $left ) ); if ( false === $block || '' === $block ) { $ok = false; break; } hash_update( $crc, $block ); hash_update( $sha, $block ); if ( fwrite( $out, $block ) !== strlen( $block ) ) { $ok = false; break; } $left -= strlen( $block ); } fclose( $in ); fclose( $out ); if ( ! $ok ) { throw new \RuntimeException( 'Could not extract a file (the part is truncated or the disk is full).' ); } if ( hash_final( $crc ) !== $entry['crc'] ) { throw new \RuntimeException( 'A file in the archive part is damaged (checksum mismatch).' ); } return hash_final( $sha ); } }