# The Most Efficient and Versatile Archive Format You Can Find
Written on
Chapter 1: Understanding the .asar Archive Format
The .asar format is a straightforward yet powerful archive type, reminiscent of the .tar format, that boasts a range of impressive capabilities.
As the number of files grows, managing them can become complicated and time-consuming. Archive formats help by consolidating multiple files and directories into a single file. This is beneficial for users who want to combine seldom-used files into one accessible format. When needed, they can easily extract these files and directories. For developers, having a single file to store resources, instead of numerous individual files on disk, enhances portability and facilitates sharing.
Recently, I was in search of an archive format suitable for storing application resources, which primarily consisted of JavaScript files and graphics. My criteria were strict: I needed random access capability, the ability to extract data without relying on external libraries, and an uncompressed structure.
To my delight, I discovered that the .asar archive format met all my requirements perfectly. If you’re in need of a simple yet robust archive format, .asar is an excellent choice.
Chapter 2: The Inner Workings of .asar
The structure of .asar is remarkably simple. It is organized as follows:
Copy| UInt32: header_size | String: header | Bytes: file1 | ... | Bytes: file42 |
Here, header_size is a binary data segment that indicates the size of the header section. The header itself contains a JSON representation of the directory tree encompassing all stored files. Following this, the content consists of the concatenated files, similar to the .tar format.
The JSON structure of the header appears as follows: if an element is a directory, it will have a files attribute that lists its child contents. If it's a file, it will include two properties: offset for indexing and size to denote the file's size.
#### How to Package Files
The .asar format features an efficient Command Line Interface (CLI) that allows for easy creation of archives from specified directories. During this packaging process, the CLI analyzes the directory structure to generate the header and concatenate the files as needed.
To pack files using the CLI, you would use the command:
$ asar pack
You can also automate this process using the npm package.
#### How to Unpack Files
The CLI simplifies the unpacking process as well. Since .asar does not employ hard compression, this operation is quite rapid. The CLI parses the header JSON, creating files or directories for each node. This is straightforward due to the clear structure of the directories outlined in the header, along with the size and offset of each file.
To extract files using the CLI, you would enter:
$ asar extract
Similar programmatic extraction is also available.
Chapter 3: Random Access in .asar
The internal structure of the .asar format resembles that of .tar, as both maintain file information in a sequential manner. However, .asar also enables random access to any file within the archive. This means that if you have a specific file path, you can extract just that file without needing to interact with the others. This feature is particularly advantageous for application resource files, allowing for quick loading into physical memory with optimal performance.
Crucially, you can implement this random access logic yourself without the need for third-party libraries, thanks to the straightforward nature of the .asar structure. For instance, I developed a C++ implementation for managing random access and resource handling.
The .asar format is quite adaptable, allowing you to customize file attributes to meet your needs. For instance, if you wish to include the creation date for each file, you can easily modify the directory tree JSON by adding a new attribute.
This flexibility and efficiency make .asar an ideal choice for developers and users alike looking for a reliable archive format.