Can aria2 Automatically Extract Archives After Downloading?

While aria2 is a highly efficient, lightweight, and multi-protocol command-line download utility, it does not have a built-in, native feature to automatically extract compressed archives like .zip, .tar.gz, or .rar files upon completion. However, aria2 provides a powerful event-hook system via the --on-download-complete option. This allows users to trigger custom external scripts that automatically handle the extraction process the moment a download finishes.


Understanding aria2’s Core Functionality

The primary design philosophy behind aria2 is to be an ultra-fast, resource-light downloader supporting HTTP(S), FTP, SFTP, BitTorrent, and Metalink. Because it focuses strictly on data transfer efficiency, features like file manipulation, archive extraction, or file renaming are intentionally excluded from the core binary.

To achieve automation, aria2 offloads post-processing tasks to the operating system’s shell using environment variables passed directly to a user-defined script.


How to Automate Extraction Using Scripts

To automatically extract archives, you must create a shell script (for Linux/macOS) or a batch file (for Windows) and instruct aria2 to execute it using the following command-line argument:

aria2c --on-download-complete=/path/to/script.sh "download_url"

When a download finishes, aria2 automatically executes the script and passes three arguments to it:

  1. GID: The unique download ID assigned by aria2.
  2. Number of files: The total count of files in the download.
  3. File path: The absolute path of the first downloaded file.

Example Automation Script (Linux/macOS)

Below is an example of a simple Bash script (extract.sh) that reads the file path provided by aria2, checks if the file is a compressed archive, and extracts it to the destination directory.

#!/bin/bash
# aria2 passes the file path as the third argument ($3)
FILE_PATH="$3"

# Extract based on file extension
case "$FILE_PATH" in
    *.zip)
        unzip -d "$(dirname "$FILE_PATH")" "$FILE_PATH"
        ;;
    *.tar.gz|*.tgz)
        tar -xzf "$FILE_PATH" -C "$(dirname "$FILE_PATH")"
        ;;
    *.tar.bz2|*.tbz2)
        tar -xjf "$FILE_PATH" -C "$(dirname "$FILE_PATH")"
        ;;
    *)
        echo "Downloaded file is not a supported archive or does not need extraction."
        ;;
esac

Before running aria2 with this script, ensure you grant execution permissions to the script using the command chmod +x extract.sh.


Making the Setup Permanent

If you do not want to type the --on-download-complete flag every time you download a file, you can add this setting permanently to your aria2 configuration file (aria2.conf).

Open your configuration file and add the following line:

on-download-complete=/path/to/extract.sh

With this configuration in place, aria2 will actively monitor every completed download and seamlessly run your extraction script in the background without requiring further manual intervention.