gpt4 book ai didi

javascript - 尝试利用 JSZip 打开并解析 .zip 中的特定文件

转载 作者:行者123 更新时间:2023-11-28 02:23:03 24 4
gpt4 key购买 nike

一直在尝试使用JSZip library循环浏览 .zip 中的文件,查找我想要解析内容的文件(此处为 test.txt)。

已尝试对 sample 进行修改[建议查看 JSZip 提供的源代码]:

<!DOCTYPE HTML>

<html>

<head>

<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">

</head>
<body>
<div class = "container">
<div class = "hero-unit">
<input type="file" class="span7" id="input" name="file" multiple /> <!-- redo this in a bootstrappy way-->
<br>
<output id="output"></output>
</div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="/js/jszip-load.js"></script>
<script src="/js/jszip.js"></script>
<script src="/js/jszip-inflate.js"></script>

<script>

if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}


function handleFileSelect(evt) {
var files = evt.target.files; // FileList object


// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {

if (f.type !== "application/zip") {
document.getElementById('output').innerHTML = "<p class='text-error'>" + f.name + " isn't a zip file.</div>";
continue;
}

var reader = new FileReader();

reader.onload = (function(theFile) {
return function(e) {

var zip = new JSZip(e.target.result)


$.each(zip.files, function (index, zipEntry) {
if (zipEntry.name == "test.txt"){

var text = zipEntry.asText();
var lines = text.split(/[\r\n]+/g); // tolerate both Windows and Unix linebreaks

for(var i = 0; i < lines.length; i++) {
if (lines[i].length > 240){
output.push('<li>' + lines[i] + '<br>');
}
}

document.getElementById('output').innerHTML = '<h2>Paths with more than 240 characters:</h2> <br><ol>' + output.join('') + '</ol>';

else{
alert("file not found!")
}

}

});


}
})(f);


}
}

document.getElementById('input').addEventListener('change', handleFileSelect, false);


</script>
</body>
</html>

出于某种原因,我确信与我使用闭包的方式有关,它实际上并没有解析有问题的 .zip 文件。你知道我在这里可能做错了什么吗?

最佳答案

我使用此代码并且能够获取所有文件数据。内容变量具有文件内容:

function loadSettingsFile(evt) {
var files = evt.target.files;
for (var i = 0, f; f = files[i]; i++) {

var reader = new FileReader();

// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
try {
var zip = new JSZip(e.target.result);
$.each(zip.files, function (index, zipEntry) {
var content = zipEntry.asText();
alert(content);
});
} catch(e) {
alert(e)
}
}
})(f);

// read the file !
// readAsArrayBuffer and readAsBinaryString both produce valid content for JSZip.
reader.readAsArrayBuffer(f);
// reader.readAsBinaryString(f);
}
}

关于javascript - 尝试利用 JSZip 打开并解析 .zip 中的特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com