0

I am trying to send over HTTP a ZIP file, to achieve that I encode/decode it in Base64. That is not working unfortunately. I have figured out the issue is actually in the encode/decode itself and was able to isolate and reproduce it.

Consider a simple code which:

  1. Reads a file from the filesystem.
  2. Base64 encodes that file.
  3. Base64 decodes the previously computed base64 string into a binary stream and save it into another file (same identical as original).
const fs = require("fs");

const buffer = fs.readFileSync("C:/users/Public/myzip.zip"); // 1. read
const base64data = buffer.toString("base64");                // 2. encode

fs.writeFileSync("C:/users/Public/myzip2.zip",
                 new Buffer(base64data, "base64"),
                 "base64");                                  // 3. decode + save

The code runs fine (I am on Windows 10), no errors. It successfully reads and writes files. However, file myzip2.zip is written but it cannot be opened: Windows complains it is invalid :(


A bit more context

The reason for this question is the following. I am using Base64 encoding in order to successfully send over a ZIP file from a client to a server.

This code isolates the problem I am having by leaving the networking complexity out of the equation. I need to figure out how to properly encode/decode a file using Base64. Once I can make it work on a single machine, it will work when sending the file.

Why is this basic set of commands not working?

Andry
  • 16,172
  • 27
  • 138
  • 246
  • Aren't the slashes backwards with Windows ? – clota974 Mar 29 '20 at 21:07
  • 1
    Node can handle them, the code runs fine, reads and writes the streams correctly – Andry Mar 29 '20 at 21:09
  • why not: `fs.createReadStream('test.log').pipe(fs.createWriteStream('newLog.log'));` – Get Off My Lawn Mar 29 '20 at 21:16
  • Does this answer your question? [Fastest way to copy file in node.js](https://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js) – Get Off My Lawn Mar 29 '20 at 21:21
  • Please see the updated question – Andry Mar 29 '20 at 21:23
  • Do not vote for close because you think this is about copying... this is not about it (see the title of the question). Why is this basic encode/decode code not working? – Andry Mar 29 '20 at 21:24
  • I tested with two small zip files and got a binary identical results. How large are your files and what are the differences when you do a binary diff? – jps Mar 29 '20 at 21:29
  • @jps: The zip is very very small (1KB). I am on Windows10. Which OS are you on? – Andry Mar 29 '20 at 21:30
  • Also, use `Buffer.from(base64data, "base64")`, not `new Buffer`. – Aplet123 Mar 29 '20 at 21:30
  • @jps How to make a binary diff? – Andry Mar 29 '20 at 21:31
  • @Andry: I also use Windows 10 and Node.js 12.13.1, I also tested two very small files 115 bytes and 715 bytes. I used an online diff tool: https://www.diffnow.com – jps Mar 29 '20 at 21:33
  • @jps Oh man I don't get this :( The diff tool you suggested sees them as identical!!! It was even able to open the generated zip I cannot open locally!!!! And the content is correct. Why can't I open the generated zip on my OS? – Andry Mar 29 '20 at 21:36
  • @jps Have you actually tried to open the zip file on Windows and see the content? Or did you just use the diff tool straight? – Andry Mar 29 '20 at 21:37
  • @Andry: I created the myfile.zip directly with Windows Explorer, context menu -> send to / zip compressed folder and could also open it again. Even worked with a large jpg photo. – jps Mar 29 '20 at 21:40
  • @jps And when you ran the code and generated `myzip2.zip`, did you try to open this file as well using explorer? – Andry Mar 29 '20 at 21:41
  • @Andry, yes, always checked myfile2.zip and had no problem. So whatever the problem is, it's certainly more on windows side and not node.js. If you can't open the copy (myfile2.zip) you probably also can't open myfile.zip (as they are identical)? – jps Mar 29 '20 at 21:44
  • @jps That is the problem... I can open `myzip.zip`, I cannot open `myzip2.zip` :( But the diff tool you gave me can actually see its content and I have a feeling that 7zip can open it too... Seems like this is a problem that sometimes users bump into: https://superuser.com/questions/1107853/zip-archives-not-opening-in-explorer-on-some-windows-10-machines – Andry Mar 29 '20 at 21:45

0 Answers0