# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
require 'zlib'
+require 'bzip2'
# Error class for all exceptions to do with Filesys.
class FilesysError < StandardError; end
path
end
- # Class method allowing open to be used on (zipped) files.
- # See File.open.
def self.open(*args)
- args = *args
- file = args.first
-
- if file == "-"
- ios = self.new(STDIN)
- elsif File.pipe? file
- ios = self.new(File.open(*args))
+ file = args.shift
+ mode = args.shift
+ options = args.shift || {}
+
+ if mode == 'w'
+ case options[:compress]
+ when :gzip
+ ios = Zlib::GzipWriter.new File.open(file, mode, options)
+ when :bzip, :bzip2
+ ios = Bzip2::Writer.new File.open(file, mode, options)
+ else
+ ios = File.open(file, mode, options)
+ end
else
- ios = self.zopen(*args)
+ if file == '-'
+ ios = STDIN
+ else
+ case `file #{file}`
+ when /gzip/
+ ios = Zlib::GzipReader.new File.open(file, mode, options)
+ when /bzip/
+ # ios = Bzip2::Reader.new File.open(file, mode, options) # TODO this method is buggy, investigate
+ ios = IO.popen("bzcat #{file}")
+ else
+ ios = File.open(file, mode, options)
+ end
+ end
end
if block_given?
begin
- yield ios
+ yield self.new(ios)
ensure
ios.close
end
else
- return ios
+ return self.new(ios)
end
end
- # TODO figure out what type is for.
- def initialize(io, type=nil)
- @io = io
- @type = type
+ def initialize(ios)
+ @io = ios
+ end
+
+ def puts(*args)
+ @io.puts(*args)
end
- # Method to close ios.
def close
@io.close
end
+ def eof?
+ @io.eof?
+ end
+
# Iterator method for parsing entries.
def each
while entry = get_entry do
yield entry
end
end
-
- # Method to puts directoy on Filesys objects.
- def puts(*args)
- @io.puts(*args)
- end
-
- private
-
- # Helper method to return an ios to a file that may be zipped in which case
- # the ios is unzipped on the fly. See File.open.
- def self.zopen(*args)
- ios = File.open(*args)
-
- begin
- ios = Zlib::GzipReader.new(ios)
- rescue
- ios.rewind
- end
-
- self.new(ios)
- end
end