2013-03-12 58 views
1

已嘗試使用JSZip library循環瀏覽.zip中的文件,尋找我想要解析內容的文件(這裏是test.txt) 。試圖利用JSZip打開,然後解析.zip中的特定文件

曾試圖做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文件。任何想法我可能在這裏做錯了嗎?

回答

1

我使用此代碼,並能夠獲取所有文件數據。內容變量有文件內容:

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); 
      } 
    } 
相關問題