简体   繁体   English

将zip下载到文件中并使用Node.js解压缩-如何检测下载何时完成以及是否可以安全解压缩

[英]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. 我需要编写两个单独的组件-一个组件是一个下载器,它采用文件路径(或FILE DESCRIPTOR,看似更好的文件)和url并将该URL中存在的任何文件下载到文件路径中。 The code that I have written so far for the downloader component is : https://gist.github.com/akshita31/b3df6f8b45da342b7a8a4bd24cdf6c1c 到目前为止,我为下载程序组件编写的代码是: 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 安装程序的代码是: https : //gist.github.com/akshita31/9e86c1090622c462cc357b3374acba4c

I have a third utility that creates a tmpFile(using npm tmp) - https://gist.github.com/akshita31/92878afbb0943d3a95e14193ddf9f64b . 我的第三个工具,它创建了一个TMPFILE(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. 经过大量的搜索之后,我以某种方式了解到它可能与WriteStream上的“ finish”事件有关,但是如果我仅解决下载程序,则程序将等待很多时间,然后超时。 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. 处理文件流时,我意识到我可以在writestream上侦听“ finish”事件,但这意味着有一个file.end,因此文件描述符将被关闭,这不是我们想要的。 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. 因此,我改为使用缓冲区,而不是将下载内容写入文件流,然后使用yauzl.fromBuffer从缓冲区读取zip。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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