Sha256: f9b0deb13b864abc6f156573751cc5aaf23f6d76d9fcc83af5ebb53a616e68fd

Contents?: true

Size: 1.29 KB

Versions: 11

Compression:

Stored size: 1.29 KB

Contents

# encoding: utf-8

require 'zlib'
require 'archive/tar/minitar'
require 'zip/zip'

module Oddb2xml
 class Compressor
    include Archive::Tar
    attr_accessor :contents
    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]}")
      @contents = []
      super()
    end
    def finalize!
      if @contents.empty?
        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::ZipFile.open(@compress_file, Zip::ZipFile::CREATE) do |zip|
            @contents.each do |file|
              filename = File.basename(file)
              zip.add(filename, file)
            end
          end
        end
        if File.exists? @compress_file
          @contents.each do |file|
            Oddb2xml.download_finished(file)
          end
        end
      rescue Errno::ENOENT, StandardError
        Oddb2xml.download_finished(@compress_file)
        return false
      end
      return true
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
oddb2xml-1.7.3 lib/oddb2xml/compressor.rb
oddb2xml-1.7.2 lib/oddb2xml/compressor.rb
oddb2xml-1.7.1 lib/oddb2xml/compressor.rb
oddb2xml-1.7.0 lib/oddb2xml/compressor.rb
oddb2xml-1.6.9 lib/oddb2xml/compressor.rb
oddb2xml-1.6.8 lib/oddb2xml/compressor.rb
oddb2xml-1.6.7 lib/oddb2xml/compressor.rb
oddb2xml-1.6.6 lib/oddb2xml/compressor.rb
oddb2xml-1.6.5 lib/oddb2xml/compressor.rb
oddb2xml-1.6.4 lib/oddb2xml/compressor.rb
oddb2xml-1.6.3 lib/oddb2xml/compressor.rb