<commit_before>var ZipEntry = require("./zipEntry"),
    Headers = require("./headers");
    Utils = require("./util");

module.exports = function(/*Buffer*/buf) {
    var entryList = [],
        entryTable = {},
        _comment = '',
        endHeader = new Headers.MainHeader();

    if (buf) {
        readMainHeader();
    }

    function readEntries() {
        entryTable = {};
        entryList = new Array(endHeader.diskEntries);  // total number of entries
        var index = endHeader.offset;  // offset of first CEN header
        for(var i = 0; i < entryList.length; i++) {

            var tmp = index,
                entry = new ZipEntry();

            entry.header = buf.slice(tmp, tmp += Utils.Constants.CENHDR);
            entry.entryName = buf.toString('utf8', tmp, tmp += entry.header.fileNameLength);

            if (entry.header.extraLength)
                entry.extra = buf.slice(tmp, tmp += entry.header.extraLength);

            if (entry.header.commentLength)
                entry.comment = buf.toString('utf8', tmp, tmp + entry.header.commentLength);

            index += entry.header.entryHeaderSize;

            if (!entry.isDirectory) {
                // read data
                entry.setCompressedData(buf.slice(entry.header.offset, entry.header.offset + Utils.Constants.LOCHDR + entry.header.compressedSize + entry.entryName.length));
            }

            entryList[i] = entry;
            entryTable[entry.entryName] = entry;
        }
    }

    function readMainHeader() {
        var i = buf.length - Utils.Constants.ENDHDR, // END header size
            n = Math.max(0, i - 0xFFFF), // 0xFFFF is the max zip file comment length
            endOffset = 0; // Start offset of the END header

        for (i; i >= n; i--) {
            if (buf[i] != 0x50) continue; // quick check that the byte is 'P'
            if (buf.readUInt32LE(i) == Utils.Constants.ENDSIG) { // "PK\005\006"
                endOffset = i;
                break;
            }
        }
        if (!endOffset)
            throw Utils.Errors.INVALID_FORMAT;

        endHeader.loadFromBinary(buf.slice(endOffset, endOffset + Utils.Constants.ENDHDR));
        if (endHeader.commentLength) {
            _comment = buf.toString('utf8', endOffset + Utils.Constants.ENDHDR);
        }
        readEntries();
    }

    return {
        /**<commit_msg>Fixed issue #15, #12, #13<commit_after>var ZipEntry = require("./zipEntry"),
    Headers = require("./headers");
    Utils = require("./util");

module.exports = function(/*Buffer*/buf) {
    var entryList = [],
        entryTable = {},
        _comment = '',
        endHeader = new Headers.MainHeader();

    if (buf) {
        readMainHeader();
    }

    function readEntries() {
        entryTable = {};
        entryList = new Array(endHeader.diskEntries);  // total number of entries
        var index = endHeader.offset;  // offset of first CEN header
        for(var i = 0; i < entryList.length; i++) {

            var tmp = index,
                entry = new ZipEntry();

            entry.header = buf.slice(tmp, tmp += Utils.Constants.CENHDR);
            entry.entryName = buf.toString('utf8', tmp, tmp += entry.header.fileNameLength);

            if (entry.header.extraLength)
                entry.extra = buf.slice(tmp, tmp += entry.header.extraLength);

            if (entry.header.commentLength)
                entry.comment = buf.toString('utf8', tmp, tmp + entry.header.commentLength);

            index += entry.header.entryHeaderSize;

            if (!entry.isDirectory) {
                // read data
                //entry.setCompressedData(buf.slice(entry.header.offset, entry.header.offset + Utils.Constants.LOCHDR + entry.header.compressedSize + entry.entryName.length));
                entry.setCompressedData(buf.slice(entry.header.offset, entry.header.offset + Utils.Constants.LOCHDR + entry.header.compressedSize + entry.entryName.length + buf.readUInt16LE(entry.header.offset + Utils.Constants.LOCEXT)));
            }

            entryList[i] = entry;
            entryTable[entry.entryName] = entry;
        }
    }

    function readMainHeader() {
        var i = buf.length - Utils.Constants.ENDHDR, // END header size
            n = Math.max(0, i - 0xFFFF), // 0xFFFF is the max zip file comment length
            endOffset = 0; // Start offset of the END header

        for (i; i >= n; i--) {
            if (buf[i] != 0x50) continue; // quick check that the byte is 'P'
            if (buf.readUInt32LE(i) == Utils.Constants.ENDSIG) { // "PK\005\006"
                endOffset = i;
                break;
            }
        }
        if (!endOffset)
            throw Utils.Errors.INVALID_FORMAT;

        endHeader.loadFromBinary(buf.slice(endOffset, endOffset + Utils.Constants.ENDHDR));
        if (endHeader.commentLength) {
            _comment = buf.toString('utf8', endOffset + Utils.Constants.ENDHDR);
        }
        readEntries();
    }

    return {
        /**
{'fraction_non_alphanumeric': 0.12852288626823904, 'fraction_numerical': 0.009194483310013992, 'mean_word_length': 1.9263157894736842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2671354', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1412, 'n_words': 317}