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
1 Answer
Reset to default 6All 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
版权声明:本文标题:javascript - unexpected end of file error when unzipping file node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744276198a2598423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论