简体   繁体   English

使用zip.js通过Node.js上的xmlhttp / ajax调用读取zip文件

[英]using zip.js to read a zip file via xmlhttp/ajax call on Node.js

I am trying to : 我在尝试着 :

  1. Send a zip file via xmlhttp to the client 通过xmlhttp发送一个zip文件到客户端
  2. then read the file using zip.js and render its contents 然后使用zip.js读取文件并呈现其内容

I successfully receive the binary of the file ie the success callback is called but I get and error when I try to do getEntries . 我成功接收了文件的二进制文件,即调用了成功回调,但是在尝试执行getEntries时却得到了错误。 I think the error is with the way of sending stream , please help. 我认为错误与发送流的方式有关,请帮助。

Error msg : 错误消息

Error in reading zip file 读取zip文件时出错

My client side code (using angular) : 我的客户端代码(使用angular):

$http.get(window.location.origin + '/book/'+bookName,{responseType:"Blob"}).
success(function (data , error) {
    var a = new Uint8Array(data);
        //var dataView = new DataView(data);
        //var blob = new Blob(dataView.buffer);
    zip.useWebWorkers = true;
    zip.workerScriptsPath = '/js/app/';

    zip.createReader(new zip.BlobReader(data), function(reader) {

      // get all entries from the zip
      reader.getEntries(function(entries) {   //HERE I GET THE ERROR
        if (entries.length) {

          // get first entry content as text
          entries[0].getData(new zip.TextWriter(), function(text) {
            // text contains the entry data as a String
            console.log(text);

            // close the zip reader
            reader.close(function() {
              // onclose callback
              var a = 0;
            });

          }, function(current, total) {
            // onprogress callback
            var a = 0;
          });
        }
      });
    },
     function(error) {
      // onerror callback
        var a = 0;
    });


})
.error( function (data , error) {
    var a = 0;

});

My Server side code on Node: 我在节点上的服务器端代码:

router.get('/book/:bookName',function (req , res ) {


console.log('Inside book reading block : ' + req.params.bookName);
req.params.bookName += '.zip';

var filePath = path.join(__dirname,'/../\\public\\books\\' ,req.params.bookName );
var stat = fileSystem.statSync(filePath);

res.writeHead(200, {
    //'Content-Type': 'application/zip',
    'Content-Type': 'blob',
    'Content-Length': stat.size
});

var readStream = fileSystem.createReadStream(filePath);
// replace all the event handlers with a simple call to readStream.pipe()
readStream.pipe(res);   
});

It is probable that you might have already found a solution. 您可能已经找到了解决方案。 I faced the same problem today and this is how I solved it in plain javascript: 我今天遇到了同样的问题,这就是我用普通的javascript解决的方法:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'assets/object/sample.zip', true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
    // response is unsigned 8 bit integer
    var responseArray = new Uint8Array(this.response);
    var blobData = new Blob([responseArray], {
        type: 'application/zip'
    });
    zip.createReader(new zip.BlobReader(blobData), function(zipReader) {
        zipReader.getEntries(displayEntries);
    }, onerror);
};

xhr.send();

The problem I see in your code is that you are changing the value to Uint8Array and assigning it to var a , but still use the raw data in blobreader. 我在您的代码中看到的问题是,您正在将值更改为Uint8Array并将其分配给var a ,但仍在blobreader中使用原始数据。 Also the blob reader required blob and not an array. 另外,blob阅读器需要blob而不是数组。 So you should have converted var a into blob and then used it for reading. 因此,您应该将var a转换为blob,然后将其用于读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM