2016-09-26 5 views
3

zip 파일이 있습니다 (실제로는 epub 파일 임). 파일을 반복하여 디스크에 압축을 풀지 않고 읽을 필요가 있습니다. 나는 Node.js를 라이브러리를 사용하려고Node.js 파일을 압축 해제하지 않고 zip 파일을 읽습니다.

JSZip라고하지만, 각 파일의 내용을 버퍼 메모리에 저장되고 내가 문자열 버퍼의 내용을 해독하려고 할 때마다 반환 된 내용

여기 코드의 읽을 나는 시도 :

const zip = new JSZip(); 
    // read a zip file 
    fs.readFile(epubFile, function (err, data) { 
     if (err) throw err; 
     zip.loadAsync(data).then(function (zip) { 
      async.eachOf(zip.files, function (content, fileName, callback) { 
       if (fileName.match(/json/)) { 
        var buf = content._data.compressedContent; 
        console.log(fileName); 
        console.log((new Buffer(buf)).toString('utf-8')); 
       } 
       callback(); 
      }, function (err) { 
       if (err) { 
        console.log(err); 
       } 
      }); 
     }); 
    }); 

답변

5
npm install unzip 

https://www.npmjs.com/package/unzip

fs.createReadStream('path/to/archive.zip') 
    .pipe(unzip.Parse()) 
    .on('entry', function (entry) { 
    var fileName = entry.path; 
    var type = entry.type; // 'Directory' or 'File' 
    var size = entry.size; 
    if (fileName === "this IS the file I'm looking for") { 
     entry.pipe(fs.createWriteStream('output/path')); 
    } else { 
     entry.autodrain(); 
    } 
    }); 
+0

어떻게 항목을 읽기 스트림으로 사용 하시겠습니까? 나는 그것을 s3에 파이프하려고하고있다. –

관련 문제