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

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.

Parameters:

  • handlers (Hash<Regexp,Proc>, nil) (defaults to: nil)

    a hash of file handlers

Returns:

  • (Class)

    a new file class



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.

Parameters:

  • filename (String)

    the name of the file

  • mode (String, nil) (defaults to: nil)

    the open mode

Yields:

  • (File)

    this file

Returns:

  • (File)

    the opened file



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

.read(filename) ⇒ String

Reads text from a file with standard handlers.

This method recognizes gzip, bzip2 and xz compressed files.

Parameters:

  • filename (String)

    the name of the file

Returns:

  • (String)

    the content of the file



253
254
255
# File 'lib/zopen.rb', line 253

def self.read(filename)
  File.read(filename)
end

.write(filename, text) ⇒ Object

Writes text to a file with standard handlers.

This method recognizes gzip, bzip2 and xz compressed files.

Parameters:

  • filename (String)

    the name of the file

  • text (String)

    the text to be written to the file



243
244
245
# File 'lib/zopen.rb', line 243

def self.write(filename, text)
  File.write(filename, text)
end