-
-
Notifications
You must be signed in to change notification settings - Fork 3
Opening and reading ASAR archive
To open an existing ASAR archive AsarArchive
class is used.
It provides functionality for reading header and files data that located in opened archive.
Here is an example of how to open an ASAR archive from file:
AsarArchive archive = new AsarArchive("/path/to/archive.asar");
This will open an ASAR archive from specified file. ASAR archive might have a folder named as *archive-name*.asar.unpacked
right beside the .asar file which will contain unpacked files. If this folder is missing and archive has unpacked files, library will fail to open any unpacked files within that folder.
There is also constructor that allows you to open ASAR archive from data stream, but it does not support ASAR archives that has unpacked files in them, so it is recommended to use first constructor with string if you are unfamiliar with archive contents.
Here is an example of how to open an ASAR archive from data stream:
FileStream fileStream = File.OpenRead("path/to/archive.asar"); // it could be any type of stream
AsarArchive archive = new AsarArchive(fileStream);
Since AsarArchive
class implements IDisposable
interface, you can use using
statement when opening archive.
using (AsarArchive archive = new AsarArchive("path/to/archive.asar"))
{
// read archive
}
You can use multiple ways to read files from archive, read whole file as an array of bytes or read file as a Stream
.
To read whole file as an array of bytes, you can use AsarArchive.ReadBytesAsync(string)
or AsarArchive.ReadBytesAsync(AsarArchiveFile)
First method accepts a string that contains a path to file within the archive (see it as *archive*/path/to/file.bin
, so the path will be /path/to/file.bin
). Second one accepts AsarArchiveFile
which can be found using AsarArchive.FindFile()
method.
Example of reading file as an array of bytes:
byte[] bytes1 = await archive.ReadBytesAsync("path/to/some/file.bin");
// or
AsarArchiveFile fileData = archive.FindFile("path/to/some/file.bin");
byte[] bytes2 = await archive.ReadBytesAsync(file);
You can also open file within archive as a data stream using AsarArchive.OpenFileAsStream(string)
method or AsarArchive.OpenFileAsStream(AsarArchiveFile)
method.
These methods work similar to methods for reading files as an array of bytes, but they are returning Stream
instead of an array of bytes.
Here is an example for opening file as stream:
Stream dataStream = archive.OpenFileAsStream("path/to/some/file.bin");
// or
AsarArchiveFile fileData = archive.FindFile("path/to/some/file.bin");
Stream dataStream = archive.OpenFileAsStream(file);
Made with ❤️ by craftersmine