How Unrar Handles Sparse Files on Linux

This article explains how the unrar utility handles the extraction of sparse files on Linux systems. It covers how the RAR format stores sparse file metadata, the filesystem-level mechanisms Linux uses during decompression, the behavior differences between various versions of the tool, and how to verify that disk space was saved correctly.

RAR Support for Sparse Files

A sparse file is a file that contains large blocks of empty data (zeros), which modern filesystems can store efficiently by saving metadata instead of allocating physical disk blocks. When creating an archive, RAR archivers (particularly the RAR5 format) can automatically detect large sequences of zeros or preserve sparse file attributes from the host system. This ensures that the empty regions are flagged as sparse within the archive rather than stored as literal blocks of uncompressed or compressed zero bytes.

How unrar Extracts Sparse Files

When extracting files on Linux, the official unrar utility (distributed by RARLAB) natively handles sparse files if the underlying Linux filesystem (such as ext4, XFS, or Btrfs) supports them:

  1. Header Identification: During extraction, unrar reads the file flags in the RAR header. If the file was marked as sparse during compression, the utility prepares to restore that structure.
  2. Hole Punching via lseek: Instead of writing millions of literal null bytes (0x00) to the destination disk, unrar uses the POSIX lseek() system call to advance the file pointer past the empty blocks.
  3. Data Block Allocation: When actual data occurs, unrar performs a standard write() operation. The filesystem marks the skipped blocks as "holes," allocating zero physical disk space to them while maintaining the logical file size.

Because of this mechanism, extraction does not require special command-line flags to enable sparse file restoration; unrar performs this optimization automatically.

Proprietary unrar vs. unrar-free

It is important to differentiate between the utilities available on Linux distributions:

  • Official unrar (non-free): The standard package provided directly by RARLAB fully supports modern RAR5 specifications and proper sparse block recreation.
  • unrar-free: An open-source GNU alternative based on older code. This tool lacks full RAR5 compatibility and often fails to recreate sparse file structures properly, instead writing physical zero-bytes to disk and filling up storage space.

To ensure correct handling of sparse files, the proprietary RARLAB unrar binary is recommended.

Verifying Sparse File Extraction

After extracting an archive containing sparse files, you can verify whether the file was successfully written as sparse using standard Linux utilities:

  • Apparent size vs. actual size: Run ls -lh filename to see the apparent logical size of the file.
  • Allocated disk usage: Run du -h filename or ls -lhs filename to check the actual blocks allocated on the disk.

If the output of du -h is significantly smaller than the size reported by ls -lh, unrar has successfully extracted the file in sparse format.