How to Pipe Stdin into a 7-Zip Archive

This guide explains how to stream standard input (stdin) directly into a 7-Zip archive using the command-line interface. By utilizing 7-Zip's built-in standard input switch, you can compress data streams from other programs on the fly without writing temporary files to disk. Below, you will find the exact argument syntax, optional parameters for specifying file names, and practical command examples.

The -si Switch Syntax

The command-line syntax for reading from standard input in 7-Zip is the -si switch.

The general syntax format is:

<data-producer> | 7z a <archive_name> -si[internal_file_name]

Syntax Options

  • -si (Default Name): If you use the -si flag without appending a name, 7-Zip creates an archive containing a single file with no name or an empty extension, depending on the archive format.
  • -si<name> (Explicit Name): You can specify the filename to be used inside the archive by immediately appending the desired name to the switch without a space (e.g., -sidata.txt).

Practical Examples

1. Compress Standard Output to an Archive

To pipe the output of a file directly into a 7-Zip archive and name the compressed entry backup.sql:

mysqldump -u root -p database_name | 7z a db_backup.7z -sibackup.sql

2. Stream and Compress a tar Stream

When archiving directories on Linux or macOS while preserving permissions, pair tar with 7-Zip:

tar -cf - /var/log | 7z a logs.tar.7z -si

In this case, 7-Zip treats the incoming tar stream as the payload for the logs.tar.7z container.

3. Pipe Stdin and Stream to Stdout

You can combine -si (standard input) with -so (standard output) to create a completely streamable compression pipeline:

cat large_file.bin | 7z a dummy.7z -si -so > compressed_output.7z

When using -so, the archive file parameter in the command acts as a placeholder, as the resulting compressed stream is redirected directly to standard output.