DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Tar2zip.rb

10.03.2005
| 1255 views |
  • submit to reddit
        Converts the given tarfile to a zipfile. You must have tar and zip in the system PATH. You will need to uncompress the tarfile or modify the script if you tar is compressed. 

#! /usr/bin/ruby
class Tar2Zip
	def run(dir, cmd)
		pwd = Dir.getwd
		print "cd ",dir, "\n"	
		Dir.chdir(dir)
		print cmd, "\n"
		system(cmd)
		print "cd ",pwd, "\n"	
		Dir.chdir(pwd)
	end
	def main(argv)
		tarfile=File.expand_path(argv[0])
		zipfile= File.join(File.dirname(tarfile), File.basename(tarfile,".tar"))+".zip"
		if File.exists?(zipfile)
			print zipfile, " already exists\n"
			return
		end	
		pwd = Dir.getwd
		basename = File.basename(tarfile)
		tmpdir = File.join("/tmp", basename)
		Dir.mkdir(tmpdir)
		run(tmpdir, "tar -xvf #{tarfile}")
		run(tmpdir, "zip -rm #{zipfile} .")
		Dir.unlink(tmpdir)
	end
end
Tar2Zip.new.main(ARGV)