quake 3 | find only bsps in map paks | unpack pk3s | Search

This code snippet uses the node-stream-zip library to list all .bsp files found within a specified ZIP archive. It's designed to be reusable as a module within a larger application.

Run example

npm run import -- "list bsps in a pak"

list bsps in a pak

var StreamZip = require('node-stream-zip');
var fs = require('fs');
var path = require('path');

function listBsps(zipFile) {
    const zip = new StreamZip({
        file: zipFile,
        storeEntries: true
    });

    zip.on('ready', () => {
        console.log('Entries read: ' + zip.entriesCount + ' ' + path.basename(zipFile));
        for (const entry of Object.values(zip.entries())) {
            if(entry.name.includes('.bsp')) {
                console.log(entry.name)
            }
        }
    });
    
    zip.on('error', err => {  });
}

module.exports = listBsps;

What the code could have been:

const StreamZip = require('node-stream-zip');
const fs = require('fs');
const path = require('path');

/**
 * Lists all.bsp files in a given zip file.
 *
 * @param {string} zipFile The path to the zip file.
 */
function listBsps(zipFile) {
    try {
        const zip = new StreamZip({
            file: zipFile,
            storeEntries: true
        });

        // Wait for the zip file to be fully read
        return new Promise((resolve, reject) => {
            zip.on('ready', () => {
                const bspFiles = [];
                for (const entry of Object.values(zip.entries())) {
                    if (entry.name.includes('.bsp')) {
                        bspFiles.push(entry.name);
                    }
                }
                resolve(bspFiles);
            });

            zip.on('error', (err) => {
                reject(err);
            });
        });
    } catch (err) {
        console.error(`Error reading zip file: ${err.message}`);
        return [];
    }
}

module.exports = listBsps;

This code snippet is designed to list all .bsp files (common Quake map files) within a given ZIP archive.

Here's a breakdown:

  1. Dependencies: It imports the node-stream-zip library for handling ZIP archives, fs for file system operations, and path for path manipulation.

  2. listBsps Function:

  3. Export: The listBsps function is exported as a module, allowing it to be used in other parts of a larger application.

In essence, this code provides a simple way to inventory .bsp files within a ZIP archive, which is useful for tasks like game mod management or asset analysis.