0

我正在尝试使用 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

}+����Y���L�]��%:�<em>���<code>_����ld���c{\��h7���L��e33� \"ԅտ׉��v�˕3�-��^�'�Ҁ霗�^�p�q�W��������v �G�%1/���Wer����\d���A�upR�L����up�jemF������k9y�</em>�^Ն;h�1 :ca delete.txt �-�-F1p[

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

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

0 回答 0