The Simplest and Most Powerful Archive File Format Ever Made
.asar is a simple .tar-like archive format with impressive features.
When the file count is increasing, file management is a bit complex and time-consuming. Archive file formats are helping us to create a single file by merging multiple files and directories. Therefore, for users, they can make one file from a set of rarely accessed files. Whenever they need to access the files, they can extract them as typical files and directories. For developers, they can make a single file for storing resources rather than storing all resources separately on disk. That will make the particular software more portable and easy to share as well.
I found that the .asar archive format exactly matches my requirements. If you are looking for a simple but powerful archive format, .asar is indeed the option for you.
The entire structure is a piece of cake. .asar has the following internal format.
| UInt32: header_size | String: header | Bytes: file1 | ... | Bytes: file42 |
header_size is an 8-bit binary data segment that stores the size of the
header segment. The
header segment stores the directory tree of all stored files in JSON format. After that, it’s all about the content of the concatenated files like the .tar archive format.
The header’s JSON structure looks like shown below.
If the particular element is a directory, it has the
files attribute with its child contents. If there is a file, it will contain two properties:
offset for indexing, and
size attribute for the size of the file.
How to pack files
.asar has a great CLI. We can use the CLI to create archives from the directories we have. This packing process will make the header by analyzing the provided directory structure. Also, it will concatenate all files accordingly.
Packing files using the CLI.
$ asar pack <directory> <filename>
We can pack files programmatically as well with the help of its npm package.
How to unpack files
The CLI will also help you with the unpacking process. Since there is no hard compression, the process is pretty fast. This process will parse the header JSON, and for each node, it will create a file or a directory. Parsing is easy because we have the exact structure of the directories in the header. Also, we have the size and the offset of each file.
Unpacking files using the CLI.
$ asar extract <archive_filename> <directory>
We can do the same programmatically as well.
The .asar archive format looks like the .tar format internally. Because both .tar and .asar store file information in sequential order. Additionally, .asar supports random access to any file. In other words, if we have the file path, we can extract only the required file without touching other files. Random accessing is a helpful feature for application resource files because any resource file can be loaded into the physical memory instantly with good performance.
Importantly, this random accessing logic could be implemented by ourselves without even using a third-party library because .asar structure is undoubtedly simple. For example, I made the following C++ implementation for random accessing and resource handling.
.asar is a very flexible format. You will be able to extend the file attributes according to your requirements. For example, if you need to store created date of each file, you can modify the directory tree JSON by adding a new attribute.