Socket
Socket
Sign inDemoInstall

jszip

Package Overview
Dependencies
12
Maintainers
3
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

jszip

Create, read and edit .zip files with JavaScript http://stuartk.com/jszip


Version published
Maintainers
3
Weekly downloads
7,336,162
decreased by-0.6%
Bundle size
28.2 kB
Minified + gzipped

Weekly downloads

Package description

What is jszip?

The jszip npm package is a library for creating, reading, and editing .zip files with JavaScript. It allows for the manipulation of zip files directly in the browser or in a Node.js environment. With jszip, users can generate new zip files, add files and folders to them, extract their contents, and more.

What are jszip's main functionalities?

Creating a new zip file

This code creates a new zip file with a single file 'Hello.txt' containing the text 'Hello World' and saves it as 'example.zip'.

const JSZip = require('jszip');
const zip = new JSZip();
zip.file('Hello.txt', 'Hello World');
zip.generateAsync({type: 'nodebuffer'}).then(function(content) {
  require('fs').writeFileSync('example.zip', content);
});

Adding a folder and files

This code adds a folder named 'images' to the zip file and then adds a file 'smile.gif' with base64 encoded image data to this folder.

const JSZip = require('jszip');
const zip = new JSZip();
const imgFolder = zip.folder('images');
imgFolder.file('smile.gif', imgData, {base64: true});
zip.generateAsync({type: 'nodebuffer'}).then(function(content) {
  require('fs').writeFileSync('example.zip', content);
});

Reading a zip file

This code reads an existing zip file 'example.zip' and logs the names of all files contained within it.

const JSZip = require('jszip');
const fs = require('fs');
const zip = new JSZip();
fs.readFile('example.zip', function(err, data) {
  if (err) throw err;
  zip.loadAsync(data).then(function(contents) {
    Object.keys(contents.files).forEach(function(filename) {
      console.log(filename);
    });
  });
});

Extracting a file from a zip

This code extracts the content of the file 'Hello.txt' from the zip file 'example.zip' and logs it to the console.

const JSZip = require('jszip');
const fs = require('fs');
const zip = new JSZip();
fs.readFile('example.zip', function(err, data) {
  if (err) throw err;
  zip.loadAsync(data).then(function() {
    zip.file('Hello.txt').async('string').then(function(content) {
      console.log(content);
    });
  });
});

Other packages similar to jszip

Readme

Source

JSZip

A library for creating, reading and editing .zip files with JavaScript, with a lovely and simple API.

See https://stuk.github.io/jszip for all the documentation.

const zip = new JSZip();

zip.file("Hello.txt", "Hello World\n");

const img = zip.folder("images");
img.file("smile.gif", imgData, {base64: true});

zip.generateAsync({type:"blob"}).then(function(content) {
    // see FileSaver.js
    saveAs(content, "example.zip");
});

/*
Results in a zip containing
Hello.txt
images/
    smile.gif
*/

License

JSZip is dual-licensed. You may use it under the MIT license or the GPLv3 license. See LICENSE.markdown.

Keywords

FAQs

Last updated on 02 Aug 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc