[clue] Unzipping zip archives with duplicates?
Sean LeBlanc
seanleblanc at comcast.net
Tue Aug 28 20:51:20 MDT 2012
In case anyone was interested, I used Ruby to do this. The script looks
like this. It could probably be tightened up, and probably has some edge
cases it won't handle, but for now, it will do.
Dirty details: I needed this to be cross-platform - it will run in a CI
server that is Linux, but developers may use it on their clients, which
might be Windows, OS X, or Linux.
Ultimately, it will most likely be launched by Ant - modern versions now
support a "script" task that utilizes Apache's BSF which implements
JSR-223 - which means that with the JRuby jar, I can run Ruby within Ant
without shelling out. This is so I don't have to write all the crufty
Ant stuff to detect OS version, and then try to either guess where the
ruby binary might be installed or what shell to launch....bash or
cmd...which is a long way of saying "this is why there isn't a shebang
at the top of the script".
A sample call might look like:
$ ruby unzip.rb file_with_dupes.zip directoryToUnzipIn
unzip.rb contents:
require 'rubygems'
require 'zip/zip'
def unzip_file (file, destination)
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
f_path_try = f_path
versionNumber = 1
while File.file?(File.dirname(f_path_try))
f_path_try = File.join(File.dirname(f_path) + "." +
versionNumber.to_s , f.name)
versionNumber = versionNumber + 1
end
versionNumber = 1
f_path_final = f_path_try
while File.exist?(f_path_final)
f_path_final = f_path_try + "." + versionNumber.to_s
versionNumber = versionNumber + 1
end
puts f_path_final
FileUtils.mkdir_p(File.dirname(f_path_final))
zip_file.extract(f, f_path_final) unless File.exist?(f_path_final)
}
}
end
if (ARGV.size == 2) then
unzip_file(ARGV[0], ARGV[1])
end
More information about the clue
mailing list