带有node.js / io.js的Filetransfer(压缩)

时间:2015-06-23 10:31:22

标签: node.js compression file-transfer tcpsocket

通过tcp套接字在两台服务器之间传输二进制压缩文件(大约100MB,tar.gz)的正确方法是什么。一般来说,我的传输工作正常,但目标上传输的文件几乎是原来的两倍,不再可用。

client.js:

var net = require('net');
var fs = require('fs');

//read file
var myFile = fs.readFileSync('e:/test/console/deploy/test.tar.gz');

upload = function(ip, port, callback){

  var client = new net.Socket();

    // connect
  client.connect(port, ip, function () {

    console.log('Connecting  ....');

       // Send file
       client.end(myFile);

  });

} // end upload


// Uploading file
upload('127.0.0.1', '9001', function(reponse){
    console.log(reponse)
});

server.js:

var net = require('net');
var fs = require('fs');

var uplsrv = net.createServer(function (socket) {

    console.log('Upload Server started ....');

    var payload = Buffer(0); 
    socket.on('data', function(chunk){
            console.log('Reiceiving data ....');
            payload += chunk;
        });

    socket.on('end', function(){

        var path = 'e:/test/software_repo/test.tar.gz'

        fs.open(path, 'w', function(err, fd) {
            if (err) {
                throw 'error opening file: ' + err;
            }

            fs.writeSync(fd, payload, 0, payload.length, 'base64', function(err) {
                if (err) throw 'error writing file: ' + err;
                fs.close(fd, function() {
                    console.log('Upload finished .... and file written');
                })
            });
        });

    });

    socket.on('close', function(){

            console.log('Connection to Upload Server was closed ...');
        });

}).listen(9001);

有什么建议吗?样品欢迎。

干杯

TMOE

0 个答案:

没有答案