May 18, 2004

Creating gzipped archives from Java

Using the Javatar library and the standard GZIP output stream you can create gzipped tarballs pretty easily. Since the Javatar library doesn’t include any examples of creating an archive and you can spend a while spinning your wheels about how to most easily do this, here you go:

File destArchive = new File( "somefile.tar.gz" );
TarArchive archive = new TarArchive(
    new GZIPOutputStream( new FileOutputStream( destArchive ) )
// assume we've got a collection of 'someFiles' from somewhere
for ( Iterator it = someFiles.iterator(); it.hasNext(); )
    File recordFile = (File);
    TarEntry entry = new TarEntry( recordFile );
    entry.setName( recordFile.getName() );     // no leading dir info
    archive.writeEntry( entry, false );
Next: Installing MonoDevelop on OS X
Previous: HTML programmer