Extract First 100 Bytes of a File Using Unrar

This article explains whether the unrar command-line utility can extract only the first 100 bytes of a compressed file from a RAR archive. While unrar does not have a native parameter to specify byte ranges directly, you can achieve this exact result by piping the decompressed stream to standard output and truncating it using standard operating system utilities.

The Direct Answer

No, the unrar utility cannot natively limit extraction to a specific byte count using built-in flags. However, you can extract the first 100 bytes without saving the entire file to disk by streaming the output to standard output (stdout) and piping it to a tool like head.

The Command Solution (Linux / macOS)

To extract the first 100 bytes of a specific file inside an archive, run the following command:

unrar p -inul archive.rar "path/to/file.ext" | head -c 100 > output.bin

How It Works:

  • p: Instructs unrar to print the contents of the specified file directly to standard output instead of writing it to disk.
  • -inul: Suppresses all diagnostic, banner, and informational messages. This ensures that header text does not corrupt the raw byte stream.
  • head -c 100: Reads only the first 100 bytes from the input stream and immediately closes the pipe.
  • > output.bin: Redirects the resulting 100 bytes into a destination file.

Efficiency and Resource Handling

Because RAR uses stream-based compression algorithms, decompression must always begin from the start of the compressed file stream. When using head -c 100, the pipe closes as soon as the 100th byte is read. This sends a SIGPIPE signal to unrar, causing it to terminate immediately. As a result, your system does not waste CPU cycles decompressing the remainder of a potentially multi-gigabyte file.

Note that if the archive was created as a "solid" archive, unrar may still need to read and decode the files preceding your target file in the archive chain to initialize the decompression dictionary.

Windows Solution (PowerShell)

On Windows systems using PowerShell, you can achieve the same result by streaming the output through .NET byte handling:

$process = Start-Process -FilePath "unrar" -ArgumentList "p -inul archive.rar target.ext" -NoNewWindow -RedirectStandardOutput "temp.bin" -PassThru
$bytes = [System.IO.File]::ReadAllBytes("temp.bin") | Select-Object -First 100
[System.IO.File]::WriteAllBytes("output.bin", $bytes)
Remove-Item "temp.bin"

If you have Unix-like utilities installed on Windows (such as Git Bash or WSL), the Linux head -c 100 piping method works natively and remains the fastest approach.