class Reviser::Components::Archiver

Manages uncompression of archive. Archiver extracts all data in a given compressed file.

In case of the University of Lorraine, the archive contains all computing projects, compressed too.

If you want to add support of archive format, @see Extractors.

@example The simple way to extract a compressed file is :

Archiver.extract(myFile, myDirectory)

@author Yann Prono

Public Class Methods

destination?(destination) click to toggle source

Checks if the destination directory exists. else create it. For the moment, if the directory exists // TODO little input to confirm @param destination [String] the destination directory

# File lib/reviser/components/archiver.rb, line 64
def self.destination?(destination)
        unless $rejected.include? File.basename destination
                FileUtils.rm_rf(destination) if Dir.exists? destination
                FileUtils.mkdir_p destination, :mode => 0700
        end
end
extract(file_name, destination = '.') click to toggle source

Extracts the archive into the destination directory. @param file_name [String] The name of the archive. @param destination [String] The destination directory.

# File lib/reviser/components/archiver.rb, line 76
def self.extract(file_name, destination = '.')
        raise "#{file_name} not found, please check in the current directory." unless File.exist? file_name

        # Get extension of file_name to know which method calls
        ext = File.extname(file_name)
        ext = ext.delete('.')

        # Raise exception if the format is unknown by Archiver
        raise "Unknown compression format '#{ext}'" unless respond_to?(ext)

        # Check if destination exists
        self::destination? destination

        # Run extraction!
        send(ext,file_name, destination)
end
new(data) click to toggle source

Initializes archive file and the directory of destination.

Calls superclass method Reviser::Component::new
# File lib/reviser/components/archiver.rb, line 51
def initialize(data)
        super data
        @src = Cfg[:src]
        @destination = Cfg[:dest]
        @results = []
end

Public Instance Methods

run() click to toggle source

Method which extracts an archive which contains all computing projects.

This method extracts,in first time,the archive given in the constructor and after, all extracted files.

Use this method in a global usage of Reviser! Options are for the moment :verbose

# File lib/reviser/components/archiver.rb, line 103
def run
        @logger.h1 Logger::INFO,"First extraction - #{@src}"
        # Extract the original archive
        Archiver.extract(@src, @destination)

        @logger.h1 Logger::INFO,'Extraction of sub archives'
        
        # Extract all sub archives
        entries = Dir.entries(@destination) - $rejected
        extracted = 0

        entries.each do |entry|
                puts "----[#{extracted+1}/#{entries.size}]\t#{entry}"

                ext = File.extname entry
                basename = File.basename entry, ext
                #begin
                        file_name = File.join(@destination,File.basename(entry))
                        destination = File.join(@destination,basename)

                        # Run extraction!
                      Archiver.extract(file_name, destination)
                        extracted += 1

                        @logger.h2 Logger::INFO, "extracting #{file_name} to #{destination}"
                        @results << basename

                # Delete in all case the archive (useless after this step)
                FileUtils.rm_rf file_name
      end
      @logger.h1 Logger::INFO, "[#{extracted}/#{entries.size}] projects have been processed"

        @results
end