Create Compressed Files With Phing

15th January 2009

Building your projects into directories is nice, but distributing these projects is difficult if you have to build the compressed files yourself. Phing has the ability to create zip and tar files using simple commands.

The most convenient way of using the tar and zip commands is by using a fileset. But rather than use the fileset that was used to copy the files into the build directory it is best to create a separate fileset that is used to compress the contents of the build directory.


To compress the files into a tar use the tar element. This accepts two parameters, the destination file (called destfile) and the compression to use (called compression). The compression attribute can take one of three values, these are gzip, bzip2 and none. Using none will not run any compression algorithms on the files. The following example will create a file called myProject.tar in the current directory, which contains all of the files and directories from the myProject_build directory.

<tar destfile="myProject.tar" compression="gzip"><fileset refid="zipFiles"></fileset></tar>

To compress the files into a zip file you need to use the zip element. This works in exactly the same way as the tar element, except it doesn't have a compression attribute.


The tar element works very well and the file created was as expected, but I had a little bit of trouble with the zip element. I am using Windows XP to run phing and the file that was created was blocked by Windows for having odd content. After I figured out how to open it I realized that phing had used the full path to the build folder. So rather than the zip file containing a folder called myProject_build, it contained the following folder structure, with the correct files included in the end directory.


Has anyone else seen this? I would be grateful for any help on the subject.

You can also use the zip and tar elements without the fileset element by using the basedir attribute. By setting the basedir you are telling phing to use everything in that directory and every sub directory.


There is a warning on the phing website that states using basedir and fileset simultaneously can result in strange contents in the archive. So I would suggest using one or the other.

Automated Build With Phing


Did you ever get the issue with zip files sorted? I have come up against the same thing, and it is rather annoying. Mike

Mike Dowler (Tue, 05/12/2009 - 20:59)

Just had the problem you describe with zipped files. But this on a Linux workstation and Archive Tool (Linux equiv. to - say - WinZip) could not open them. So maybe a cross-platform problem with current versions of phing?

JamesB (Wed, 11/07/2012 - 17:41)

Add new comment

The content of this field is kept private and will not be shown publicly.