Create Compressed Files With Phing

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.

<fileset dir="./" id="zipFiles">
<include name="myProject_build/**" />
</fileset>

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" />
</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.

<zip destfile="myProject.zip">
<fileset refid="zipFiles" />
</zip>

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.

C:\dev\tests\phing\myProject_build

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.

<tar destfile="phing.tar" basedir="." compression="gzip"/>

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

Comments

Did you ever get the issue with zip files sorted? I have come up against the same thing, and it is rather annoying. Mike
Permalink
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?
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.