node-zip-stream icon indicating copy to clipboard operation
node-zip-stream copied to clipboard

zip-stream exits abruptly without any error if content is large

Open anandncode opened this issue 4 years ago • 5 comments

Description

When trying to add a string of considerable size such as >7 MB, the archive.entry exits abruptly without any exception and exit code - 0.

Below is sample code

const ZipStream = require('zip-stream');
const archive = new ZipStream(); 

function arrayToCsv(dataArray) {
    const output = dataArray.map((val) => {
        if (val === null || val === undefined) {
            return '';
        }
        const modVal = `${val}`; // Convert everything to string
        return `"${modVal.replace(/"/g, '""')}"`;
    }).join(separator);
    return output;
}

function getCSV(rows, cols = 10) {

    let csv = '';
    for (let i = 0; i < rows; i++) { // files

        const row = new Array(cols);
        row.fill(`randomString${i + 1}`);
        csv = csv + arrayToCsv(row) + '\n';
    }
    return csv;
}

process.on('exit', (code) => {
    console.log(`Process exit with code - ${code}`)
});

console.log(`Before adding csv`);
csv = getCSV(20000);

archive.entry(csv, { name: 'csv1.csv', zlib: { level: 9 } }, (err, entry) => {
    if (err) {
        throw err
    }
    console.log(`After adding csv`);
    archive.finish();
})

Output

Before adding csv
Process exit with code 0

The After adding csv is not printed to console.

I have used object-sizeof to calculate size of csv size of csv - 7777880 OR 7.42 MB

For lower sizes, it works fine.

Please let me know if there is anything wrong with the code above.

anandncode avatar Jan 13 '22 05:01 anandncode

Btw the same thing works fine with archiver. Also calling zlib.deflate on csv string also works fine without any issue

anandncode avatar Jan 13 '22 05:01 anandncode

tested your example, seems like it is due to the stream buffer overflow if you don't read the zip stream then it is silently killed

littlemaneuver avatar Mar 01 '22 12:03 littlemaneuver

Thanks @antoniopuero Shouldn't it throw an error and call the callback? Also, 7.4 MB shouldn't be a big size as most of the zip scenarios deal with large file sizes.

anandncode avatar Mar 02 '22 11:03 anandncode

@anandncode were you ever able to resolve this? Seeing the same issue with a much smaller total file size (< 1mb) but a large number of files

benjaminben avatar Jul 20 '23 00:07 benjaminben

@benjaminben I could vaguely remember that I changed the library to fflate and it worked better

anandncode avatar Jul 20 '23 13:07 anandncode