TransWikia.com

less command does not open zip files on debian

Super User Asked by M.J on December 16, 2020

Here I am using debian/testing distribution on my machine but there is something interesting.

# less file.zip
"./file.zip" may be a binary file.  See it anyway?

Do I need to install some extra packages on my machine in order to be able to open zip files on my machine using less command?

2 Answers

The lesspipe tool can transparently convert files to text when you call less on them. This includes decompressing .gz files and listing the contents of .zip files. Debian includes lesspipe in the less package but doesn't automatically configure it.

Add the following to your shell's login script (e.g, ~/.bash_profile):

eval "$(lesspipe)"

This should add two environment variables, LESSOPEN and LESSCLOSE. Once you've done this, running less on a zip file should show you the list of files in the zip file.

Correct answer by satwell on December 16, 2020

tl;dr: unzip -p file.zip | less


Some background

ZIP

I think the ZIP file format first appeared on MSDOS and then MS Windows. It was primarily a way to gather a group of many files together in an archive and compress the combined data. In other words it creates a multi-file compressed archive. You cannot assume a ZIP file contains a single file and assume that file is a text file.

I recall the compression algorithms used by zip utilities changed over the years, though probably later tools could handle earlier compression algorithms. I think the most common tools for creating ZIP files were originally the PKZIP tools created by Phil Katz at PKWare. Later MS WIndows acquired the ability to create ZIP files natively.

Unix & Linux

On Unix-like systems the traditional equivalent method was to first gather multiple files together using a tool such as tar to create a something.tar file and then separately compress it to create a something.tar.Z file. Later Gnu's tar acquired compression capabilities from Gnu-Zip (gzip) and could create a something.tar.gz or something.tgz in a single operation.

So a file with a filename extension of .ZIP is most likely to have originated on Windows not Linux.

If your ZIP file definitely contains a single text file, a general solution would be to pipe the output of an unzip operation into less. This depends on having an unzip command that reads your particular flavour of zip compression and has options to write to STDOUT.

Commonly installed tools for viewing text files compressed using some "zip" compression algorithm include zcat and bzless. The man pages for these say

GZIP(1)                                                  General Commands Manual    
NAME
       gzip, gunzip, zcat - compress or expand files

SYNOPSIS
       gzip [ -acdfhklLnNrtvV19 ] [--rsyncable] [-S suffix] [ name ...  ]
       gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ...  ]
       zcat [ -fhLV ] [ name ...  ]

DESCRIPTION
       Gzip  reduces  the  size of the named files using Lempel-Ziv coding (LZ77).

and

BZMORE(1)                                                General Commands Manual 

NAME
       bzmore, bzless - file perusal filter for crt viewing of bzip2 compressed text

SYNOPSIS
       bzmore [ name ...  ]
       bzless [ name ...  ]

But this depends on the chosen compression utilities knowing the particular compression algorithm used on your zip file.

PKZIP compatible Unzip

Most likely you need an unzip tool that is compatible with the DOS/Windows ZIP format. On some Linux systems you may need to install this first using whatever package manager is appropriate for your distribution. For example sudo apt install zip.

$ ls -l eg*
-rw-rw-rw- 1 rgb rgb 182 Aug 30 19:10 eg.zip

$ unzip -l eg.zip
Archive:  eg.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       20  2020-08-30 18:50   eg.txt
---------                     -------
       20                     1 file

$ unzip -p eg.zip
hello world
The end

$ unzip -p eg.zip | less
hello world
The end

Answered by RedGrittyBrick on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP