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

5
我希望您能够用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'));
 })
});

返回: [object Promise]

我该怎么做才能得到纯文本?

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

谢谢!

2个回答

15

asyncloadAsync 一样,返回一个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);
 });
});

哦,非常容易 :) 谢谢 - howtoweb

-1

我忘记了 files[...].async,所以这是我的解决方案(需要对文件进行一些遍历):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接