Home Web Front-end H5 Tutorial Code example of application of Zip compression and decompression technology in HTML5 (picture)

Code example of application of Zip compression and decompression technology in HTML5 (picture)

Mar 27, 2017 pm 03:52 PM

JSZip is a javaScript tool that can create, read, and modify .zip files. In web applications, it is inevitable to obtain resources from the web server. If all resources can be merged into a .zip file, then only one request is required, which not only reduces the pressure on the server, but also speeds up the web process. The application's rendering speed.

Today let’s discuss how JSZip is combined with HT applications. Let’s take a look at the renderings of this Demo:

The first step is to package the application and related resources into a .zip file,

#This is the list of files I want to compress, store the corresponding resource files in the corresponding folder, and then indicate the resource loading in the loadorder file In order, the content of the loadorder file is as follows:

1

2

3

4

5

6

'js/ht.js',


'js/ht-obj.js',


'js/ht-modeling.js',


'obj/equipment.mtl',


'obj/equipment.obj',


'image/equipment.jpg'

Copy after login

In the resource loading order, the path of the response resource relative to the .zip file should be indicated, so that it can be quickly found when reading the .zip file. Corresponding resource files.

The second step is to introduce JSZip and the JSZipUtils library into the html file. The next step is to request the .zip file and parse the .zip file.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

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(&#39;js/&#39;) >= 0) {

            var js = document.createElement(&#39;script&#39;);

            js.innerHTML = zip.file(fileName).asText();

            document.getElementsByTagName(&#39;head&#39;)[0].appendChild(js);

        } else if(fileName.indexOf(&#39;image/&#39;) >= 0) {

            var buffer = zip.file(fileName).asArrayBuffer(),

                    str = _arrayBufferToBase64(buffer),

                    pIndex = fileName.indexOf(&#39;.&#39;),

                    type = fileName.substr(pIndex + 1),

                    re = &#39;data:image/&#39; + type + &#39;;base64,&#39;;

 

            image[fileName] = re + str;

        } else if(fileName.indexOf(&#39;obj/&#39;) >= 0) {

            var str = zip.file(fileName).asText();

            if(fileName.indexOf(&#39;.mtl&#39;) > 0) {

                mtlStr = str;

            } else if(fileName.indexOf(&#39;.obj&#39;) > 0) {

                objStr = str;

            }

        }

    }

 

    init(objStr, mtlStr, image);

});

Copy after login

First obtain the .zip file through JSZipUtils, and load the obtained file content into the zip through the new JSZip(data) method Variable , read the loadorder file content through zip.file(fileName), use the eval command to dynamically execute the script, convert the text content into the js variable order, and finally, by traversing the order variable, dynamically introduce the js resource into the page.

There are image files in the .zip file. JSZip can only obtain the ArrayBuffer data of the image file. At this time, the ArrayBuffer needs to be converted to Base64 to be recognized by the browser, so A conversion function is defined here: _arrayBufferToBase64

1

2

3

4

5

6

7

8

9

function _arrayBufferToBase64( buffer ) {

    var binary = &#39;&#39;;

    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 );

}

Copy after login

This case involves 3D model data and HT 3D topology application The obj directory in the .zip file stores 3D model data. During file reading, the 3D model data is read out in the form of text pairs and stored in variables, and then the data is passed to the init function, through ht The .Default.parseObj() method loads 3D model data into HT.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

function init(objStr, mtlStr, image) {

    dataModel = new ht.DataModel();

    g3d = new ht.graph3d.Graph3dView(dataModel);

 

    view = g3d.getView();

    view.className = &#39;main&#39;;

    document.body.appendChild(view);

    window.addEventListener(&#39;resize&#39;, function (e) {

        g3d.invalidate();

    }, false);

 

    g3d.setEye([0, 500, 1000]);

    g3d.setCenter([0, 200, 0]);

    g3d.setGridVisible(true);

    g3d.setGridColor(&#39;#74AADA&#39;);

 

    var param = {

        shape3d: &#39;E1&#39;,

        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.lastIndexOf(&#39;/&#39;),

                fileName = i.substr(index + 1),

                rawS3 = map.rawS3;

        for(var imgName in image) {

            if(imgName.indexOf(fileName) >= 0) {

                ht.Default.setImage(i, 256, 256, image[imgName]);

            }

        }

    }

 

    var node = new ht.Node();

    node.s({

        &#39;shape3d&#39;: &#39;E1&#39;,

        &#39;wf.visible&#39;: &#39;selected&#39;,

        &#39;wf.width&#39;: 3,

        &#39;wf.color&#39;: &#39;#F7F691&#39;

    });

    node.s3(param.rawS3);

    node.p3(0, param.rawS3[1]/2, 0);

    dataModel.add(node);

}

Copy after login

The above is the code to generate 3D topology, 3D model introduction and reference 3D model creation topology node. The setImage code needs special attention. Why do I have to go through so much trouble to determine the file name of the image? That’s because there is a attribute for setting the texture in the mtl 3D model description file. This The attribute can specify the absolute path of the file or the relative path of the file. Because JSZip cannot write the file contents in the .zip back to the local directory, the attribute name corresponding to the texture attribute can only be used as HT# The image name in ## is set to HT so that when the HT model is loaded, the image resources required by the model can be obtained. For the application of HT 3D topology, please refer to "3D Topology Automatic Layout Node.jsPart".

JSZip If the speed is slow when compressing or decompressing data, you can consider using Web Worker.

The above is the detailed content of Code example of application of Zip compression and decompression technology in HTML5 (picture). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles