Home > bash > Writing 7zless (works like zless on *.7z files)

Writing 7zless (works like zless on *.7z files)

After working with some 7z files I got tired of the whole uncompress, compress cycle. So, I had a look at /bin/zless and realised that all I had to do was replace gzip with 7z. Now I have my very own 7zless.


#!/bin/bash

# Copyright (C) 1998, 2002, 2006, 2007 Free Software Foundation

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

PATH="${GZIP_BINDIR-'/bin'}:$PATH"; export PATH

version="zless (gzip) 1.3.12
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Eggert."

usage="Usage: $0 [OPTION]... [FILE]...
Like 'less', but operate on the uncompressed contents of any compressed FILEs.

Options are the same as for 'less'.

Report bugs to <bug-gzip@gnu.org>."

case $1 in
--help)    exec echo "$usage";;
--version) exec echo "$version";;
esac

if test "${LESSMETACHARS+set}" != set; then
 # Work around a bug in less 394 and earlier;
 # it mishandles the metacharacters '$%=~'.
 space=' '
 tab='    '
 newline='
'
 LESSMETACHARS="$space$tab$newline'"';*?"()<>[|&^`#\$%=~'
 export LESSMETACHARS
fi

LESSOPEN="|gzip -cdfq -- %s"; export LESSOPEN
exec less "$@"

So I replaced gzip -cdfq with 7z e -so -bd


#!/bin/bash

version="7zless (7z) 1.0.0 "

usage="Usage: $0 [OPTION]... [FILE]...
Like 'less', but operate on the uncompressed contents of any FILEs that were compressed using 7z.
"

case $1 in
--help)    exec echo "$usage";;
--version) exec echo "$version";;
esac

if test "${LESSMETACHARS+set}" != set; then
 # Work around a bug in less 394 and earlier;
 # it mishandles the metacharacters '$%=~'.
 space=' '
 tab=' '
 newline='
'
 LESSMETACHARS="$space$tab$newline'"';*?"()<>[|&^`#\$%=~'
 export LESSMETACHARS
fi

LESSOPEN="|7z e -so -bd -y -- %s"; export LESSOPEN
exec less "$@" 2>/dev/null

And that’s it. Still needs some work but atleast I can view the contents of a file without having to extract it first.

Now just to figure out how to emulate –quiet mode in 7z (7-Zip version 4.57) so that it stops displaying “unneccesary” details. Now to write 7zcat!

Advertisements
Categories: bash Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s