Last active
October 13, 2015 19:38
-
-
Save johanneswuerbach/4245890 to your computer and use it in GitHub Desktop.
Lazy extract rar archives.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "thor" | |
require "pty" | |
class LazyExtract < Thor | |
desc "extract archive", "lazy extract an archive" | |
method_option :password, :aliases => "-p", :desc => "Password for protected archives." | |
method_option :verbose, :default => false, :aliases => "-v", :desc => "Print unrar output." | |
def extract(archive) | |
if !File.exists? archive | |
puts "Archive not found. #{archive}" | |
Process.exit | |
end | |
archiveName = "#{File.basename archive, ".part01.rar"}" | |
targetDir = "#{File.dirname archive}/#{archiveName}" | |
password = options[:password] | |
cmd = "unrar x -vp -o+ " | |
if password | |
cmd += "-p#{password} " | |
end | |
cmd += "#{archive} #{targetDir}" | |
if !File.directory?(targetDir) | |
Dir.mkdir(targetDir) | |
end | |
puts cmd | |
begin | |
PTY.spawn(cmd) do |stdin, stdout, pid| | |
begin | |
done = false | |
puts "Starting to extract." | |
puts "Archive: #{archive}" | |
puts "Target: #{targetDir}" | |
chunk = 2 | |
last = "" | |
stdin.each(1) do |char| | |
last += char | |
if options.verbose? | |
putc char | |
end | |
if last =~ /\[C\]ontinue/ | |
puts "Waiting for part: #{chunk}" | |
nextArchive = archive.gsub "part01", "part#{"%02d" % chunk}" | |
while !File.exists? nextArchive | |
sleep 1 | |
end | |
puts "Found part #{chunk}." | |
stdout.puts "C" | |
chunk += 1 | |
last = "" | |
end | |
end | |
rescue Errno::EIO | |
end | |
end | |
rescue PTY::ChildExited | |
puts "The child process exited!" | |
end | |
end | |
end | |
LazyExtract.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment