JavaScript实现的zip压缩和解压缩⼯具包Zip.js使⽤详解
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="gildas-lormeau.github.io/zip.js/demos/zip.js"></script>
<script src="gildas-lormeau.github.io/zip.js/demos/mime-types.js"></script>
<script src="apps.bdimg/libs/jquery/1.9.0/jquery.js"></script>
<script src="filesblogs/files/diligenceday/ZipArchive.js"></script>
</head>
<body>
<script>
var z = new ZipArchive;
z.addFile("", "aaaaaaacontent");
z.addFile("", "aaaaaaaccccc");
</script>
</body>
</html>
DEMO在后⾯:⽂件下载下来,⽂件夹的格式如下:
回到顶部
创建压缩⽂件和⽂件夹的详细源代码:
运⾏下⾯代码
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="gildas-lormeau.github.io/zip.js/demos/zip.js"></script>
<script src="gildas-lormeau.github.io/zip.js/demos/mime-types.js"></script>
<script src="apps.bdimg/libs/jquery/1.9.0/jquery.js"></script>
<style>
code{
display: block;
padding: 10px;
background: #eee;
}
</style>
</head>
<body>
<div>
sendfor是什么意思<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:/lindenlab/llsd/raw/7d2646cd3f9b/js/typedarray.js
</code>
</div>
</div>
<script>
zip.workerScriptsPath = "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;
//创建⼀个延迟对象;
var def = this.defer = new $.Deferred();
_this.zipWriter = zipWriter;
//继续执⾏队列;
}, );
};jquery下载文件插件
ZipArchive.blob = function (filename, content) {
return new Blob([ content ], {
type : MimeType(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!!");
//zipWriter.close(callback);
}, function (size, total) { //onendspss中bootstrap结果解读
_d(filename, blob, total);
_this.length += total;
}, function () { //onprogress
_progress(filename, blob, total);
},options || {
/
/options
});
return def;
});
},
/**
* @desc 添加⽂件夹, 我发现这个⽂件⽆法创建;
* @desc 创建⽂件夹功能不好⽤, 需要创建⽂件夹你通过 zipWriter.addFile("", blob())创建⽂件夹和对应⽂件;; * */
"_addFolder" : function (foldername , options) {
//创建⽂件夹功能⽬前不能⽤;
//创建⽂件夹功能不好⽤, 直接通过 zipWriter.addFile("", blob())创建⽂件夹和⽂件
return this;
},
"size" : function () {
return this.length;
},
delete键是什么功能/**
* @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 = ateElement("a"),
URL = window.webkitURL || URL || window.URL;
downloadButton.href = ateObjectURL( zippedBlob );
downloadButton.download = name + ".zip";
downloadButton.click();
}else{perl的界面
name( zippedBlob );
};
});
});
},
"error" : function() {
throw new Error("压缩⽂件创建失败");
}
});
</script>
<script>
var z = new ZipArchive;
z.addFile("", "aaaaaaacontent");
z.addFile("", "aaaaaaaccccc");
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论