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(ext = 'tar.gz') ⇒ Compressor

Returns a new instance of Compressor.



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

def initialize(ext='tar.gz')
  @compressed_file = 'oddb_xml_' + Time.now.strftime("%d.%m.%Y_%H.%M.#{ext}")
  @contents = []
  super()
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

Instance Method Details

#finalize!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oddb2xml/compressor.rb', line 15

def finalize!
  unless @contents.select{ |file| File.exists?(file) }.length == 2
    return false
  end
  begin
    tgz = Zlib::GzipWriter.new(File.open(@compressed_file, 'wb'))
    Minitar.pack(@contents, tgz)
    if File.exists? @compressed_file
      @contents.each do |file|
        File.unlink file
      end
    end
  rescue => error
    puts error
    if File.exists? @compressed_file
      File.unlink @compressed_file
    end
    return false
  end
  return true
end