admin管理员组

文章数量:1405120

I am trying to unzip a text file using Zlib library in node.js, but i get unexpected end of file error, when piping the files’s Readstream content towards a Gunzip object, here is my snippet:

const fs = require('fs');
const zlib = require("zlib");

var readable = fs.createReadStream(__dirname + '/greet.txt');
var readableGz = fs.createReadStream(__dirname + '/greet.txt.gz');
var writableGz = fs.createWriteStream(__dirname + '/greet.txt.gz');
var gZip = zlib.createGzip();
var gUnZip = zlib.createGunzip();

readable.pipe(gZip).pipe(writableGz);   // press file
readableGz.pipe(gUnZip).on("error", function(e){    // unpress file
    console.log("error, " + e);
});

The greet.txt has some random text in it, and all the files used are already created in the directory, however an error event is triggered when the last line reaches

I am trying to unzip a text file using Zlib library in node.js, but i get unexpected end of file error, when piping the files’s Readstream content towards a Gunzip object, here is my snippet:

const fs = require('fs');
const zlib = require("zlib");

var readable = fs.createReadStream(__dirname + '/greet.txt');
var readableGz = fs.createReadStream(__dirname + '/greet.txt.gz');
var writableGz = fs.createWriteStream(__dirname + '/greet.txt.gz');
var gZip = zlib.createGzip();
var gUnZip = zlib.createGunzip();

readable.pipe(gZip).pipe(writableGz);   // press file
readableGz.pipe(gUnZip).on("error", function(e){    // unpress file
    console.log("error, " + e);
});

The greet.txt has some random text in it, and all the files used are already created in the directory, however an error event is triggered when the last line reaches

Share Improve this question edited Jan 8, 2017 at 4:16 v-andrew 24.3k6 gold badges38 silver badges42 bronze badges asked Jan 8, 2017 at 1:05 Pablo MarinoPablo Marino 4911 gold badge5 silver badges12 bronze badges 2
  • You are writing and reading in parallel. Wait until it's finished writing before reading. – cartant Commented Jan 8, 2017 at 1:13
  • if i first press the file, and then ment that part, and just run the part when i try to unpress it, the same error is still happening – Pablo Marino Commented Jan 8, 2017 at 1:23
Add a ment  | 

1 Answer 1

Reset to default 6

All node operations are asynchronous, so you have to listen for finish event.

readable.pipe(gZip).pipe(writableGz)
   .on('finish', function () {  // finished
        console.log('Done. Now you can start reading.'); 
});

Here is a working code:

const fs = require('fs');
const zlib = require("zlib");

var readable = fs.createReadStream('./greet.txt');
var writableGz = fs.createWriteStream('./greet.txt.gz');
var gZip = zlib.createGzip();
var gUnZip = zlib.createGunzip();

readable
    .pipe(gZip)
    .pipe(writableGz)
    .on('finish', function () {  // finished
        console.log('Done. Now you can start reading.');
        var readableGz = fs.createReadStream('./greet.txt.gz');
        readableGz
            .pipe(gUnZip)  // extract file
            .on("error", function (e) {
                console.log("error, " + e); 
            });
});

本文标签: javascriptunexpected end of file error when unzipping file nodejsStack Overflow