Class: Oddb2xml::Compressor

Inherits:
Object
  • Object
show all
Includes:
Archive::Tar
Defined in:
lib/oddb2xml/compressor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix = 'oddb', options = {}) ⇒ Compressor

Returns a new instance of Compressor.



11
12
13
14
15
16
17
18
19
20
# File 'lib/oddb2xml/compressor.rb', line 11

def initialize(prefix='oddb', options={})
  @options = options
  @options[:compress_ext] ||= 'tar.gz'
  @options[:format]       ||= :xml
  @compress_file = "#{prefix}_#{@options[:format].to_s}_" + Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}")                                 
                             #      @compress_file = File.join(WorkDir, "#{prefix}_#{@options[:format].to_s}_" +
                             #Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}"))
  @contents = []
  super()
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



10
11
12
# File 'lib/oddb2xml/compressor.rb', line 10

def contents
  @contents
end

Instance Method Details

#finalize!Object



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
# File 'lib/oddb2xml/compressor.rb', line 21

def finalize!
  if @contents.empty? and @contents.size == 0
    return false
  end
  begin
    case @compress_file
    when /\.tar\.gz$/
      tgz = Zlib::GzipWriter.new(File.open(@compress_file, 'wb'))
      Minitar.pack(@contents, tgz)
    when /\.zip$/
      Zip::File.open(@compress_file, Zip::File::CREATE) do |zip|
        @contents.each do |file|
          filename = File.basename(file)
          zip.add(filename, file)
        end
      end
    end
    if File.exists? @compress_file
      puts "#{__LINE__}: @compress_file"
      @contents.each do |file|
        @tmpfile = file
      puts "#{__LINE__}: @tmpfile"
        FileUtils.rm(file) if file && File.exists?(file)
      end
    end
    rescue Errno::ENOENT, StandardError => e
    return false
  end
  return true
end