Last active
February 8, 2019 17:45
-
-
Save billdueber/a4d3cc2193cc7be419666cec906cfee9 to your computer and use it in GitHub Desktop.
Summary of zipfile by mime type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zip' | |
require 'mimemagic' | |
zipfilename = ARGV[0] | |
class MimeStats | |
attr_accessor :type, :size, :csize, :count | |
def initialize(type, size = 0, csize = 0) | |
@type = type | |
@size = size | |
@csize = csize | |
@count = 0 | |
end | |
def add(size, csize) | |
@size += size | |
@csize += csize | |
@count += 1 | |
end | |
end | |
mimetypes = Zip::File.open(zipfilename).each_with_object(Hash.new{|h, k| h[k] = MimeStats.new(k)}) do |e, mt| | |
next if e.directory? | |
guess = MimeMagic.by_path(e.name) | |
type = if guess.nil? | |
filename_components = e.name.split('/').last.split('.') | |
filename_components.size > 1 ? '.' + filename_components.last : "unknown" | |
else | |
guess.type | |
end | |
mt[type].add(e.size, e.compressed_size) | |
end | |
puts "\n" + zipfilename | |
puts "\n%-40s %-3s %10s %10s" % %w[Type Cnt Size CSize] | |
mimetypes.keys.sort{|a,b| mimetypes[b].csize <=> mimetypes[a].csize}.each do |type| | |
stats = mimetypes[type] | |
puts "%-40s %3d %9.0fk %9.0fk" % [type, stats.count, stats.size / 1024.0, stats.csize / 1024.0] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment