Subscribe For Free Updates!

We'll not spam mate! We promise.

Mar 3, 2013

Javascript to create zip file




 The Zip file format is very useful for compress the any type of data. we can generate the zip file by using some programming language.
Let us see how to create zip file by using javascript.

1. First we need javascript package jszip.

2. import jszip package in HTML document.

3 use the below code :
      <script type="text/javascript" src="https://raw.github.com/Stuk/jszip/master/jszip.js"></script>
4. Now generate ZIP file like ,

<html>
<head>
    <script type="text/javascript" src="jszip.js"></script>

<script>
 function create_zip() {
    var zip = new JSZip();
    zip.add("hello1.txt", "Hello First World\n");
    zip.add("hello2.txt", "Hello Second World\n");
    content = zip.generate();
    location.href="data:application/zip;base64," + content;
}

</script>

</head>

<body>
     <a href="" onclick="create_zip()">Create Zip</a>
 </body>
</html>


The function create_zip() calls jszip API to generate ZIP file


TechniqZone Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment