Skip to content

Instantly share code, notes, and snippets.

@ericwoodruff
Last active January 3, 2016 12:49
Show Gist options
  • Save ericwoodruff/8465735 to your computer and use it in GitHub Desktop.
Save ericwoodruff/8465735 to your computer and use it in GitHub Desktop.
recursive zip compare (deep zipcmp)
#!/usr/bin/ruby
require 'fileutils'
def run(args)
args = args.gsub /$/, ''
`#{args}`
end
def listzip(file)
(run <<-EOF
unzip -l "#{file}" | tail -n+4 | head -n-2 | cut -c31-
EOF
).split ("\n")
end
def md5sum(file)
(run <<-EOF
md5sum "#{file}" | cut -c1-32
EOF
).chomp
end
def quote_args(*args)
"\"#{args.join('" "')}\"".gsub /""/,''
end
@tmps = []
def extract_zip(file, *args)
out = `mktemp -d`.strip
@tmps << out
run <<-EOF
unzip -d "#{out}" "#{file}" #{quote_args args}
EOF
return out
end
def should_recurse(f)
(run <<-EOF
file "#{f}"
EOF
)=~ /zip/i
end
def zip_diff(z1, z2)
files1 = listzip(z1)
files2 = listzip(z2)
md5_1 = md5sum z1
md5_2 = md5sum z2
return if md5_1 == md5_2
puts
puts "--- #{z1} #{md5_1}"
puts "+++ #{z2} #{md5_2}"
{z1 => files2-files1, z2 => files1-files2}.each_with_index do |(src,array),i|
if !array.empty?
array.each { |f| puts "#{0 == i ? "+" : "-"} #{f}" }
end
end
both = files1 & files2
if !both.empty?
dir1 = extract_zip z1
dir2 = extract_zip z2
both.each do |f|
if should_recurse "#{dir1}/#{f}"
zip_diff "#{dir1}/#{f}", "#{dir2}/#{f}"
end
end
end
end
zip_diff ARGV[0], ARGV[1]
FileUtils.rm_r @tmps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment