Skip to content

Instantly share code, notes, and snippets.

@Voker57
Created July 11, 2010 13:48
Show Gist options
  • Save Voker57/471565 to your computer and use it in GitHub Desktop.
Save Voker57/471565 to your computer and use it in GitHub Desktop.
Optimally fits files into zip archives. I think.
# usage: ruby arch.rb <size> <files...>
size = ARGV.shift.to_i
$sizes_map = []
ARGV.each do |f|
$sizes_map << [File.size(f), f]
end
$sizes_map.sort!
def get_closest(size)
if $sizes_map[0][0] > size
return nil
end
(1..$sizes_map.length - 1).each do |i|
if $sizes_map[i][0] < size or i == $sizes_map.length - 1
return $sizes_map.delete_at(i-1)
end
end
return $sizes_map.delete_at(0)
end
cnt = 0
current_size = size
names = []
while not $sizes_map.empty?
a = get_closest(current_size)
if a
sz, nm = a
current_size -= sz
names << nm
elsif current_size == size
puts "Couldn't fit a file."
exit
else
`zip -u #{cnt}.zip #{names.join(" ")}`
current_size = size
names = []
cnt += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment