JavaScript 实现的 zip 压缩和解压缩工具包Zip.js使用详解

 更新时间:2015年12月14日 11:26:48   投稿:hebedich  
今天给大家介绍的文章是js实现的解压缩插件zip.js,非常的简单实用,有需要的小伙伴可以参考下。

zip.js是什么

  zip.js的github项目地址:http://gildas-lormeau.github.io/zip.js/

  通过zip.js封装一个能在网页端生成zip文件的插件, 直接在网页中创建包含文件夹和文件的压缩包,也可以自定义名字并下载;

  如何使用:

  1:引用zip.js

  2:引用jQuery;

  3:并引用封装的ZipArchive.js ,(因为zip.js的api使用起来比较繁琐,所以自己封装实现了这个插件)

  4:引用mime-types.js;

  查看DEMO, 使用方式为:

运行下面代码

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <script src="http://gildas-lormeau.github.io/zip.js/demos/zip.js"></script>
  <script src="http://gildas-lormeau.github.io/zip.js/demos/mime-types.js"></script>
  <script src="http://apps.bdimg.com/libs/jquery/1.9.0/jquery.js"></script>
  <script src="http://files.cnblogs.com/files/diligenceday/ZipArchive.js"></script>
</head>
<body>

<script>
  var z = new ZipArchive;
  z.addFile("a/a.txt", "aaaaaaacontent");
  z.addFile("aaaa.txt", "aaaaaaaccccc");
  z.export("nono");
</script>
</body>
</html>

  DEMO在后面:文件下载下来, 文件夹的格式如下:

  

回到顶部

  创建压缩文件和文件夹的详细源代码:
运行下面代码

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <script src="http://gildas-lormeau.github.io/zip.js/demos/zip.js"></script>
  <script src="http://gildas-lormeau.github.io/zip.js/demos/mime-types.js"></script>
  <script src="http://apps.bdimg.com/libs/jquery/1.9.0/jquery.js"></script>
  <style>
    code{
      display: block;
      padding: 10px;
      background: #eee;
    }
  </style>
</head>
<body>
  <div>
    <h1>
      兼容性
    </h1>
    <div>
      <p>
        zip.js可以在所有的chrome浏览器和firefox浏览器中运行, 可以在safari6和IE10,以及IE10以上运行;
      </p>
      <p>
        如果要在IE9和safari中运行需要两个设置:
      </p>
      <code>
        1:zip.useWebWorkers == false
      </code>
      <code>
        2:并引用这个JS:https://bitbucket.org/lindenlab/llsd/raw/7d2646cd3f9b/js/typedarray.js
      </code>
    </div>
  </div>
<script>
  zip.workerScriptsPath = "http://gildas-lormeau.github.io/zip.js/demos/";
  /**
   * @desc 压缩文件;
   * @event onprogress, onend, onerror;
   * */
  var ZipArchive = function() {
    function noop() {};
    this.name = "未命名文件";
    this.zippedBlob = {};
    var _this = this;
    this.length = 0;
    this.onend = noop;
    this.onerror = noop;
    this.onprogress = noop;
    //创建一个延迟对象;
    var def = this.defer = new $.Deferred();
    zip.createWriter( new zip.BlobWriter("application/zip"), function(zipWriter) {
      _this.zipWriter = zipWriter;
      //继续执行队列;
      def.resolve();
    }, this.error );
  };

  ZipArchive.blob = function (filename, content) {
    return new Blob([ content ], {
      type : zip.getMimeType(filename)
    });
  };

  $.extend( ZipArchive.prototype, {
    /**
     * @desc 添加文件
     * @param String filename为文件的名字;
     * @param String content;
     * @param Object options 传参
     *   例如:{ level : 0} 压缩的等级,0 到 9;
     *   例如:{ comment : "提示文字" }
     *   例如:{ lastModDate : "最后编辑时间" }
     * */
    "addFile" : function ( filename , content, options) {
      var _this = this;
      blob = ZipArchive.blob(filename, content);
      //为了产生链式的效果, 必须把deferrer赋值给新的defer
      this.defer = this.defer.then(function() {
        var def = $.Deferred();

        _this.zipWriter.add(filename, new zip.BlobReader(blob)
            ,function() { // reader
              console.log("addFile success!!");
              def.resolve();
              //zipWriter.close(callback);
            }, function (size, total) { //onend
              _this.onend(filename, blob, total);
              _this.length += total;
            }, function () { //onprogress
              _this.onprogress(filename, blob, total);
            },options || {
              //options
            });

        return def;
      });
    },

    /**
     * @desc 添加文件夹, 我发现这个文件无法创建;
     * @desc 创建文件夹功能不好用, 需要创建文件夹你通过 zipWriter.addFile("directory/filename.txt", blob())创建文件夹和对应文件;;
     * */
    "_addFolder" : function (foldername , options) {
      //创建文件夹功能目前不能用;
      //创建文件夹功能不好用, 直接通过 zipWriter.addFile("directory/filename.txt", blob())创建文件夹和文件
      return this;
    },

    "size" : function () {
      return this.length;
    },

    /**
     * @desc 获取blob文件
     * */
    "get" : function () {
      return this.zippedBlob;
    },

    /**
     * @desc 导出为zip文件
     * */
    "export" : function ( name ) {
      name = name || this.name;
      var _this = this;
      this.defer.then(function() {
        _this.zipWriter.close(function( zippedBlob ) {
          if( typeof name === "string" || typeof name === "number") {
            var downloadButton = document.createElement("a"),
                URL = window.webkitURL || window.mozURL || window.URL;
            downloadButton.href = URL.createObjectURL( zippedBlob );
            downloadButton.download = name + ".zip";
            downloadButton.click();
          }else{
            name( zippedBlob );
          };
        });
      });
    },

    "error" : function() {
      this.onerror( this );
      throw new Error("压缩文件创建失败");
    }
  });

</script>
<script>
  var z = new ZipArchive;
  z.addFile("a/a.txt", "aaaaaaacontent");
  z.addFile("aaaa.txt", "aaaaaaaccccc");
  z.export("nono");
</script>
</body>
</html>

相关文章

  • layui框架与SSM前后台交互的方法

    layui框架与SSM前后台交互的方法

    今天小编就为大家分享一篇layui框架与SSM前后台交互的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • 写了几个类,希望对大家有用。

    写了几个类,希望对大家有用。

    写了几个类,希望对大家有用。...
    2006-12-12
  • textarea支持图形编辑的实现方法

    textarea支持图形编辑的实现方法

    本来以为只有iframe支持编辑了,今天突然发现textarea也支持编辑 :( 是不是我太愚钝了? textarea不能用innerHTML来插入html,而用appendChild 这样的话稍微修改下,评论等简单的textarea的表情插入就可以直接显示出来了 而不是只显示emot了 比较实用标题起的确切,但如果叫“现textarea支持编辑”似乎更废话
    2008-03-03
  • 支付宝小程序实现类似微信多行输入功能(思路详解)

    支付宝小程序实现类似微信多行输入功能(思路详解)

    这篇文章主要介绍了支付宝小程序实现类似微信多行输入功能,输入超过 8 行的时候会出现滚动,这样做的好处就是输入框不会直接顶到页面最顶部,支付宝小程序实现多行输入框:使用textarea多行输入框实现,感兴趣的朋友一起看看吧
    2024-02-02
  • A标签中通过href和onclick传递的this对象实现思路

    A标签中通过href和onclick传递的this对象实现思路

    想传递当前对象给一个函数,于是就将这个URL写成"Javascript:shoControlSidebar(this)",可是结果发现这并不可行,接下来为大家详细介绍下解决方法
    2013-04-04
  • js不同客户端显示不同广告(pc端+移动端+微信端)

    js不同客户端显示不同广告(pc端+移动端+微信端)

    这篇文章主要介绍了js不同客户端显示不同广告(pc端+移动端+微信端),比较适合自适应网站的广告,需要的朋友可以参考下
    2023-02-02
  • 微信小程序实现列表条件筛选

    微信小程序实现列表条件筛选

    这篇文章主要为大家详细介绍了微信小程序实现列表条件筛选,筛选框的效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • webpack常用构建优化策略小结

    webpack常用构建优化策略小结

    这篇文章主要介绍了webpack常用构建优化策略小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 在小程序中使用Echart图表的示例代码

    在小程序中使用Echart图表的示例代码

    这篇文章主要介绍了在小程序中使用Echart图表的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • JavaScript实现计算字符串中出现次数最多的字符和出现的次数

    JavaScript实现计算字符串中出现次数最多的字符和出现的次数

    这篇文章主要介绍了JavaScript实现计算字符串中出现次数最多的字符和出现的次数,本文直接给出实现代码,需要的朋友可以参考下
    2015-03-03

最新评论