Skip to content

Creating new ASAR archives

Rodion Morozov edited this page Feb 20, 2023 · 2 revisions

To create a new ASAR archive, AsarArchivePacker class is used. It accepts a AsarArchivePackerData object which contains files and directories to pack in ASAR archive and other settings for packer.

Creating data for archive

To create AsarArchivePackerData object, AsarArchivePackerDataBuilder is used.

It contains methods such as AddFile, AddDirectory, etc.

To create AsarArchivePackerDataBuilder, AsarArchivePackerDataBuilder.CreateBuilder(string,string) method is used.

Here is an example of how AsarArchivePackerDataBuilder is created:

AsarArchivePackerDataBuilder builder = AsarArchivePackerDataBuilder.CreateBuilder("path/to/archive/output/dir", "archive-name-without-extension");

After builder has created, you can add files to the archive using this builder.

You need to keep in mind, that all Add methods are relative to archive root, so method AddFile will add file at the root of an archive, if you want to create a directory structure, use AddDirectory to add a directory of files to the archive. AddDirectory will add a provided directory as a directory in an archive root, so directory like C:\some_dir\dir will become asar_arc.asar\dir. All subdirectories and subfiles will be also added to this directory.

Here is an example of how to add files and directories to archive:

builder.AddFile("path/to/file.bin");
builder.AddFile("path/to/second-file.txt");
builder.AddDirectory("path/to/dir");

If you need to sort files before pack, call builder.PerformSort(true) method on the builder you've created.

Packing archive to file

To pack files into ASAR archive call AsarArchivePacker.PackAsync() method.

It will pack all pending files into archive in output directory with specified name

Here is an example of how to pack files into archive:

AsarArchivePacker packer = new AsarArchivePacker(builder.CreateArchiveData());
await packer.PackAsync();
// will create archive at "path/to/archive/output/dir/archive-name-without-extension.asar"
// unpacked files will be located at "path/to/archive/output/dir/archive-name-without-extension.asar.unpacked"

Packer class has two events that can be occurred when unpacking whole archive.

Occurs when archive packing status changed (current unpacking file changed, etc.)

Occurs when archive packing completed successfully. It also opens packed ASAR archive if event has any subscribers.

Chaining method calls

You can also chain method calls like this:

AsarArchivePackerData data = AsarArchivePackerDataBuilder.CreateBuilder("path/to/archive/output/dir", "archive-name-without-extension")
    .AddFile("path/to/file.bin")
    .AddFile("path/to/second-file.txt")
    .AddDirectory("path/to/dir")
    .PerformSort(true)
    .CreateArchiveData();