本文介绍了试图利用JSZip打开然后解析.zip中的特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用在.zip中循环浏览文件,寻找一个(这里, test.txt )我想要解析的内容。

Have been trying to use the JSZip library to cycle through files in a .zip, looking for one (here, test.txt) that I want to parse for content.

的 [推荐查看源代码] JSZip提供:

Have attempted to do a modification of the sample [recommend viewing source on that] that JSZip provides:

<!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文件。任何想法我可能在这里做错了?

For some reason, I'm sure having to do with the way that I am using the closure, it is not actually parsing the .zip files in question. Any ideas what I might be doing wrong here?

推荐答案

我使用这个代码,并能够得到所有的文件数据。内容变量具有文件内容:

I use this code and am able to get all file data. content variable has file 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);
            }
    }

这篇关于试图利用JSZip打开然后解析.zip中的特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:30