Skip to content

Instantly share code, notes, and snippets.

@kachick
Created May 22, 2012 04:26
Show Gist options
  • Select an option

  • Save kachick/2766561 to your computer and use it in GitHub Desktop.

Select an option

Save kachick/2766561 to your computer and use it in GitHub Desktop.
昔でっちあげたもの - ファイル分割
#!/usr/bin/ruby -w
#
# Copyright 2010 (C) Kenichi Kamiya
class Integer
def number_of_digits
n = 1
while abs > 10 ** i
n += 1
end
n
end
end
class File
class << self
def foreach_bytesize(path, bytesize)
return enum_for __callee__, path, bytesize unless block_given?
open path, 'rb' do |input|
until input.eof?
yield input.read bytesize
end
end
end
end
end
class DataSize
BINARY_PREFIXES = %w(byte kiro mega giga tera).freeze
RATIO = 1024
def initialize(size, unit='byte')
raise ArgumentError unless BINARY_PREFIXES.include? unit
@byte = size * (RATIO ** BINARY_PREFIXES.index(unit))
end
BINARY_PREFIXES.each_with_index do |prefix, idx|
define_method prefix do
@byte * (Rational(1, RATIO) ** idx)
end
end
end
require 'pathname'
require 'io/nosey'
extend IO::Nosey
PATH = ask 'Source file ?', %r!\A([^/:*?"<>|]+)$! do |_, m|
path = Pathname.new m[1]
if path.readable?
path
else
invalid "This script cannont read '#{path}'."
end
end
puts "Path: #{PATH}", "Size: #{DataSize.new(PATH.size).mega.to_f.to_s[0..4]}MB"
UNIT = choose("Choose a unit.", *DataSize::BINARY_PREFIXES)
SIZE = ask('Size on the unit ?', /\A([1-9]\d*)\z/) {|s, m|m[1].to_i}
DATA_SIZE = DataSize.new SIZE, UNIT
SPLITED_PATHS = []
File.foreach_bytesize(PATH, DATA_SIZE.byte).with_index do |data, idx|
path = "#{PATH}.#{idx + 1}"
SPLITED_PATHS << path
open path, 'wb' do |output|
output << data
end
end
open "#{PATH}.bat", 'w' do |bat|
bat.puts %Q!copy /BV-Y #{SPLITED_PATHS.map{|path|"\"#{path}\""}.join '+'} "#{PATH}"!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment