JSZip:从文件输入中获取zip格式文件的内容

JSZip: Get content of file in zip from file input

本文关键字:文件 格式 zip 输入 JSZip 获取      更新时间:2023-09-26

我想用JSZip从输入中获取zip文件的内容。我可以读取文件的标题但如何获取内容

我尝试使用jQuery:

$('.upload-input').on('change', function($event) {
 var $file = $event.target.files[0];
 JSZip.loadAsync($file).then(function($content) {
  alert($content.files["css/style.css"].async('text'));
 })
});

return: [object Promise]

如何获取纯文本

JSFiddle: https://jsfiddle.net/jyuy7q6j/

谢谢!

async,和loadAsync一样返回一个Promise。在这里你可以链接它们:

$('.upload-input').on('change', function($event) {
 var $file = $event.target.files[0];
 JSZip.loadAsync($file).then(function($content) {
  // if you return a promise in a "then", you will chain the two promises
  return $content.files["css/style.css"].async('text');
 }).then(function (txt) {
   alert(txt);
 });
});

我忘记了files[...].async,所以这是我的解决方案(通过一点点遍历文件):

var worldChars = Array.from(zip.files["world.txt"]._data.compressedContent)
var world = ""
for (var ch of worldChars) {
    world += String.fromCharCode(ch)
}