Module: RNCP::Files

Included in:
NcpListener, NcpPusher
Defined in:
lib/rncp/files.rb

Overview

Module containing functions related to the file system and archive compression/decompression.

Instance Method Summary collapse

Instance Method Details

#directory_list(path) ⇒ Array

Recursively creates a list of all items (files/directories) found in the path.

Parameters:

  • path (String)

    path to starting directory or file

Returns:

  • (Array)

    flatten array of all contents of directory, or file



26
27
28
29
30
31
32
33
34
35
# File 'lib/rncp/files.rb', line 26

def directory_list(path)
  return [path].flatten if File.directory?(path) == false

  data = []
  Dir["#{path}**/*", "#{path}**/.*"].each do |entry|
    next if entry[/^.*\/\.+$/]
    data << directory_list(entry)
  end
  return data.flatten
end

#tar(files) ⇒ Object

Creates a data string of a tar gzip file containing all files in list. See #untar for decompression. return [StringIO] data bytes of tar gzip archive.

Parameters:

  • files (Arrray)

    list of files to add to archive



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rncp/files.rb', line 41

def tar(files)
  data = StringIO.new("")
  sgz = Zlib::GzipWriter.new(data)
  tar = Archive::Tar::Minitar::Output.new sgz

  puts "[#] start writing files"
  files.each do |f|
    directory_list(f).each do |entry|
      puts "[*] adding: #{entry}"
      Archive::Tar::Minitar.pack_file(entry, tar)
    end
  end

  sgz.flush
  sgz.close
  sgz = nil

  data.rewind
  return data
end

#untar(data) ⇒ Object

Decompresses a data string of a tar gzip archive to current working directory. See #tar for compression.

Parameters:

  • data (String)

    data bytes of tar gzip archive



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rncp/files.rb', line 65

def untar(data)
  sgz = Zlib::GzipReader.new(StringIO.new(data))
  tar = Archive::Tar::Minitar::Input.new sgz

  tar.each do |entry|
    puts "[*] #{entry.name}"
    tar.extract_entry "./", entry
  end

  sgz.close
end