We have two zip files and would like to compare them.
We're also interested in calculating the percentage difference between them and the file size. Is there a tool that can do this quickly?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
You could try a bash script like this one:
#!/bin/bash SIZE1=$(stat -f "%z" "$1") SIZE2=$(stat -f "%z" "$2") PERC=$(bc <<< "scale=2; ($SIZE2 - $SIZE1)/$SIZE1 * 100") echo "$PERC %"
You then call the script by passing the two files as arguments. I used the command
stat -f "%z" "$1" to get the file size and it works on OS X. Depending on your Linux distribution, you might need a different syntax, such as
stat -c%s FILENAME, or
du -b FILENAME. Try in your console, you should get the number of bytes as output.
Notice that we need to call the program
bc because Bash can't do floating point arithmetics.
Of course you can modify the script to fulfill your needs. If you need to compare two compressed files with the uncompressed file you can add another variable and do the desired maths using
You could simply write a Perl script to handle the job. It's less messy than Bash.
Here's an example of such a script. It calculates the file size reduction between the original and compressed file.
#!/usr/bin/env perl use strict; use warnings; die "You must define at least 2 files to compare!\n" unless defined $ARGV && defined $ARGV; die "File $ARGV does not exist!\n" unless -e $ARGV; die "File $ARGV does not exist!\n" unless -e $ARGV; my ($original, $compressed) = (-s $ARGV, -s $ARGV); printf "FILESIZE REDUCTION: %.2f%%\n", ($compressed / $original - 1) * -100;
You execute it like this:
NOTE: You can quote out or remove the
die conditions. They're just to remind the user to properly use this script.