简体   繁体   中英

Jruby unzip class with each method

I am trying to write a JRuby script to unzip and parse bunch of archived log files (each log is a zip file). it can be easily implemented like this.

zipfile = java.util.zip::ZipFile.new(java.io::File.new(@zipfile))
entries = zipfile.entries

while entries.hasMoreElements
  entry = entries.nextElement
  is = zipfile.getInputStream(entry)
  bis = java.io::BufferedInputStream.new(is)
  br = java.io::BufferedReader.new(java.io::InputStreamReader.new(bis))
  while (line = br.readLine()) != nil
      /parsing line here/
    end
  end
end

but the code looks really messy. what I really want is a unzipper class which can be used like, btw the log file can be huge, so loading them all in memory is not a good option. ideally it only pull the next line whenever each is triggered

unzipped = Unzipper.new(zipped_log_file_name)
unzipped.each do |x|
    /parsing line here/
end

尝试gem rubyzip具有更多ruby-sh API http://rubydoc.info/gems/rubyzip/1.1.0/frames

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM