1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
require 'rjb'
if Rjb::VERSION < '1.3.4'
$stderr.puts "require rjb-1.3.4 or later, bye."
exit 1
end
class ZipFile
include Enumerable
Zip = Rjb::import('java.util.zip.ZipFile')
def initialize(file, &block)
@zipfile = Zip.new(file)
if block
yield self
@zipfile.close
end
end
def close
@zipfile.close
end
def each(&block)
unless block
Enumerator.new(self)
else
e = @zipfile.entries
while e.has_more_elements
yield e.next_element
end
end
end
def size
@zipfile.size
end
def unzip(ent)
if String === ent
ent = @zipfile.entry(ent)
end
is = @zipfile.input_stream(ent)
buff = "\0" * 4096
File.open(ent.name, 'wb') do |fout|
loop do
len = is.read(buff, 0, buff.size)
break if len < 0
fout.write(buff[0, len])
end
is.close
end
end
end
if __FILE__ == $0
if ARGV.size == 0
puts 'usage: ruby unzip.rb filename'
else
ARGV.each do |file|
ZipFile.new(file) do |zip|
zip.each do |f|
puts "#{f.name}, #{f.size}"
unless f.directory?
zip.unzip(f)
end
end
end
end
end
end
|