Module: Mir::Utils

Defined in:
lib/mir/utils.rb

Class Method Summary collapse

Class Method Details

.filename_with_sequence(name, seq) ⇒ String

Generates filename for a split file filename_with_sequence(“foobar.txt”, 23) => foobar.txt.00000023

Parameters:

  • name (String)

    filename

  • seq (Integer)

    the sequence number

Returns:

  • (String)


9
10
11
# File 'lib/mir/utils.rb', line 9

def self.filename_with_sequence(name, seq)
  [name, ".", "%08d" % seq].join
end

.recombine(source_dir, dest) ⇒ void

This method returns an undefined value.

Recombines a file from pieces

Parameters:

  • the (String)

    directory that holds the split files

  • the (String)

    path to the assembled file



46
47
48
49
50
51
52
53
54
# File 'lib/mir/utils.rb', line 46

def self.recombine(source_dir, dest)
  parts = Dir.glob(File.join(source_dir, "*"))
  open(File.expand_path(dest), "wb") do |file|
    parts.each do |part|
      p_io = File.new(part)
      file.write p_io.read
    end
  end
end

.split_file(file, chunk_size, dest) ⇒ void

This method returns an undefined value.

Splits a file into pieces that may be reassembled later

Parameters:

  • File (File|String)

    to be split

  • the (Integer)

    number of bytes per each chunked file

  • where (String)

    the split files should be stored



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mir/utils.rb', line 26

def self.split_file(file, chunk_size, dest)
  try_create_dir(dest) unless Dir.exist?(dest)
  
  fname = File.join(dest, File.basename(file))
  seq = 1
  
  open(file, "rb") do |f|
    while split_data = f.read(chunk_size) do
      split_name = self.filename_with_sequence(fname, seq)
      open(split_name, "wb") { |dest| dest.write(split_data) }
      seq += 1
    end
  end
end

.try_create_dir(path) ⇒ Dir

Attempts to create the directory path specified

Parameters:

  • path (String)

    the path to create

Returns:

  • (Dir)


16
17
18
19
# File 'lib/mir/utils.rb', line 16

def self.try_create_dir(path)
  Dir.mkdir(path) unless Dir.exist?(path)
  Dir.new(path)
end