读取文件后,它在nodejs中显示为空白

时间:2019-02-20 18:42:18

标签: node.js fs

我正在一个项目中,该项目需要解压缩文件并处理该数据。

我已经成功解压缩了文件,但是在解压缩后,我试图读取解压缩的文件,但输出只是空白。

const fs = require("fs");
const zlib = require("zlib");
const logFolder = '../logsample/';
var  decompress = zlib.createGunzip(); // decompress
    //readstream = fs.createReadStream(filename);

function decompressfile(filename){                        // checking if file is in .gz formate or not (decompressfile)
    var newfilename = filename.replace(".gz",".json"),
        writestream = fs.createWriteStream(newfilename);
    fs.createReadStream(filename).pipe(decompress).pipe(writestream);
}

fs.readdirSync(logFolder).forEach(file => {             // for each file in logsample folder
  if(/.gz$/i.test(file)==true){
      file = logFolder + file;
      decompressfile(file);
      //console.log(file);
  }
});

var data = fs.readFileSync('../logsample/log1.log.sample.json', 'utf');
console.log(data);

`

你能帮我吗?

0 个答案:

没有答案