<commit_before><?php

namespace Coverslide;

class ZipReader
{
    const COMPRESSION_UNCOMPRESSED  =   0;
    const COMPRESSION_DEFLATE       =   8;

    const HEADER_SIZE_FILEHEADER    =   30;
    const HEADER_SIZE_CDHEADER      =   46;
    const HEADER_SIZE_ENDHEADER     =   22;

    private $fileHandle;

    public function __construct($path)
    {
        $this->fileHandle = fopen($path, 'rb');
    }

    public function __destruct()
    {
        fclose($this->fileHandle);
    }

    public function listFiles()
    {
        fseek($this->fileHandle, 0 - self::HEADER_SIZE_ENDHEADER, SEEK_END);

        $endBlock = fread($this->fileHandle, self::HEADER_SIZE_ENDHEADER);

        $endBlockInfo = $this->unpackEndBlock($endBlock);

        fseek($this->fileHandle, $endBlockInfo['cdOffset']);

        $files = array();

        foreach (range(0, $endBlockInfo['fileCount'] - 1) as $index) {

            $cdBlock = fread($this->fileHandle, self::HEADER_SIZE_CDHEADER);

            $cdBlockInfo = $this->unpackCdBlock($cdBlock);

            $filename = fread($this->fileHandle, $cdBlockInfo['filenameLength']);
            $extraField = fread($this->fileHandle, $cdBlockInfo['extraFieldLength']);

            if ($cdBlockInfo['commentLength'] > 0) {
                $comment = fread($this->fileHandle, $cdBlockInfo['commentLength']);
            } else {
                $comment = '';
            }

            $cdBlockInfo = array_merge($cdBlockInfo, compact('filename', 'comment'));

            $files[] = $cdBlockInfo;
        }

        return $files;
    }

    public function getFileInfoAtOffset($offset)
    {
        fseek($this->fileHandle, $offset);

        $fileBlock = fread($this->fileHandle, self::HEADER_SIZE_FILEHEADER);

        $fileInfo = $this->unpackFileBlock($fileBlock);

        $fileInfo['offset'] = $offset;
        $fileInfo['filename'] = fread($this->fileHandle, $fileInfo['filenameLength']);
        $fileInfo['extraField'] = fread($this->fileHandle, $fileInfo['extraFieldLength']);

        return $fileInfo;
    }

    public function getRawDataFromFileInfo($fileInfo)
    {
        $totalOffset = self::HEADER_SIZE_FILEHEADER + $fileInfo['offset'] + $fileInfo['filenameLength'] + $fileInfo['extraFieldLength'];
        fseek($this->fileHandle, $totalOffset);
        return fread($this->fileHandle, $fileInfo['csize']);
    }

    public function createGzipDataFromFileInfo($fileInfo)
    {
        $gzipData = '';
        $gzipData .= pack('nCCCCV', 0x1f8b,8,0,0,0,0); //gzip header<commit_msg>Might possibly get zero-length fields.<commit_after><?php

namespace Coverslide;

class ZipReader
{
    const COMPRESSION_UNCOMPRESSED  =   0;
    const COMPRESSION_DEFLATE       =   8;

    const HEADER_SIZE_FILEHEADER    =   30;
    const HEADER_SIZE_CDHEADER      =   46;
    const HEADER_SIZE_ENDHEADER     =   22;

    private $fileHandle;

    public function __construct($path)
    {
        $this->fileHandle = fopen($path, 'rb');
    }

    public function __destruct()
    {
        fclose($this->fileHandle);
    }

    public function listFiles()
    {
        fseek($this->fileHandle, 0 - self::HEADER_SIZE_ENDHEADER, SEEK_END);

        $endBlock = fread($this->fileHandle, self::HEADER_SIZE_ENDHEADER);

        $endBlockInfo = $this->unpackEndBlock($endBlock);

        fseek($this->fileHandle, $endBlockInfo['cdOffset']);

        $files = array();

        foreach (range(0, $endBlockInfo['fileCount'] - 1) as $index) {

            $cdBlock = fread($this->fileHandle, self::HEADER_SIZE_CDHEADER);

            $cdBlockInfo = $this->unpackCdBlock($cdBlock);

            $filename = $cdBlockInfo['filenameLength'] ? fread($this->fileHandle, $cdBlockInfo['filenameLength']) : '';
            $extraField = $cdBlockInfo['extraFieldLength'] ? fread($this->fileHandle, $cdBlockInfo['extraFieldLength']) : '';

            $comment = $cdBlockInfo['commentLength'] ? fread($this->fileHandle, $cdBlockInfo['commentLength']) : '';

            $cdBlockInfo = array_merge($cdBlockInfo, compact('filename', 'comment'));

            $files[] = $cdBlockInfo;
        }

        return $files;
    }

    public function getFileInfoAtOffset($offset)
    {
        fseek($this->fileHandle, $offset);

        $fileBlock = fread($this->fileHandle, self::HEADER_SIZE_FILEHEADER);

        $fileInfo = $this->unpackFileBlock($fileBlock);

        $fileInfo['offset'] = $offset;
        $fileInfo['filename'] = $fileInfo['filenameLength'] ? fread($this->fileHandle, $fileInfo['filenameLength']) : '';
        $fileInfo['extraField'] = $fileInfo['extraFieldLength'] ? fread($this->fileHandle, $fileInfo['extraFieldLength']) : '';

        return $fileInfo;
    }

    public function getRawDataFromFileInfo($fileInfo)
    {
        $totalOffset = self::HEADER_SIZE_FILEHEADER + $fileInfo['offset'] + $fileInfo['filenameLength'] + $fileInfo['extraFieldLength'];
        fseek($this->fileHandle, $totalOffset);
        return fread($this->fileHandle, $fileInfo['csize']);
    }

    public function createGzipDataFromFileInfo($fileInfo)
    {
        $gzipData = '';
        $gzipData .= pack('nCCCCV', 0x1f8b,8,0,0,0,0); //gzip header
{'fraction_non_alphanumeric': 0.14641684373189107, 'fraction_numerical': 0.007533320455862469, 'mean_word_length': 3.0548159749412687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '95757', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1549, 'n_words': 249}