[Encoding] Binary Mode

Have you ever tried writing a software to read a zip file and send it over the server, only for the server to receive something different?

If you do, one of the possible reasons is you are not reading your zip file correctly. A beginner mistake is to read a zip file as if you are reading a text file. 

Remember, text, in the computer world, are bytes interpreted by a specific type of encoding. If you program your software to read your zip file as if it is reading a text file, you might end up using the wrong encoding, and thus, be unable to interpret your zip file later on.

Personal Experience

I got this problem when I was trying to add a feature where you download zip from the server via my web site. My server and webapp were coded in Ruby, so I'm going to use Ruby syntax.

At first, I used File.read(...) to read my zip file. Then I do an encode64 because my webapp crashes when it sees unreadable characters. In my webapp, I then decode64 (since I encode64-ed it in the server), and then send the zip file to the user to download.

In my log files, I seemed that the server's zip data and webapp's zip data were identical. But for some reason, my downloaded zip is corrupted.

In the end, my solution was to use File.open(..., 'rb'). Apparently reading it in binary mode is different from reading as text.... this prompted me to do more research in binary mode encoding.


Binary Mode

According to yourdictionary.com, binary mode means:
(1) A mode of operation that deals with non-textual data. When a "binary" parameter is added to a command, it enables every type of data to be transferred or compared rather than just ASCII text.
(2) A compiler mode that deals with file I/O. It allows programmers to manipulate data files at the byte level rather than at the field level.

This makes sense. With my limited knowledge, it seems that even though the zip data were textual-identical in my webapp and server's zip data (when doing File.read), it might not have been binary-identical. 




Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS