Extract ZIP File at Relative Target Location Angular 13 Tutorial

In this article, you will learn how to extract or uncompress a ZIP file using the npm script inside the package.json of the Angular 13 project.

In our previous tutorial, we discussed how to extract the zip files to the root folder, move them to a specific folder using glob in angular.json then clean up. Then see those extracted files in the dist folder.

We used the extract-zip package module for the ZIP file extraction. But there was a limitation if you use the extract-zip package. You can give a relative destination path as there is a check inside the source code that needs the destination path as an Absolute path only.

If you add a script in the package.json as below:

"scripts": {
    ...
    "extract-my-files": "extract-zip ./my-images.zip ./src/assets"
  },

Where ./src/assets is the destination path, it will show the following error:

error! Error: Target directory is expected to be absolute

If you check the source code you will notice a check, that is throwing an error if the target path is not Absolute:

...
module.exports = async function (zipPath, opts) {
  debug('creating target directory', opts.dir)

  if (!path.isAbsolute(opts.dir)) {
    throw new Error('Target directory is expected to be absolute')
  }

  await fs.mkdir(opts.dir, { recursive: true })
  opts.dir = await fs.realpath(opts.dir)
  return new Extractor(zipPath, opts).extract()
}
...

 

Solution:

To resolve this, we need to comment on this check. Due to this reason, I have published the extract-zip-relative-path package on NPM.

You can install it as shown below:

npm install extract-zip-relative-path

The only motive was to remove the isAbsolute check from the source code. This is the fork from the actual repository from its creator github.com/maxogden/extract-zip

Please use this extract-zip-relative-path package at your own risk. The only change you can see in this file .

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top