无法使用读/写流正确解析和重组二进制文件

时间:2022-02-22 21:24:13

I'm trying to get certain segments of a binary file and then recombine them into a condensed format. However, upon completion of this task the binary data is scrambled in a semi-random order. In fact, the first roughly 15k bytes are correct then conflicts occur continuously after this. What is going wrong? I believe that these streams are running into one another (for lack of a better metaphor) and the resultant byte length is correct. Why is this happening? I thought I was taking adequate precautions as to asynchronicity?

我试图获取二进制文件的某些段,然后将它们重新组合成一个压缩格式。但是,完成该任务后,二进制数据以半随机顺序加扰。实际上,第一个大约15k字节是正确的,然后在此之后不断发生冲突。出了什么问题?我相信这些流相互竞争(缺乏更好的比喻)并且结果字节长度是正确的。为什么会这样?我以为我对异步性采取了足够的预防措施?

                 var destinationPath = something.bin;
                 Promise.all(sources.map(function (source) {
                    return new Promise(function (resolve, reject) {
                        var output = fs.createWriteStream(destinationPath, {'flags': 'a', 'bufferSize': 64 * 4096});
                        var input = fs.createReadStream(source.filePath, {
                            start: source.parameters.start,
                            end: source.parameters.end + 3
                        });
                        input.on('end', function () {
                            output.on('finish', function () {
                                resolve();
                            });
                        });
                        input.pipe(output);
                    });
                }));

1 个解决方案

#1


1  

not sure if this has an impact, but did you try sequentially ? You create many write streams at the same time on the same file.

不确定这是否有影响,但你是否顺序尝试?您可以在同一文件上同时创建许多写入流。

For example:

             var destinationPath = something.bin;
             var promise = Promise.resolve();
             sources.forEach(function (source) {
               promise = promise.then(funtion() {
                 return new Promise(function (resolve, reject) {
                    var output = fs.createWriteStream(destinationPath, {'flags': 'a', 'bufferSize': 64 * 4096});
                    var input = fs.createReadStream(source.filePath, {
                        start: source.parameters.start,
                        end: source.parameters.end + 3
                    });
                    input.on('end', function () {
                        output.on('finish', function () {
                            resolve();
                        });
                    });
                    input.pipe(output);
                });
              });
            });

            promise.then(function() {
              // we're done here
            });

#1


1  

not sure if this has an impact, but did you try sequentially ? You create many write streams at the same time on the same file.

不确定这是否有影响,但你是否顺序尝试?您可以在同一文件上同时创建许多写入流。

For example:

             var destinationPath = something.bin;
             var promise = Promise.resolve();
             sources.forEach(function (source) {
               promise = promise.then(funtion() {
                 return new Promise(function (resolve, reject) {
                    var output = fs.createWriteStream(destinationPath, {'flags': 'a', 'bufferSize': 64 * 4096});
                    var input = fs.createReadStream(source.filePath, {
                        start: source.parameters.start,
                        end: source.parameters.end + 3
                    });
                    input.on('end', function () {
                        output.on('finish', function () {
                            resolve();
                        });
                    });
                    input.pipe(output);
                });
              });
            });

            promise.then(function() {
              // we're done here
            });