How do I zip and unzip files using Ant?
Author: Deron Eriksson
Description: This Ant tutorial describes how to zip and unzip files.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

Zipping and unzipping files with AntSW is very easy since we can use the zip and unzip tasks. The EclipseSW project in this tutorial features a directory that we shall zip up called 'zip-me'. It contains two files that will be archived, and one file that will be excluded from the zip archive. The project features an Ant build.xml file. The project is shown below.

'zip-test' project

The build.xml file has three targets: clean, zip, and unzip. The clean target cleans up any directories, files, or zip files created by Ant. The zip target is set as the default target. It contains the zip task, with the name of the zip file specified by the destfile attribute, the name of the directory to zip specified by the basedir attribute, and the files to exclude specified by the excludes attribute. Files beginning with 'dont' are excluded from the zip archive. The unzip target features the unzip task. In the unzip task, the name of the zip file is specified using the src attribute, and the destination directory is specified by the dest attribute.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="zip-test" default="zip" basedir=".">

	<property name="project-name" value="${ant.project.name}" />
	<property name="folder-to-zip" value="zip-me" />
	<property name="unzip-destination" value="unzipped" />

	<target name="clean">
		<delete file="${project-name}.zip" />
		<delete dir="${unzip-destination}" />
	</target>

	<target name="zip">
		<zip destfile="${project-name}.zip" basedir="${folder-to-zip}" excludes="dont*.*" />
	</target>

	<target name="unzip">
		<unzip src="${project-name}.zip" dest="${unzip-destination}" />
	</target>

</project>

(Continued on page 2)

Page:    1 2 >