javascript - unexpected end of file error when unzipping file node.js - Stack Overflow

I am trying to unzip a text file using Zlib library in node.js, but i get unexpected end of file error,

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); 
            });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744276199a4566341.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信