unzip snippets

Find a text pattern in jar files

Tagged jar, find, search, recursive, linux, unzip  Languages bash

Helpful when you need to find a class or package in some jar file recursively below the current directory. Still needs a test to see if the file found was a file or directory. Works case insensitively. Uses the unzip command because of it's performance superiority in comparison to jar.

for f in find . -type f -name '*\.jar'
        unzip -l $f | grep -i $1 && echo "was found in $f"

How to unzip, gunzip, untar files with Ruby

Tagged gunzip, ruby, untar, unzip, nice, ionice  Languages ruby

This is a very advanced and resource efficient algorithm for expanding compressed content with Ruby:

def gunzip(filename)
  command = "gunzip --force #{filename}"
  success = system(command)
  success && $?.exitstatus == 0

To customize, change gunzip to whatever command you like.

For example, to avoid 100% CPU utilization during uncompression, set the niceness value of the command with nice -n 5 <command>:

command = "nice -n 5 gunzip --force #{filename}"

There's also ionice...

If you want you can also use GzipReader:

res = RestClient.get(url)
body = res.body
body = Zlib::GzipReader.new(StringIO.new(body)).read if res.headers[:content_type].include?('gzip')