### Registering and Using AsarURLs Source: https://context7.com/anatawa12/asar4j/llms.txt Configures the asar: protocol handler and demonstrates creating URLs for files and nested archives. ```java import com.anatawa12.asar4j.AsarURLs; import java.net.URL; import java.io.InputStream; // Option 1: Register via system property (preferred, requires system classloader) AsarURLs.registerToSystemProp(); // Option 2: Get package name for manual protocol registration String pkg = AsarURLs.getProtocolHandlerPackage(); System.setProperty("java.protocol.handler.pkgs", pkg); // Option 3: Use URLStreamHandlerFactory (application-wide, can only set once) URL.setURLStreamHandlerFactory(AsarURLs.factory); // Create ASAR URLs programmatically URL asarFileUrl = new URL("file:/path/to/app.asar"); URL entryUrl = AsarURLs.createUrl(asarFileUrl, "src/index.js"); // Result: asar:file:/path/to/app.asar!/src/index.js // Read content via URL try (InputStream is = entryUrl.openStream()) { byte[] content = is.readAllBytes(); System.out.println(new String(content, StandardCharsets.UTF_8)); } // Contextual URL resolution URL rootUrl = new URL("asar:file:/app.asar!/"); URL relative = new URL(rootUrl, "src/main.js"); // asar:file:/app.asar!/src/main.js // Nested archive access (ASAR inside JAR) URL nestedUrl = AsarURLs.createUrl( new URL("jar:file:/bundle.zip!/resources/app.asar"), "index.js" ); // Result: asar:jar:file:/bundle.zip%21/resources/app.asar!/index.js ``` -------------------------------- ### Traversing Directory Entries Source: https://context7.com/anatawa12/asar4j/llms.txt Demonstrates how to navigate the archive structure using AsarFile and AsarEntry to access children. ```java import com.anatawa12.asar4j.AsarFile; import com.anatawa12.asar4j.AsarEntry; import java.util.Collection; try (AsarFile asar = new AsarFile("archive.asar")) { // Get root directory (empty path returns root) AsarEntry root = asar.getEntry(""); if (root.getType() == AsarEntryType.DIRECTORY) { // Get all direct children Collection children = root.getChildren(); for (AsarEntry child : children) { System.out.println(child.getBasename() + " -> " + child.getType()); } // Get specific child by name AsarEntry srcDir = root.getChild("src"); if (srcDir != null && srcDir.getType() == AsarEntryType.DIRECTORY) { AsarEntry mainJs = srcDir.getChild("main.js"); if (mainJs != null) { System.out.println("Found: " + mainJs.getName()); } } } } ``` -------------------------------- ### Create ASAR Archives with AsarOutputStream Source: https://context7.com/anatawa12/asar4j/llms.txt AsarOutputStream allows creating ASAR archives similar to ZipOutputStream. Use putNextEntry, write data, and closeEntry for each file. Directories and symbolic links can also be added. ```java import com.anatawa12.asar4j.AsarOutputStream; import com.anatawa12.asar4j.AsarEntry; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; try (AsarOutputStream asar = new AsarOutputStream(new FileOutputStream("output.asar"))) { // Write a text file AsarEntry textFile = new AsarEntry("/src/main.js"); asar.putNextEntry(textFile); asar.write("console.log('Hello ASAR!');\n".getBytes(StandardCharsets.UTF_8)); asar.closeEntry(); // Write an executable file AsarEntry execFile = new AsarEntry("/bin/run.sh"); execFile.setExecutable(true); asar.putNextEntry(execFile); asar.write("#!/bin/bash\necho 'Running...'\n".getBytes(StandardCharsets.UTF_8)); asar.closeEntry(); // Write binary data AsarEntry binFile = new AsarEntry("/data/image.png"); asar.putNextEntry(binFile); byte[] imageData = Files.readAllBytes(Path.of("source-image.png")); asar.write(imageData); asar.closeEntry(); // Create empty directory explicitly (directories are auto-created for files) asar.addDirectory("/empty-dir"); // Create symbolic link (absolute path from root) asar.addLink("/src/index.js", "/src/main.js"); // Create symbolic link (relative path) asar.addLink("/lib/utils.js", "../src/utils.js"); } // close() automatically calls finish() ``` -------------------------------- ### AsarOutputStream - Archive Writer Source: https://context7.com/anatawa12/asar4j/llms.txt The `AsarOutputStream` class provides `ZipOutputStream`-like functionality for creating ASAR archives. It supports writing files, creating directories, and adding symbolic links. File entries are added using `putNextEntry()` followed by writing data, then `closeEntry()`. ```APIDOC ## AsarOutputStream - Archive Writer ### Description Enables the creation of ASAR archives with functionality similar to `ZipOutputStream`. Supports writing files, directories, and symbolic links. ### Method `new AsarOutputStream(OutputStream out)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```java import com.anatawa12.asar4j.AsarOutputStream; import com.anatawa12.asar4j.AsarEntry; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; try (AsarOutputStream asar = new AsarOutputStream(new FileOutputStream("output.asar"))) { // Write a text file AsarEntry textFile = new AsarEntry("/src/main.js"); asar.putNextEntry(textFile); asar.write("console.log('Hello ASAR!');\n".getBytes(StandardCharsets.UTF_8)); asar.closeEntry(); // Write an executable file AsarEntry execFile = new AsarEntry("/bin/run.sh"); execFile.setExecutable(true); asar.putNextEntry(execFile); asar.write("#!/bin/bash\necho 'Running...'\n".getBytes(StandardCharsets.UTF_8)); asar.closeEntry(); // Write binary data AsarEntry binFile = new AsarEntry("/data/image.png"); asar.putNextEntry(binFile); byte[] imageData = Files.readAllBytes(Path.of("source-image.png")); asar.write(imageData); asar.closeEntry(); // Create empty directory explicitly (directories are auto-created for files) asar.addDirectory("/empty-dir"); // Create symbolic link (absolute path from root) asar.addLink("/src/index.js", "/src/main.js"); // Create symbolic link (relative path) asar.addLink("/lib/utils.js", "../src/utils.js"); } // close() automatically calls finish() ``` ### Response #### Success Response (200) - **void** - The archive is written to the provided output stream. ``` -------------------------------- ### Gradle Dependency for ASAR4j Source: https://github.com/anatawa12/asar4j/blob/master/README.md Add this dependency to your Gradle project to include ASAR4j. Replace '' with the specific module (e.g., 'file', 'writer', 'url') and '' with the desired library version. ```kotlin dependencies { implementation("com.anatawa12.asar4j::") } ``` -------------------------------- ### Reading ASAR Archives Source: https://context7.com/anatawa12/asar4j/llms.txt AsarFile provides methods to access, read, and iterate through entries in an ASAR archive. ```java import com.anatawa12.asar4j.AsarFile; import com.anatawa12.asar4j.AsarEntry; import java.io.InputStream; import java.nio.charset.StandardCharsets; // Open an ASAR file (multiple constructor options) try (AsarFile asar = new AsarFile("/path/to/archive.asar")) { // Get a specific entry by path AsarEntry entry = asar.getEntry("/src/index.js"); // Read file contents as InputStream if (entry != null && entry.getType() == AsarEntryType.FILE) { try (InputStream is = asar.getInputStream(entry)) { byte[] content = is.readAllBytes(); String text = new String(content, StandardCharsets.UTF_8); System.out.println(text); } } // Get entry without resolving symbolic links AsarEntry linkEntry = asar.getEntry("/src/link.js", false); if (linkEntry.getType() == AsarEntryType.LINK) { String target = linkEntry.getLinkTarget(); // e.g., "../other/file.js" } // Iterate through all entries for (AsarEntry e : asar.iterable()) { System.out.println(e.getName() + " [" + e.getType() + "]"); } // Stream API for functional processing asar.stream() .filter(e -> e.getType() == AsarEntryType.FILE) .filter(e -> e.getName().endsWith(".js")) .forEach(e -> System.out.println("JS file: " + e.getName())); // Get total entry count int count = asar.size(); } ``` -------------------------------- ### Maven Dependency for ASAR4j Source: https://github.com/anatawa12/asar4j/blob/master/README.md Add this dependency to your Maven project to include ASAR4j. Replace '[choose from file, writer or url]' with the specific module you need and '[version]' with the desired library version. ```xml com.anatawa12.asar4j [choose from file, writer or url] [version] ``` -------------------------------- ### Access ASAR File Entries with SeekableByteChannel Source: https://context7.com/anatawa12/asar4j/llms.txt Use getChannel to obtain a SeekableByteChannel for random access to ASAR file entry contents. This is efficient for large files or when seeking is required. Ensure to handle potential IOExceptions. ```java import com.anatawa12.asar4j.AsarFile; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; try (AsarFile asar = new AsarFile("/path/to/archive.asar")) { AsarEntry entry = asar.getEntry("/data/large-file.bin"); try (SeekableByteChannel channel = asar.getChannel(entry)) { // Get file size long size = channel.size(); // Read specific portion of file ByteBuffer buffer = ByteBuffer.allocate(1024); channel.position(100); // Seek to position 100 int bytesRead = channel.read(buffer); buffer.flip(); // Process buffer content byte[] data = new byte[bytesRead]; buffer.get(data); } } ``` -------------------------------- ### AsarFile Class Source: https://context7.com/anatawa12/asar4j/llms.txt Provides functionality for reading ASAR archives, similar to ZipFile, supporting file paths and seekable byte channels. ```APIDOC ## AsarFile ### Description Provides ZipFile-like functionality for reading ASAR archives. Implements Closeable for resource management. ### Methods - **getEntry(String path)**: Retrieves an entry by path. - **getEntry(String path, boolean resolveLinks)**: Retrieves an entry, optionally resolving symbolic links. - **getInputStream(AsarEntry entry)**: Returns an InputStream for the file content. - **iterable()**: Returns an iterable of all entries. - **stream()**: Returns a Stream of all entries for functional processing. - **size()**: Returns the total count of entries in the archive. ``` -------------------------------- ### Representing ASAR File Entries Source: https://context7.com/anatawa12/asar4j/llms.txt Use AsarEntry to manage metadata and path normalization for files within an archive. ```java import com.anatawa12.asar4j.AsarEntry; import com.anatawa12.asar4j.AsarEntryType; // Create a new file entry AsarEntry entry = new AsarEntry("/path/to/file.txt"); // Get entry properties String fullName = entry.getName(); // Returns: "/path/to/file.txt" String baseName = entry.getBasename(); // Returns: "file.txt" AsarEntryType type = entry.getType(); // Returns: AsarEntryType.FILE int size = entry.getSize(); // Returns file size or -1 if not set // Check and set executable flag boolean isExec = entry.isExecutable(); entry.setExecutable(true); // Normalize arbitrary paths to standard format String normalized = AsarEntry.normalizeName("path\\to//file.txt"); // Returns: "/path/to/file.txt" ``` -------------------------------- ### AsarFile.getChannel - SeekableByteChannel Access Source: https://context7.com/anatawa12/asar4j/llms.txt The `getChannel` method returns a `SeekableByteChannel` for reading file entry contents, enabling efficient random access operations. This is useful when working with large files or when you need to seek within the file content. ```APIDOC ## AsarFile.getChannel - SeekableByteChannel Access ### Description Provides a `SeekableByteChannel` for reading file entry contents, allowing for efficient random access operations within ASAR archives. ### Method `getAsarFile.getChannel(AsarEntry entry)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```java import com.anatawa12.asar4j.AsarFile; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; try (AsarFile asar = new AsarFile("/path/to/archive.asar")) { AsarEntry entry = asar.getEntry("/data/large-file.bin"); try (SeekableByteChannel channel = asar.getChannel(entry)) { // Get file size long size = channel.size(); // Read specific portion of file ByteBuffer buffer = ByteBuffer.allocate(1024); channel.position(100); // Seek to position 100 int bytesRead = channel.read(buffer); buffer.flip(); // Process buffer content byte[] data = new byte[bytesRead]; buffer.get(data); } } ``` ### Response #### Success Response (200) - **SeekableByteChannel** (SeekableByteChannel) - A channel for reading the specified file entry. ``` -------------------------------- ### AsarEntry Class Source: https://context7.com/anatawa12/asar4j/llms.txt Represents a file entry within an ASAR archive, providing methods to manage metadata such as names, sizes, and executable flags. ```APIDOC ## AsarEntry ### Description Represents a file entry within an ASAR archive. Stores metadata including entry name, size, offset, and executable flag. ### Methods - **getName()**: Returns the normalized entry name. - **getBasename()**: Returns the base name of the entry. - **getType()**: Returns the AsarEntryType of the entry. - **getSize()**: Returns the file size. - **isExecutable()**: Checks if the executable flag is set. - **setExecutable(boolean)**: Sets the executable flag. - **normalizeName(String)**: Static method to normalize arbitrary paths to the standard ASAR format. ``` -------------------------------- ### AsarOutputStream.finish - Complete Archive Without Closing Stream Source: https://context7.com/anatawa12/asar4j/llms.txt The `finish` method writes the ASAR header and finalizes the archive without closing the underlying output stream. This is useful when writing to streams that should remain open for further use. ```APIDOC ## AsarOutputStream.finish - Complete Archive Without Closing Stream ### Description Finalizes the ASAR archive by writing the header, without closing the underlying output stream. Useful for streams that need to remain open. ### Method `finish()` ### Parameters None ### Request Example ```java import com.anatawa12.asar4j.AsarOutputStream; import java.io.ByteArrayOutputStream; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); AsarOutputStream asar = new AsarOutputStream(buffer); // Add entries asar.putNextEntry(new AsarEntry("/config.json")); asar.write("{\"version\": 1}".getBytes(StandardCharsets.UTF_8)); asar.closeEntry(); // Finalize archive but keep buffer stream open asar.finish(); // Get the complete ASAR archive bytes byte[] asarBytes = buffer.toByteArray(); // Buffer stream can still be used buffer.close(); // Close manually when done ``` ### Response #### Success Response (200) - **void** - The ASAR header is written to the stream. ``` -------------------------------- ### Handling ASAR Entry Types Source: https://context7.com/anatawa12/asar4j/llms.txt AsarEntryType defines the classification of entries, which is essential for conditional processing logic. ```java import com.anatawa12.asar4j.AsarEntryType; // Entry types available AsarEntryType.DIRECTORY // Directory containing child entries AsarEntryType.FILE // Regular file with content AsarEntryType.LINK // Symbolic link pointing to another entry // Usage in entry processing switch (entry.getType()) { case DIRECTORY: // Process directory - get children with entry.getChildren() break; case FILE: // Process file - read content via AsarFile.getInputStream(entry) break; case LINK: // Process link - get target with entry.getLinkTarget() break; } ``` -------------------------------- ### AsarEntryType Enumeration Source: https://context7.com/anatawa12/asar4j/llms.txt Defines the types of entries that can exist within an ASAR archive. ```APIDOC ## AsarEntryType ### Description Enum defining the three types of entries in an ASAR archive. ### Types - **DIRECTORY**: Directory containing child entries. - **FILE**: Regular file with content. - **LINK**: Symbolic link pointing to another entry. ``` -------------------------------- ### Finalize ASAR Archive Without Closing Stream Source: https://context7.com/anatawa12/asar4j/llms.txt The finish() method finalizes the ASAR archive by writing the header but does not close the underlying output stream. This is useful when the stream needs to be kept open for other operations. ```java import com.anatawa12.asar4j.AsarOutputStream; import java.io.ByteArrayOutputStream; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); AsarOutputStream asar = new AsarOutputStream(buffer); // Add entries asar.putNextEntry(new AsarEntry("/config.json")); asar.write("{\"version\": 1}".getBytes(StandardCharsets.UTF_8)); asar.closeEntry(); // Finalize archive but keep buffer stream open asar.finish(); // Get the complete ASAR archive bytes byte[] asarBytes = buffer.toByteArray(); // Buffer stream can still be used buffer.close(); // Close manually when done ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.