Class: Nucleus::ArchiveExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/nucleus/core/common/files/archive_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(exclude_git = true) ⇒ ArchiveExtractor

Returns a new instance of ArchiveExtractor.



3
4
5
# File 'lib/nucleus/core/common/files/archive_extractor.rb', line 3

def initialize(exclude_git = true)
  @exclude_git = exclude_git
end

Instance Method Details

#extract(file, destination_path, compression_format) ⇒ Integer

Extract the file to the destination path. The compression format indicates which method must be used to extract the archive.

Parameters:

  • file (IO)

    in-memory archive file to extract

  • destination_path (String)

    where the archive is going to be extracted to

  • compression_format (String)

    represented by well-known file extensions, e.g. zip or tar.gz

Returns:

  • (Integer)

    number of extracted files

Raises:

  • (StandardError)

    if the compression_format is not supported and can’t be extracted



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nucleus/core/common/files/archive_extractor.rb', line 14

def extract(file, destination_path, compression_format)
  compression_method = compression_format_method_name(compression_format)
  fail StandardError, 'Unsupported compression format' unless respond_to?(compression_method, true)

  # be sure that directory exists
  FileUtils.mkdir_p(destination_path, verbose: false)

  begin
    send(compression_method, file, destination_path)
  rescue Zip::Error, Zlib::GzipFile::Error
    raise API::Errors::ApplicationArchiveError, "Failed to extract #{compression_format} archive"
  end
end

#supports?(compression_format) ⇒ Boolean

Checks if the compression format is supported and an archive of this type could be extracted.

Parameters:

  • compression_format (String)

    represented by well-known file extensions, e.g. zip or tar.gz

Returns:

  • (Boolean)

    true if format is supported, false if not



31
32
33
34
# File 'lib/nucleus/core/common/files/archive_extractor.rb', line 31

def supports?(compression_format)
  compression_method = compression_format_method_name(compression_format)
  respond_to?(compression_method, true)
end