2017-03-24 6 views
0

메모리에 zip을 생성 할 노드 모듈이 있습니까? (이 zip 파일을 디스크에 저장하지 않으므로)이 생성 된 zip 파일을 다른 서버로 보낼 수 있습니다 (From 기억). 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? Gzip으로 할 경우노드 모듈을 사용하여 메모리에있는 Crete ZIP 파일

var file_system = require('fs'); 
var archiver = require('archiver'); 
var dirToCompress = 'directoryToZIP'; 

module.exports = function (req, res, next) { 
var archive = archiver.create('zip', {}); 
archive.on('error', function (err) { 
    throw err; 
}); 

    var output = file_system.createWriteStream('/testDir/myZip.zip',{flags:'a'});//I don't want this line 
    output.on('close', function() { 
     console.log(archive.pointer() + ' total bytes'); 
     console.log('archiver has been finalized and the output file descriptor has closed.'); 
    }); 

    archive.pipe(output); 

    archive.directory(dirToCompress); 

    archive.finalize(); 

}; 
+0

[archiver] (https://www.npmjs.com/package/archiver)를 사용하고 스트림을 응답으로 파이프하는 것이 좋습니다. 이렇게하면 디스크에 아무 것도 쓸 필요가 없습니다. –

+0

출력 변수는 다른 서버의 api가되어이 zip 파일을 보낼 수 있습니다. – Torreto

답변

0

, 당신은 the built-in zlib module을 사용하고 심지어 외부 모듈을로드 할 수 없습니다 : 여기 내 예입니다.

const gzip = zlib.createGzip(); 

// If you have an inputStream and an outputStream: 

inputStream.pipe(gzip).pipe(outputStream); 

Gzip으로 우편의 경우와하지, 당신은 archiver 또는 zip-stream을 확인할 수 있습니다.