1

I'm trying to save an array of uint8arrays in an file using node JS, does anyone know how can I do that using fs? Something like that:

[ Uint8Array(162) [ 133, 111, 74, 131, 187, 35, 69, 135, 1, 151, 1, 1, 198, 69, 229, 121, 168, 106, 59, 104, 91, 198, 115, 218, 79, 67, 102, 212, 204, 206, 145, 126, 152, 59, 147, 114, 133, 173, 119, 91, 220, 251, 174, 57, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 2, 2, 251, 255, 149, 134, 6, 9, 65, 100, 100, 32, 99, 97, 114, 100, 50, 0, 9, 1, 2, 2, 4, 19, 4, 21, 14, 52, 3, 66, 4, 86, 5, 87, 26, 112, 2, 3, 0, ... 62 more items ], Uint8Array(170) [ 133, 111, 74, 131, 67, 31, 18, 227, 1, 159, 1, 1, 187, 35, 69, 135, 0, 7, 102, 52, 237, 242, 169, 192, 199, 237, 238, 142, 226, 200, 98, 129, 144, 184, 181, 198, 33, 248, 228, 223, 158, 171, 250, 192, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 3, 5, 251, 255, 149, 134, 6, 8, 65, 100, 100, 32, 99, 97, 114, 100, 0, 10, 1, 2, 2, 4, 17, 4, 19, 4, 21, 14, 52, 3, 66, 4, 86, 5, 87, 29, 112, 2, 3, ... 70 more items ], Uint8Array(120) [ 133, 111, 74, 131, 71, 17, 199, 138, 1, 110, 1, 67, 31, 18, 227, 131, 113, 252, 222, 71, 172, 34, 205, 40, 96, 11, 236, 242, 153, 43, 182, 4, 136, 135, 36, 6, 79, 52, 223, 123, 188, 42, 7, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 4, 8, 251, 255, 149, 134, 6, 11, 68, 101, 108, 101, 116, 101, 32, 99, 97, 114, 100, 0, 10, 1, 2, 2, 2, 17, 2, 19, 2, 52, 1, 66, 2, 86, 2, 112, 2, 113, 2, 115, ... 20 more items ], Uint8Array(126) [ 133, 111, 74, 131, 51, 54, 68, 129, 1, 116, 1, 71, 17, 199, 138, 51, 167, 78, 185, 254, 251, 73, 245, 53, 153, 253, 146, 203, 139, 250, 206, 105, 223, 51, 227, 156, 55, 3, 146, 177, 243, 235, 140, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 5, 9, 251, 255, 149, 134, 6, 17, 77, 97, 114, 107, 32, 99, 97, 114, 100, 32, 97, 115, 32, 100, 111, 110, 101, 0, 9, 1, 2, 2, 2, 21, 6, 52, 1, 66, 2, 86, 2, 112, ... 26 more items ] ]

Fbosisio
  • 11
  • 1
  • 1
    Have you tried serializing this to JSON? – Sebastian Simon Jun 13 '21 at 04:18
  • 1
    Do you want to save it as binary data or JSON or what format? If you're not going to use JSON, you have to invent your own file format that allows you to know where each of the nested arrays starts and stops. – jfriend00 Jun 13 '21 at 07:25
  • I just want to save this data in disk and restore it with the same format… I’ve tried do that with Json but when I’ve tried to restore the format doesn’t match with the original… maybe I’m doing something wrong – Fbosisio Jun 14 '21 at 11:56

1 Answers1

1

I am simply using the following, for exporting into docx file:

buffer = Uint8Array(86762) [
   80,  75,   3,   4,  10,   0,   0,   0,   8,   0,  79,  72,
  111,  85, 212,  85, 145,  41, 234,   1,   0,   0,  43,  11,
    0,   0,  19,   0,   0,   0,  91,  67, 111, 110, 116, 101,
  110, 116,  95,  84, 121, 112, 101, 115,  93,  46, 120, 109,
  108, 197, 150, 205,  78, 227,  48,  20, 133, 247,  60, 133,
  229,  45, 106,  92,  64,  26, 141,  80,  83,  22, 252,  44,
   25, 164, 233,  60, 128,  27, 223, 164, 134, 248,  71, 182,
   91, 232, 219, 207, 117,  66,  67, 169,  18, 198, 154,  54,
   98,  83,  41, 182,
  ... 86662 more items
]
fs.writeFileSync('output.docx', buffer)

I suppose restoring it will be just fs.readFileSync(pathToFile)

Vitomir
  • 335
  • 2
  • 15