Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Javascript JSZip Example to Compress Multiple Files Using HTML5 Form & Download it as ZIP File

Posted on February 12, 2023

 

 

Welcome folks today in this blog post we will be compressing multiple files which are selected using html5 form by the user and then downloaded it as a zip file. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an index.html file and copy paste the following code

 

 

index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
</head>
<body>
    <input type="file" id="files" multiple required>
    <button onclick="download()">Compress & Download ZIP File</button>
</body>
<script src="script.js"></script>
</html>

 

 

As you can see we are including the jszip and filesaver.js library cdn library. And then inside the html we have the input field where we allow the user to select multiple files and then we have the button to compress and download it as zip file. And also we have binded a onclick event listener to call download() method.

 

Now we need to write the javascript code as shown below

 

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function download(){
    let zip = new JSZip()
 
    let files = document.getElementById('files')
 
    console.log(files.files)
 
    Array.from(files.files).forEach((f,i) => {
        zip.file(f.name,f)
    })
 
    zip.generateAsync({type:'blob'})
    .then((content) => {
        saveAs(content,"output.zip")
    })
}

 

 

As you can see in the above function we are making a new zip file and inside it we are using the foreach loop to add every file that the user selected using the html5 form. And lastly we are downloading the zip file as an attachment inside the browser using saveAs() method.

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme