How to unzip, gunzip, untar files with Ruby
This is a very advanced and resource efficient algorithm for expanding compressed content with Ruby:
1 def gunzip(filename) 2 command = "gunzip --force #{filename}" 3 success = system(command) 4 5 success && $?.exitstatus == 0 6 end
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>:
1 command = "nice -n 5 gunzip --force #{filename}"
There’s also ionice…
Find a text pattern in jar files
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.
1 #!/bin/sh 2 for f in `find . -type f -name '*\.jar'` 3 do 4 unzip -l $f | grep -i $1 && echo "was found in $f" 5 done