1

一直在尝试使用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 文件。有什么想法我可能在这里做错了吗?

4

1 回答 1

1

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

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);
            }
    }
于 2014-07-22T17:47:56.037 回答