ubuntu extract file from zip to stdout

To extract a file from a zip archive to standard output (stdout) in Ubuntu, use the 

unzip command with the -p option. 

The -p option pipes the file's contents to standard output without extracting the metadata (date, permissions, etc.) and without printing filenames or other messages to stdout, making it ideal for use with pipes or redirection. 

Command Syntax

bash
unzip -p <archive_name>.zip <path/to/file/inside/archive> 

Examples

View a text file's content directly:

bash
unzip -p myarchive.zip document.txt | less 

This command extracts document.txt from myarchive.zip and pipes it to less for viewing, without creating a file on disk.Redirect the output to a new file on the local filesystem:

bash
unzip -p myarchive.zip path/to/zipped/file.bin > newfile.bin 

This extracts the content of file.bin and saves it as newfile.bin in the current directory. This works for both text and binary files.Extract from a piped input (more advanced):
If the zip file itself is coming from standard input (e.g., a download with curl), you can use a temporary file or a specialized script to handle the zip format, as unzip typically needs the archive as a file on disk (due to the zip directory structure being at the end of the file).A simple workaround for this, if you are sure the archive only contains one file, is:

bash
curl -s http://example.com | sh -c 'cat > /tmp/temp.zip && unzip -p /tmp/temp.zip && rm /tmp/temp.zip' 

A simpler and cleaner method for this specific case is to just download it to a temporary file, as shown above. 

Note on -c option

The -c option also extracts to stdout, but it is less useful for general scripting as it prints the filename and may perform EOL