Module: IMW::CompressedFiles::Base

Included in:
Archives::Tarbz2, Archives::Targz, Bz2, Gz
Defined in:
lib/imw/compressed_files.rb

Overview

Defines methods for decompressing a compressed file. This module isn’t used to directly extend an IMW::Resource – instead, format specific modules (e.g. - IMW::Resources::CompressedFiles::Bz2) include this module and further define the command-line flags &c. needed to make everything work.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#compression_settingsObject

Returns the value of attribute compression_settings.



26
27
28
# File 'lib/imw/compressed_files.rb', line 26

def compression_settings
  @compression_settings
end

Instance Method Details

#decompressIMW::Resource

Decompress this file in its present directory, overwriting any existing files while keeping the original compressed file.

FIXME The implementation is a little stupid as the file is needlessly copied.

Returns:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/imw/compressed_files.rb', line 81

def decompress
  should_exist!("Cannot decompress.")
  begin
    copy = cp(path + '.imw_copy')
    regular_file = decompress!
    copy.mv(path)
    regular_file
  ensure
    copy.mv(path) if copy && copy.exist?
  end
end

#decompress!IMW::Resource

Decompress this file in its present directory overwriting any existing files and without saving the original compressed file.

Returns:



67
68
69
70
71
72
# File 'lib/imw/compressed_files.rb', line 67

def decompress!
  should_exist!("Cannot decompress.")
  program = compression_settings[:decompression_program] || compression_settings[:program]
  FileUtils.cd(dirname) { IMW.system(program, compression_settings[:decompress], path) }
  IMW.open(decompressed_path)
end

#decompressed_basenameString

The basename of this resource after it is decompressed

IMW::Resource.new('/path/to/my_file.txt.bz2').decompressed_basename
=> 'my_file.txt'

Returns:

  • (String)

    the decompressed basename



48
49
50
# File 'lib/imw/compressed_files.rb', line 48

def decompressed_basename
  basename[0..-(extname.size + 1)]
end

#decompressed_pathString

The path of this resource after it is decompressed

IMW::Resource.new('/path/to/my_file.txt.bz2').decompressed_basename
=> '/path/to/my_file.txt'

Returns:

  • (String)

    the decompressed path



58
59
60
# File 'lib/imw/compressed_files.rb', line 58

def decompressed_path
  File.join(dirname, decompressed_basename)
end

#is_compressed?true, false

Is this file compressed?

Returns:

  • (true, false)


31
32
33
# File 'lib/imw/compressed_files.rb', line 31

def is_compressed?
  true
end

#is_compressible?true, false

Can this file be compressed?

Returns:

  • (true, false)


38
39
40
# File 'lib/imw/compressed_files.rb', line 38

def is_compressible?
  false
end