[英]Copying file using node.js with the possibility of cancellation
I am implementing a node.js application which copies an array of file paths, into an specific destination. 我正在实现一个node.js应用程序,该程序将文件路径数组复制到特定的目标位置。
The method is simple, Let's say I have 10 file paths at the Origin paths, and 1 path as the destination path. 该方法很简单,假设我在原始路径中有10个文件路径,在目标路径中有1个路径。 so Each From path will be copied to the destination ONE By ONE using fs.copy function. 因此,将使用fs.copy函数将Every From路径一一复制到目标位置 。
However, as the file array might be large in terms of number and size therefore I am trying to implement a feature which allows user to cancel the operation of copying files and removing whatever has been already copied. 但是,由于文件数组的数量和大小可能很大,因此我试图实现一种功能,该功能允许用户取消复制文件的操作并删除已复制的内容。
My question is , Is it possible to have cancellation operation on fs.copy ? 我的问题是 ,可以对fs.copy进行取消操作吗? For example how can I stop fs.copy operation while it is trying to copy a 2GB file? 例如,在尝试复制2GB文件时如何停止fs.copy操作?
Is/Are there any alternatives besides fs.copy to achieve what I want? 除了fs.copy之外,是否有其他选择可以实现我想要的?
I guess you could just pipe a readsteam to a writestream, and the unpipe and unlink when you want to stop the copy and delete the destination file. 我想您可以通过管道将readsteam传递到writestream,然后在要停止复制并删除目标文件时通过unpipe和取消链接。
var from = fs.createReadStream(fromFilename)
.pipe(to);
var to = fs.createWriteStream(toFilename)
.on("unpipe", function() {
fs.unlink(toFilename); // effectively deletes the file
});
// triggered by user
from.unpipe(); // stops the copy, triggers the delete
Would that work? 那行得通吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.