试图利用JSZip打开并解析.zip格式的特定文件

trying to leverage JSZip to open and then parse a specific file in a .zip

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

一直在尝试使用JSZip库来循环浏览.zip中的文件,寻找我想解析内容的文件(这里是test.txt)。

尝试修改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);
            }
    }