


JavaScript implements decompression and viewing of zip files on the web page_javascript skills
WEB front-end decompression ZIP package
What is the use of web front-end to decompress zip files:
If we only consider standard browsers, the server only needs to transmit the compressed package to the client, which saves bandwidth and transmission time, which sounds very impressive;
If there is a lot of front-end code and contains large pictures, then various data such as js, css, jpg and png can be packaged into zip through the server and sent to the browser. The browser is responsible for decompression. CSS practical dynamics Generate and insert into the dom, js is also directly executed using globalEval, various jpg or png image files are converted from blob streams into images, and directly inserted into the browser;
HTML5 supports reading Blobs (binary large objects, file files also inherit Blobs) and converts them into image streams or text streams or other stream formats. This is why browsers can read "application/zip" files. ;
To decompress zip files in the browser, you need to introduce four js, because UnZipArchive.js depends on zip.js, mime-type.js and jquery.js. The test demo is as follows:
<!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/UnZipArchive.js"></script> </head> <body> <h2> demo </h2> <div> <input type="file" id="file"> </div> <ul id="dir"> </ul> <script> $("#file").change(function (e) { var file = this.files[0]; window.un = new UnZipArchive( file ); un.getData( function() { //获取所以的文件和文件夹列表; var arr = un.getEntries(); //拼接字符串 var str = ""; for(var i=0; i<arr.length; i++ ) { //点击li的话直接下载文件; str += "<li onclick=download('"+arr[i]+"')>"+arr[i]+"</li>" }; $("#dir").html( str ); }); }); var download = function ( filename ) { un.download( filename ); }; </script> </body> </html>
UnzioarichiveJS is encapsulated by itself. If you have any questions, please feedback in time
Complete DEMO of decompressing ZIP compressed package
<!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> <h2> demo </h2> <div> <input type="file" id="file"> </div> <ul id="dir"> </ul> <script> $("#file").change(function (e) { var file = this.files[0]; window.un = new UnZipArchive( file ); un.getData( function() { var arr = un.getEntries(); var str = ""; for(var i=0; i<arr.length; i++ ) { str += "<li onclick=download('"+arr[i]+"')>"+arr[i]+"</li>" }; $("#dir").html( str ); }); }); var download = function ( filename ) { un.download( filename ); }; </script> </div> <script> zip.workerScriptsPath = "http://gildas-lormeau.github.io/zip.js/demos/"; /** * @desc 解压缩文件的类; * @return UnZipArchive 的实例; * */ var UnZipArchive = function( blob ) { if( !blob ) { alert("参数不正确, 需要一个Blob类型的参数"); return ; }; if( !(blob instanceof Blob) ) { alert("参数不是Blob类型"); return ; }; function noop() {}; this.entries = {}; this.zipReader = {}; var _this = this; this.length = 0; this.onend = noop; this.onerror = noop; this.onprogress = noop; //创建一个延迟对象; var def = this.defer = new $.Deferred(); zip.createReader( new zip.BlobReader( blob ), function(zipReader) { _this.zipReader = zipReader; zipReader.getEntries(function(entries) { _this.entries = entries; //继续执行队列; def.resolve(); }); }, this.error.bind(_this) ); }; /** * @desc 把blob文件转化为dataUrl; * */ UnZipArchive.readBlobAsDataURL = function (blob, callback) { var f = new FileReader(); f.onload = function(e) {callback( e.target.result );}; f.readAsDataURL(blob); }; $.extend( UnZipArchive.prototype, { /** * @desc 获取压缩文件的所有入口; * @return ArrayList; * */ "getEntries" : function() { var result = []; for(var i= 0, len = this.entries.length ; i<len; i++ ) { result.push( this.entries[i].filename ); } return result; }, /** * @desc 获取文件Entry; * @return Entry * */ "getEntry" : function ( filename ) { var entrie; for(var i= 0, len = this.entries.length ; i<len; i++ ) { if( this.entries[i].filename === filename) { return this.entries[i]; }; } }, /** * @desc 下载文件 * @param filename; * @return void; * */ "download" : function ( filename , cb , runoninit) { var _this = this; this.defer = this.defer.then(function() { var def = $.Deferred(); if(!filename) return ; if(runoninit) { return runoninit(); }; var entry = _this.getEntry( filename ); if(!entry)return; entry.getData(new zip.BlobWriter(zip.getMimeType(entry.filename)), function(data) { if( !cb ) { UnZipArchive.readBlobAsDataURL(data, function( dataUrl ) { var downloadButton = document.createElement("a"), URL = window.webkitURL || window.mozURL || window.URL; downloadButton.href = dataUrl; downloadButton.download = filename; downloadButton.click(); def.resolve( dataUrl ); _this.onend(); }); }else{ cb( data ); def.resolve( data ); } }); return def; }); }, /** * @desc 获取对应的blob数据; * @param filename 文件名; * @param callback回调, 参数为 blob; * @desc 或者可以直接传一个函数作为zip解压缩完毕的回调; * */ "getData" : function ( filename, fn ) { if( typeof filename === "string") { this.download(filename, function( blob ) { fn&&fn( blob ); }); }else if( typeof filename === "function") { this.download("test", null, function( blob ) { filename(); }); }; }, "error" : function() { this.onerror( this ); throw new Error("压缩文件解压失败"); } }); </script> </body> </html>
But browser compatibility is a big problem;

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version
