NodeJS 将二进制缓冲区写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12868089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:28:19  来源:igfitidea点击:

NodeJS write binary buffer into a file

node.jsbinarystreamwriter

提问by max246

I can't rewrite a file that I am getting from a binary buffer, I have checked with the original file and all bytes are the same.

我无法重写从二进制缓冲区获取的文件,我已经检查了原始文件并且所有字节都相同。

This is the file create from NodeJS:

这是从 NodeJS 创建的文件:

# hd test.txt  | head
00000000  47 49 46 38 39 61 32 00  32 00 f7 00 00 96 8c 73  |GIF89a2.2.?....s|
00000010  66 5e 45 c6 bb 9f 7b 72  5a 47 47 47 8a 81 65 ca  |f^E?.{rZGGG..e?|
00000020  c1 a6 c9 c1 ac ee ea dd  c8 c5 bc 8c 87 7a d3 c9  |??????????..z??|
00000030  ab 43 3b 26 eb e5 d1 fa  fa fa e5 e4 e2 a6 9d 83  |?C;&??????????..|
00000040  86 7e 67 c1 b4 8e e4 dc  c6 82 82 82 e1 dd d1 e3  |.~g??.???...????|
00000050  dd ca e4 da bc f5 f1 e6  26 25 25 9c 91 73 f8 f3  |???????&%%..s??|
00000060  e4 c3 b9 9d d3 ca b4 4a  42 2a d1 c6 a2 6c 62 46  |?ù.??JB*??lbF|
00000070  ea e6 db bb b3 9c db d3  bb 5c 54 3d f1 ee e6 dc  |????.??\T=????|
00000080  da d3 e7 e4 dc ce c2 9f  f8 f6 f2 76 6c 53 fc fb  |???????.???vlS??|
00000090  f9 e9 e1 ca 17 13 09 67  4d 00 f8 f4 e8 dc d3 b5  |????...gM.?????|

This the original one:

这是原来的:

$ hd runner_small.gif   | head
00000000  47 49 46 38 39 61 32 00  32 00 f7 00 00 96 8c 73  |GIF89a2.2......s|
00000010  66 5e 45 c6 bb 9f 7b 72  5a 47 47 47 8a 81 65 ca  |f^E...{rZGGG..e.|
00000020  c1 a6 c9 c1 ac ee ea dd  c8 c5 bc 8c 87 7a d3 c9  |.............z..|
00000030  ab 43 3b 26 eb e5 d1 fa  fa fa e5 e4 e2 a6 9d 83  |.C;&............|
00000040  86 7e 67 c1 b4 8e e4 dc  c6 82 82 82 e1 dd d1 e3  |.~g.............|
00000050  dd ca e4 da bc f5 f1 e6  26 25 25 9c 91 73 f8 f3  |........&%%..s..|
00000060  e4 c3 b9 9d d3 ca b4 4a  42 2a d1 c6 a2 6c 62 46  |.......JB*...lbF|
00000070  ea e6 db bb b3 9c db d3  bb 5c 54 3d f1 ee e6 dc  |.........\T=....|
00000080  da d3 e7 e4 dc ce c2 9f  f8 f6 f2 76 6c 53 fc fb  |...........vlS..|
00000090  f9 e9 e1 ca 17 13 09 67  4d 00 f8 f4 e8 dc d3 b5  |.......gM.......|

You can compare these two files and every bytes are the same, I am guessing that the encoding from NodeJS is not the right one.

你可以比较这两个文件,每个字节都是一样的,我猜来自 NodeJS 的编码是不正确的。

This is the piece of code

这是一段代码

var foo = "71%73%70%56%57%97%50%0%50%0%247%0%0%150%140%115%102%94%69%198%187%159%123%114%90%71%71%71%138%129%101%202%193%166%201%193%172%238%234%221%200%197%188%140$
var bytes = foo.split("%");

var b = new Buffer(bytes.length);
for (var i = 0;i < bytes.length;i++) {
        b[i] = bytes[i];
}

fs.writeFile("test.txt", b,  "binary",function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
});

You can try to run it on your NodeJS and see that the result is wrong.

你可以尝试在你的 NodeJS 上运行它,发现结果是错误的。

What can I do to fix it?

我能做些什么来修复它?

回答by Mohamed Alimam

I am not sure if this would help but try to change the b variable to the bytes variable in the line below at least you would be able to view the file in a test editor

我不确定这是否有帮助,但尝试将 b 变量更改为下面一行中的 bytes 变量,至少您可以在测试编辑器中查看该文件

fs.writeFile("test.txt", b,  "binary",function(err) { });

If you would like to have the numbers space separated try the code below:

如果您想将数字空间分开,请尝试以下代码:

var fs = require('fs');

var foo = "71%73%70%56%57%97%50%0%50%0%247%0%0%150%140%115%102%94%69%198%187%159%123%114%90%71%71%71%138%129%101%202%193%166%201%193%172%238%234%221%200%197%188%140"
var bytes = foo.split("%");

var b = new Buffer(bytes.length);
var c = "";
for (var i = 0;i < bytes.length;i++) {
    b[i] = bytes[i];
    c = c + " " + bytes[i]
}

fs.writeFile("test.txt", c,  "binary",function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
});

回答by Pedro Rodrigues

You can try this:

你可以试试这个:

var writeFileSync = function (path, buffer, permission) {
    permission = permission || 438; // 0666
    var fileDescriptor;

    try {
        fileDescriptor = fs.openSync(path, 'w', permission);
    } catch (e) {
        fs.chmodSync(path, permission);
        fileDescriptor = fs.openSync(path, 'w', permission);
    }

    if (fileDescriptor) {
        fs.writeSync(fileDescriptor, buffer, 0, buffer.length, 0);
        fs.closeSync(fileDescriptor);
    }
}

// then writeFileSync('path_to_your_file', your_buffer);