使用adm zip解压缩上传的文件

时间:2017-08-13 15:17:35

标签: javascript node.js filereader adm-zip

我正在尝试使用adm-zip获取上传文件中的文件列表。由于我们无法获取上传文件的路径,因此我尝试将zip转换为Buffer并将其传递给adm-zip。但是zip.getEntries();它没有给我文件列表。

 checkZipFiles(){
      var AdmZip = require("adm-zip");
      var filesInput =  document.querySelector('input[type="file"]').files[0];
      var res;
      var zip = new AdmZip(filesInput);
      console.log(' zip is '+JSON.stringify(zip));
      var zipEntries = zip.getEntries();
      console.log(' zipEntries is '+zipEntries);
      zipEntries.forEach(function(zipEntry) {
          console.log(zipEntry.toString()); // outputs zip entries information
          if (zipEntry.entryName == "my_file.txt") {
               console.log(zipEntry.data.toString('utf8')); 
          }
      });
      var reader = new FileReader();
      reader.readAsArrayBuffer(filesInput);
      var bytes,buf ;
      reader.onloadend = function(e){
          var readLen=e.target.result.byteLength;
          var arrayBuffer = reader.result
           bytes = new Uint8Array(arrayBuffer);
          console.log('insidebytes is '+bytes);
           buf = new Buffer(e.target.result.byteLength);
           for (var i = 0; i < buf.length; ++i) {
            buf[i] = bytes[i];
        }
          console.log(e.target.result.byteLength+' length is '+bytes.byteLength);
          var myZip = e.target.result;   
          console.log('bytes is '+bytes);
          console.log('buf is '+buf);

            zip = new AdmZip(buf);
//            console.log(zip+' reader ');
            var zipEntries = zip.getEntries();
            console.log(' zip is '+zipEntries);  

      }

  }

当我打印缓冲区时,它打印的内容如下所示,文件名在zip中。

121832长度是121832

} + YL]%: _����ld� ��c{\��h7�� Le33 \“ԅտ׉V3- ^'Ҁ霗^ pqWvްRaknyEgrG%1 /疫情周报 \d A upR L up jemF k9y ^Ն;h 1:ca delete.txt --F1p [

但AdmZip给出了以下错误。缓冲区或AdmZip的问题是否无法读取缓冲区?

Uncaught Invalid END header (bad signature) mainHeader.js?4281:35 

0 个答案:

没有答案