Module: ZOpen
- Defined in:
- lib/zopen.rb
Overview
Introduction
ZOpen is a simple module that provides a File-like class, which *can read and write to uncompressed as well as compressed *files. Whether a file is compressed or not is determined by a *(programmable) file extension.
The default class automatically recognizes files ending in .gz (gzip), .bz2 (bzip2) and .xz (xz). If the corresponding ruby modules are available (Zlib [builtin], Bzip2 [bzip2-ruby gem] and xz [ruby-xz gem], respectively) they are used. Otherwise ZOpen uses external packers (gzip, bzip2 and xz) for compression.
ZOpen allows to create new Files that recognize different file extensions and use different packers.
Defined Under Namespace
Classes: File
Constant Summary collapse
- Plain =
Handler for a plain file.
This just opens a File
lambda do |filename, mode| File.new(filename, mode) end
- Gzip =
Handler for gzip compressed files.
This handler uses either zlib module or the external gzip program.
lambda do |filename, mode| begin require 'zlib' case mode when /r/, nil then Zlib::GzipReader.open(filename) when /w/ then Zlib::GzipWriter.open(filename) else fail "Unsupported open mode: #{mode}" end rescue LoadError case mode when /r/, nil then IO.popen("gzip -d -c '#{filename}'", mode) when /w/ then IO.popen("gzip -c - > '#{filename}'", mode) else fail "Unsupported open mode: #{mode}" end end end
- Bzip2 =
Handler for bzip2 compressed files.
This handler uses either bzip2 module provided by the bzip2-ruby gem or the external bzip2 program.
lambda do |filename, mode| begin require 'bzip2' case mode when /r/, nil then Bzip2::Reader.open(filename) when /w/ then Bzip2::Writer.open(filename) else fail "Unsupported open mode: #{mode}" end rescue LoadError case mode when /r/, nil then IO.popen("bzip2 -d -c '#{filename}'", mode) when /w/ then IO.popen("bzip2 -c - > '#{filename}'", mode) else fail "Unsupported open mode: #{mode}" end end end
- XZ =
Handler for xz compressed files.
This handler uses either XZ module provided by the ruby-xz gem or the external xz program.
lambda do |filename, mode| begin require 'xz' case mode when /r/, nil then ::XZ::StreamReader.open(filename) when /w/ then ::XZ::StreamWriter.open(filename) else fail "Unsupported open mode: #{mode}" end rescue LoadError case mode when /r/, nil then IO.popen("xz -d -c '#{filename}'", mode) when /w/ then IO.popen("xz -c - > '#{filename}'", mode) else fail "Unsupported open mode: #{mode}" end end end
Class Method Summary collapse
-
.newfile(handlers = nil) ⇒ Class
Create a new File-like class with handlers.
-
.open(filename, mode = nil) {|File| ... } ⇒ File
Opens a file with standard handlers.
-
.read(filename) ⇒ String
Reads text from a file with standard handlers.
-
.write(filename, text) ⇒ Object
Writes text to a file with standard handlers.
Class Method Details
.newfile(handlers = nil) ⇒ Class
Create a new File-like class with handlers.
This handlers parameter should be a hash whose keys are a regular expression and the values are open-like callable objects. If a name of a file to be opened matches a regular expression, the corresponding callable object is called with the file name and the open mode, and it should return an appropriate IO object.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/zopen.rb', line 205 def self.newfile(handlers = nil) if handlers then zFile = Class.new(File) zFile.send :define_method, :handlers do handlers end zFile else File end end |
.open(filename, mode = nil) {|File| ... } ⇒ File
Opens a file with standard handlers.
This opens a file that recognized gzip, bzip2 and xz compressed files.
Like File.open this method may be passed a code block which is called with the new File as parameter.
229 230 231 232 233 234 235 |
# File 'lib/zopen.rb', line 229 def self.open(filename, mode=nil) if block_given? then File.open(filename, mode) { |f| yield f } else File.open(filename, mode) end end |