zip.js来读取zip中的文件名。 (仅限客户端脚本)

时间:2016-04-08 13:01:36

标签: javascript jquery zip.js

现在有多个文件输入我必须检查每个文件输入,zip文件不包含任何无效文件(doc,docx和pdf只允许)。

我写了

 <script   src="https://code.jquery.com/jquery-2.2.1.js"   integrity="sha256-eNcUzO3jsv0XlJLveFEkbB8bA7/CroNpNVk3XpmnwHc=" crossorigin="anonymous"></script>    
<script type="text/javascript" src="/js/zip.js"/>
<script type="text/javascript" src="/js/inflate.js"/>
<script type="text/javascript" src="/js/deflate.js"/>
<script type="text/javascript" src="/js/z-worker.js"/>
<script type="text/javascript">
    $(document).ready(function () {
        if((window.location.href.indexOf("LibraryItemUpload`1&ParentId=7d428470-2234-41c0-85f4-a512d51198c6") > -1) || (window.location.href.indexOf("LibraryItemUpload%601&ParentId=7d428470-2234-41c0-85f4-a512d51198c6") > -1)) 
        {
            $("input:file").change(function () {
               var regex=new RegExp("^[A-Za-z0-9 ]+$");
               var file=this.files[0];
                var key = this.value;
                var ze = key.split('\\').pop();
                var filename = ze.split('.')[0];
                var extension=key.split('.').pop().trim().toLowerCase();
                if(extension == 'zip')
                {               
                    zip.createReader(new zip.BlobReader(file), function(reader) {

                      // get all entries from the zip
                      reader.getEntries(function(entries) {
                        if (entries.length) {

                          // get first entry content as text
                          entries[0].getData(new zip.TextWriter(), function(text) {
                            // text contains the entry data as a String
                            console.log(text);

                            // close the zip reader
                            reader.close(function() {
                              // onclose callback
                            });

                          }, function(current, total) {
                            // onprogress callback
                          });
                        }
                      });
                    }, function(error) {

                    });
                }
               if (!regex.test(filename))  {                
                    alert('Please do not use special characters in file name please rename file name and upload it again.');
                    location.reload();
                }
                else {
                    return true;
                }
            });
        }
    });
  </script> 

并编写了从zip.js读取文件名的代码,但是流程不在zip.createReader函数中。

请建议我,如果另一个javascript可用于读取zip文件,我只想让条目对象读取文件名。

1 个答案:

答案 0 :(得分:1)

function readEntries(entries) {
    var entryLength = entries.length;
    for (i = 0; i < entryLength; i++) {
        var entry = entries[i];
        var fileName = entry.filename.substring(entry.filename.lastIndexOf("/") + 1); //if inside folder
        var ext = fileName.split(".").pop().toLowerCase();
        if (ext.toUpperCase() == 'DOC' || ext.toUpperCase() == 'PDF' ||
            ext.toUpperCase() == 'DOCX') {
            //logic
        }
    }
}

zip.useWebWorkers = false;//explicitly include (required) zip-workers ['zip.js','zip-fs.js','z-worker.js','inflate.js','deflate.js']

var fileInput = document.getElementById("zipfile");//Input File
fileInput.addEventListener('change', function(event) {
    zip.createReader(new zip.BlobReader(fileInput.files[0]), function(zipReader) {
        zipReader.getEntries(readEntries);
    }, function (error) {
         console.log(error);
    });
});

我没有测试过这段代码,但我之前使用过类似的逻辑。