Skip to content

Deno: Create Zip files

Deno, with its built-in standard library, doesn’t have a native module to create or manipulate zip files as of my last update. However, you can achieve this by using third-party modules or by directly implementing it in Deno. The jszip library is one popular choice for creating zip files in JavaScript. You can use it in Deno by leveraging its compatibility with web standards.

Firstly, install jszip using import_map.json

{
  "imports": {
    "jszip": "https://cdn.skypack.dev/jszip"
  }
}

Then, create a Deno script to use jszip

import JSZip from 'jszip';

const createZip = async () => {
  const zip = new JSZip();

  // Add files or folders to the zip
  zip.file('hello.txt', 'Hello World!');
  zip.folder('images').file('image.jpg', getImageBytes()); // Replace getImageBytes with your image data

  // Generate the zip content
  const content = await zip.generateAsync({ type: 'blob' });

  // Save the zip file
  await Deno.writeFile('example.zip', content);
};

// Call the function to create the zip
createZip().then(() => console.log('Zip file created')).catch(console.error);

In this example, replace getImageBytes() with your function that fetches or generates image data as an array buffer or bytes.

Remember to use appropriate Deno permissions when working with files, especially when writing to the file system (--allow-write flag for writing files and --unstable for some features like Deno.writeFile).

Always verify the documentation or the specific version compatibility of the modules you’re using, as updates or changes may affect the code.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments