Method: FileUtil.split
- Defined in:
- lib/ec2/amitools/fileutil.rb
.split(filename, part_name_prefix, cb_size, dst_dir) ⇒ Object
Split the specified file into chunks of the specified size. yields the <file,chunk,chunk size> to a block which writes the actual chunks The chunks are output to the local directory. Typical invocation looks like: FileUtil.split(‘file’,5){|sf,cf,cs| ChunkWriter.write_chunk(sf,cf,cs)}
((|filename|)) The file to split. ((|part_name_prefix|)) The prefix for the parts filenames. ((|cb_size|)) The chunk size in bytes. ((|dst_dir|)) The destination to create the file parts in.
Returns a list of the created filenames.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ec2/amitools/fileutil.rb', line 91 def FileUtil.split(filename, part_name_prefix, cb_size, dst_dir) begin # Check file exists and is accessible. begin file = File.new(filename, File::RDONLY) rescue SystemCallError => e raise FileError.new(filename, "could not open file to split", e) end # Create the part file upfront to catch any creation/access errors # before writing out data. nr_parts = (Float(File.size(filename)) / Float(cb_size)).ceil part_names = [] nr_parts.times do |i| begin nr_parts_digits = nr_parts.to_s.length part_name_suffix = PART_SUFFIX + i.to_s.rjust(nr_parts_digits).gsub(' ', '0') part_names[i] = part_name = part_name_prefix + part_name_suffix FileUtils.touch File.join(dst_dir, part_name) rescue SystemCallError => e raise FileError.new(filename, "could not create part file", e) end end # Write parts to files. part_names.each do |part_file_name| File.open(File.join(dst_dir, part_file_name), 'w+') do |pf| write_chunk(file, pf, cb_size) end end part_names ensure file.close if not file.nil? end end |