#!ruby #vim: set fileencoding:utf-8 # AUTHOR: dearblue # Only this program is usable as public domain software. require "extlz4" require "optparse" require "find" PROGNAME = File.basename(__FILE__) opt = OptionParser.new(<<-EOS, 8, " ") Usage: #{PROGNAME} [option]... [file]... This program is likely lz4-cli. Reinventing the wheel. EOS class << opt # # Define hidden switch. # def hide_on(opt, &block) top.short[opt.to_s[1]] ||= OptionParser::Switch::NoArgument.new(&block) end end mode = nil verbose = 1 opt.separator("") level = 1 opt.on("-1", "fastest (worst) compression (default)") { mode = :encode; level = 1 } opt.separator(" -2 .. -8 set compression level") opt.hide_on("-2") { mode = :encode; level = 2 } opt.hide_on("-3") { mode = :encode; level = 3 } opt.hide_on("-4") { mode = :encode; level = 4 } opt.hide_on("-5") { mode = :encode; level = 5 } opt.hide_on("-6") { mode = :encode; level = 6 } opt.hide_on("-7") { mode = :encode; level = 7 } opt.hide_on("-8") { mode = :encode; level = 8 } opt.on("-9", "best (slowest) compression") { mode = :encode; level = 9 } outstdout = false opt.on("-c", "write to stdout, keep original files") { outstdout = true } opt.on("-d", "uncompress files") { mode = :decode } forceoverwrite = false opt.on("-f", "force overwriting of output file") { forceoverwrite = true } keepfile = false opt.on("-k", "don't delete input files during operation") { keepfile = true } opt.on("-q", "output no warnings") { verbose = 0 } recursive = false opt.on("-r", "recursively compress files in directories") { recursive = true } opt.on("-t", "test compressed file") { mode = :test } opt.on("-v", "increment verbosery level") { verbose += 1 } outdir = nil opt.on("-Cdir", "set output directory") { |dir| outdir = dir } blocksize = 0 blockdep = false blockchecksum = false opt.separator(" -B# set block size [4-7] (default: 7)") # merged to "-BD" opt.on("-BD", "set mode to block dependency (improve compression ratio)", %w[4 5 6 7 D]) do |o| case o when ?4, ?5, ?6, ?7 blocksize = 1 << ((o.ord - ?0.ord) * 2 + 8) when ?D blockdep = true else 0/0 end end checksum = true opt.on("-Sx", "disable content checksum (default: enabled)", %w(x)) { checksum = false } opt.on("-V", "display program version") { puts <<-EOS #{PROGNAME}: extlz4-cli program version #{LZ4::VERSION} (powered by #{RUBY_ENGINE}-#{RUBY_VERSION}) EOS exit 0 } opt.separator < 0 $stderr.puts "#{PROGNAME}: correcting lz4 file - #{infile} to #{outfile}" end if verbose > 1 outinfo = ->(mesg, offset, total, *etc) do $stderr.puts "#{mesg} (at #{offset} of #{total})" end end LZ4.fix_extlz4_0_1_bug(infile, outfile, &outinfo) end else $stderr.puts "#{PROGNAME}: mode error - #{mode}" end rescue LZ4::Error #, Object $stderr.puts "#{PROGNAME}: #{file} - #$! (#{$!.class})" end end end end rescue LZ4::Error $stderr.puts "#{PROGNAME}: #$! (#{$!.class})" rescue SystemCallError, SignalException # do nothing end