Over a million developers have joined DZone.

Tar2zip.rb

·
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)
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}