How UnRAR Extracts Illegal Windows Path Characters

When extracting archives created on Unix-like operating systems, Windows users frequently encounter files containing characters not supported by the Windows file system. This article explains how the UnRAR utility identifies illegal path characters, the automated sanitization rules it applies to prevent file creation errors, and the methods users can employ to control or work around these naming restrictions during extraction.

The Problem: OS File Naming Discrepancies

POSIX-compliant systems (such as Linux and macOS) permit virtually any character in file names except for the null byte (\0) and the directory separator forward slash (/). Windows file systems (NTFS and FAT32), driven by the Win32 namespace, enforce much stricter naming conventions.

Windows forbids the following characters in file and directory names:

  • Less than (<)
  • Greater than (>)
  • Colon (:)
  • Double quote (")
  • Forward slash (/)
  • Backslash (\)
  • Vertical bar or pipe (|)
  • Question mark (?)
  • Asterisk (*)

Additionally, Windows disallows files ending with a period (.) or a space ( ), as well as reserved system device names such as CON, PRN, AUX, NUL, COM1 through COM9, and LPT1 through LPT9.

Automatic Character Sanitization

When running natively on Windows, the UnRAR utility checks every target path against Win32 file system rules before attempting to write data to disk. If an archive contains forbidden characters, UnRAR handles them through deterministic substitution:

  1. Character Replacement: UnRAR replaces forbidden characters (<, >, :, ", |, ?, *) with an underscore (_).
  2. Trailing Characters: If a filename or folder ends with a period or space, UnRAR typically strips the trailing character or converts it to an underscore to conform to standard Windows API expectations.
  3. Reserved Device Names: If a file matches a reserved DOS device name (like aux.txt or nul), UnRAR alters the name—frequently appending an underscore or counter—to ensure the file is not redirected to a physical or virtual system device.

This automatic sanitization allows the extraction process to continue without aborting or throwing unhandled OS-level I/O exceptions.

Handling Collision via Renaming Switches

Because replacing illegal characters with underscores can cause multiple distinct files to resolve to the same name (for example, file:1.txt and file?1.txt both becoming file_1.txt), naming collisions can occur.

UnRAR provides extraction switches to handle these collisions:

  • -or (Rename automatically): If an extracted file would overwrite an existing or previously extracted file due to sanitization, -or appends a unique numeric suffix (e.g., file_1(1).txt) to preserve both files.
  • -o+ and -o-: Force overwriting without prompting, or skip conflicting files entirely.

Path Sanitization and Directory Traversal Protection

Beyond visual naming characters, UnRAR actively sanitizes control characters and malicious path structures:

  • Directory Traversal: Sequences such as ../ or ..\ are stripped to prevent path traversal vulnerabilities, ensuring files cannot be extracted outside the designated destination directory.
  • Absolute Path Removal: Leading drive letters (such as C:\) or root slashes are stripped by default, converting absolute paths into relative paths within the working extraction folder.

Preserving Exact File Names on Windows

If exact preservation of illegal characters is mandatory (for instance, when inspecting Linux-targeted configuration files), standard Windows UnRAR cannot write them directly to standard Win32 namespaces. Workarounds include:

  1. Windows Subsystem for Linux (WSL): Running the Linux-native unrar binary inside a WSL environment. WSL leverages the NTFS POSIX namespace, allowing characters like colons or trailing dots to exist on an NTFS drive without substitution.
  2. Virtualization or Containerization: Extracting within a Linux virtual machine or Docker container prevents any Windows-specific sanitization from triggering.