简体   繁体   中英

Downloading a zip into a file and unzipping it using nodejs - how to detect when the download is complete and it is safe to unzip

I need to write two separate components - one is a downloader which takes a file path(or a FILE DESCRIPTOR whichever seems better) and url and downloads whatever file is present in that url into the file path. The code that I have written so far for the downloader component is : https://gist.github.com/akshita31/b3df6f8b45da342b7a8a4bd24cdf6c1c

Once the download is complete, I have an installer component that unzips the file present at that file descriptor and puts them at a destination path. Code for the installer is: https://gist.github.com/akshita31/9e86c1090622c462cc357b3374acba4c

I have a third utility that creates a tmpFile(using npm tmp) - https://gist.github.com/akshita31/92878afbb0943d3a95e14193ddf9f64b .

Finally I do:

tmpFile = await createTmpFile();
await DownloadFile(tmpFile.fd, pkg.description, pkg.url, pkg.fallbackUrl, eventStream, provider);
await InstallPackage(tmpFile.fd, pkg.description, pkg.installPath, pkg.binaries, eventStream);

But in the installation I sometimes get - 'End of central directory record signature not found' which to me suggests that the file was not completely downloaded and put in to the desired path and we are trying to read it before that. After a lot of googling I somehow understood that it might have to do something with 'finish' event on the WriteStream, but if I resolve the downloader just on that my program waits a lot of time and then times out. What is it that I am doing wrong ?

Working around with file streams I realised I could listen to the 'finish' event on the writestream but that would mean a file.end and hence the file descriptor would be closed which is not what we would want. So I instead used the buffer instead of writing the download to a file stream and then use yauzl.fromBuffer to read the zip from the buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM