Application of ZIP compression and decompression technology in HTML5 browser

Source: Internet
Author: User

Jszip is a javaScript tool that can create, read, and modify. zip files. In a Web application, you need to get resources from a Web server, and if you can merge all of your resources into a single. zip file, you only need to make one request at a time, which reduces the pressure on the server and speeds up the rendering of your Web application.

Discuss how the next jszip can be combined with the HT topology application today. Let's take a look at this Demo:

The first step is to package the app into a. zip file for the relevant resources,

This is the list of files that I want to compress, the response resource file is stored in the corresponding folder, and then in the Loadorder file to indicate the order of the resource loading, the Loadorder file contents are as follows:

' Js/ht.js ',? ' Js/ht-obj.js ',? ' Js/ht-modeling.js ',? ' Obj/equipment.mtl ',? ' Obj/equipment.obj ',? ' Image/equipment.jpg '

In the resource load order, indicate the path of the response resource relative to the. zip file, which makes it easy to quickly find the appropriate resource file when reading the. zip file.

Step two, introduce jszip and Jsziputils libraries in the HTML file, and then request the. zip file and the. zip file for parsing processing.

Jsziputils.getbinarycontent (' Res/importobj.zip ', function (err, data) {if (err) {throw err;//or Handle err    } var zip = new Jszip (data);    var loadorderstr = zip.file (' Loadorder '). Astext (), order;    Eval (' order = [' + loadorderstr + '] ');    var len = order.length, image = {}, Mtlstr = ', Objstr = ';        for (var i = 0; i < len; i++) {var fileName = order[i];            if (Filename.indexof (' js/') >= 0) {var js = document.createelement (' script ');            js.innerhtml = Zip.file (fileName). Astext ();        document.getElementsByTagName (' head ') [0].appendchild (JS);                    } else if (Filename.indexof (' image/') >= 0) {var buffer = Zip.file (fileName). Asarraybuffer (), str = _arraybuffertobase64 (buffer), Pindex = Filename.indexof ('. '), type = File       NAME.SUBSTR (Pindex + 1), re = ' data:image/' + type + '; base64, ';     Image[filename] = re + str;            } else if (Filename.indexof (' obj/') >= 0) {var str = zip.file (fileName). Astext ();            if (Filename.indexof ('. MTL ') > 0) {mtlstr = str;            } else if (Filename.indexof ('. obj ') > 0) {objstr = str; }}} init (Objstr, mtlstr, image);});

First get the. zip file via jsziputils , load the retrieved file contents into the zip variable via the new Jszip method, read the Loadorder file contents through Zip.file (fileName), try E The Val command dynamically executes the script, converts the text content to the JS variable order, and finally introduces the JS resource to the page by traversing the order variable.

In the. zip file contains the picture file, Jszip can only obtain the ArrayBuffer data of the picture file, the need to convert ArrayBuffer to Base64 to be recognized by the browser, so here defines a conversion function: _ ArrayBufferToBase64

function _arraybuffertobase64 (buffer) {    var binary = ';    var bytes = new Uint8array (buffer);    var len = bytes.bytelength;    for (var i = 0; i < len; i++) {        binary + = String.fromCharCode (bytes[i]);    }    return Window.btoa (binary);}

in this case, there is a combination of 3D model data and ht 3D topology application, where the obj directory in the. zip file is the 3D model data, In the file reading, the model data is read in text to the situation and stored in the variable, and then the data is passed to the INIT function through HT. The Default.parseobj () method loads the model data into the ht .

function init (objstr, mtlstr, image) {Datamodel = new ht.    Datamodel ();    G3d = new Ht.graph3d.Graph3dView (Datamodel);    view = G3d.getview ();    View.classname = ' main ';    Document.body.appendChild (view);    Window.addeventlistener (' Resize ', function (e) {g3d.invalidate ();    }, False);    G3d.seteye ([0, 500, 1000]);    G3d.setcenter ([0, 200, 0]);    G3d.setgridvisible (TRUE);    G3d.setgridcolor (' #74AADA ');    var param = {shape3d: ' E1 ', Center:true, cube:true}; var modelmap = ht.    Default.parseobj (objstr, mtlstr, param); for (var model in Modelmap) {var map = Modelmap[model], i = map.image, index = I.last        IndexOf ('/'), FileName = I.SUBSTR (index + 1), rawS3 = MAP.RAWS3; for (var imgname in image) {if (Imgname.indexof (fileName) >= 0) {ht.            Default.setimage (i, N., Image[imgname]); }}} var node = new ht.   Node (); NODE.S ({' Shape3d ': ' E1 ', ' wf.visible ': ' Selected ', ' Wf.width ': 3, ' wf.color ': ' #F7F691 '}    );    NODE.S3 (PARAM.RAWS3);    NODE.P3 (0, PARAM.RAWS3[1]/2, 0); Datamodel.add (node);}

The above is the code that generates a 3D topology , introduces a 3D model, and references a 3D model to create a topology node. One of the setimage code needs to pay special attention, why do I have to take the time to judge the file name of image, that is because in the MTL 3D model description file has a property to set the map, the property can specify the absolute path of the file, you can also specify the relative path of the file, because the Jszip cannot write the contents of the file in the. zip back to the local directory, so you can only set the attribute name of the map property to HT as the image name in HT, so that the HT model can get the picture resources needed to get the model when it is loaded. Applications for HT 3D topologies can be found in thenode. JS chapter of the 3D topology auto-layout .

Jszip when compressing or extracting data, if there is a slow situation, you can consider using the Web Worker,web Worker specific application can refer to the3D topology automatic layout of the Web workers chapter .

Application of ZIP compression and decompression technology in HTML5 browser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.