Module: Mir::Utils
- Defined in:
- lib/mir/utils.rb
Class Method Summary collapse
-
.filename_with_sequence(name, seq) ⇒ String
Generates filename for a split file filename_with_sequence(“foobar.txt”, 23) => foobar.txt.00000023.
-
.recombine(source_dir, dest) ⇒ void
Recombines a file from pieces.
-
.split_file(file, chunk_size, dest) ⇒ void
Splits a file into pieces that may be reassembled later.
-
.try_create_dir(path) ⇒ Dir
Attempts to create the directory path specified.
Class Method Details
.filename_with_sequence(name, seq) ⇒ String
Generates filename for a split file filename_with_sequence(“foobar.txt”, 23) => foobar.txt.00000023
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
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.(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
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
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 |