Search code examples
angularuploadpasswordszipverification

In Angular, how to just verify the password of a zip file before upload


I'm working on an application where users can upload zip files which are always password protected. I have a file-uploader, and a simple text-input for that password.

Currently, the password is only checked in the BE after the file upload (which might take several minutes, since the files can be several GB) and when the password is incorrect, the user will get an application message (not really important how that works) saying 'upload failed, provided wrong password' and they have to start the entire process from scratch.

I want to make it so that the upload process won't even start when the provided password is incorrect. So; how do i only verify the password of a zip file in Angular(9) without extracting that entire file?


Solution

  • hello on StackOverflow!

    I would say it is not easily doable to check the validity of the password beforehand, but some projects exist that may help you.

    The Reason is that browsers do not have access to a filesystem yet. (The Native Filesystem API is currently in chrome origin trial, check here for more information and future updates on browser compatibility)

    You could try to use an in-memory filesystem in the browser as a replacement, for example memfs in combination with memfs-webpack.

    But keep in mind:

    • Browsers limit the amount of RAM available to a webpage. If your zip is too big, it will likely crash your website!
    • A zip extraction process can be very long. Use a service worker for extracting the zip into the in-memory file system.
    • Since Browsers do not have file system access normally, there's probably no zip extraction library that is able to run in the browser. You may need to port an existing library from nodejs to be able to run in the browser.

    I found this page, which seems to be a zip extract in pure JavaScript in the Browser: https://zipextractor.app/
    They claim to not send the files to any server for extraction, so you may be able to find the tools there you need to implement that functionality for yourself.

    I hope this was helpful!
    Benjamin