Home > Maven > Maven – Packing and unpacking archives in Mojo with TrueZip

Maven – Packing and unpacking archives in Mojo with TrueZip

Note: an update is available.

Normally, I don’t like to write about things … hmm … that I don’t like (like bad movies or bad libraries) – simply because there are so many of them! Talking about fun things (lie Groovy and Maven :)) is what makes it all look a little bit better than it really is ..

But somehow there’s this thing that really annoyed me recently – a TrueZip library. This should be a Java API for working with archives via virtual file system. Sounds big but all I needed was to pack or unpack a couple of ZIPs, nothing fancy. Unfortunately, I couldn’t. I kid you not – there are numerous de.schlichtherle.io.File methods but I didn’t manage to pack a ZIP file in a bearable amount of time (an hour or two). Call me stupid but files were copied from place to place, other things were happening, exceptions were thrown but a simple “zip” operation, you know – I didn’t get to it. archiveCopyFrom(), archiveCopyTo(), whatever … I had some success with packing a directory, however – surprisingly (yes, I was surprised when things were happening out of sudden the way I’ve expected), archiveCopyAllFrom() did the job. Same thing happened with “unzip” – I couldn’t unpack a ZIP archive either 😦

I think that’s a good example of overly complex and not intuitive API. I bet I could figure out how to pack or unpack a file after RTFM-ing for half a day but alas, I’m not that patient for such a basic thing. TrueZip Maven Plugin to the rescue! Thanks God someone have wrapped this library with a more meaningful API (although I still don’t get how one talks about ZIP-related library and never mentions words like “(un)zip” or “(un)pack” – only “copy”, like in “copy to/from archive”).

Anyway, using FileSet and DefaultTrueZip#copy() one can easily pack and unpack archives in Mojo:

import org.apache.maven.shared.model.fileset.FileSet;
import org.codehaus.mojo.truezip.util.DefaultTrueZip;

...

getLog().info( String.format( "Packing [%s] to [%s]", 
                              sourceDirectoryPath, 
                              destinationArchivePath ));

FileSet fs = new FileSet();
fs.setDirectory( sourceDirectoryPath );
fs.setIncludes( includes );
fs.setExcludes( excludes );
fs.setOutputDirectory( destinationArchivePath );

new DefaultTrueZip().copy( fs, false, getLog());
de.schlichtherle.io.File.umount(); // To update (flush) the file

getLog().info( String.format( "[%s] packed to [%s]", 
                              sourceDirectoryPath, 
                              destinationArchivePath ));

...

getLog().info( String.format( "Unpacking [%s] to [%s]", 
                              sourceArchivePath, 
                              destinationDirectoryPath ));

FileSet fs = new FileSet();
fs.setDirectory( sourceArchivePath );
fs.setOutputDirectory( destinationDirectoryPath );

new DefaultTrueZip().copy( fs, false, getLog());
de.schlichtherle.io.File.umount(); // To update (flush) the file

getLog().info( String.format( "[%s] unpacked to [%s]", 
                              sourceArchivePath, 
                              destinationDirectoryPath ));

...

<dependency>
    <groupId>de.schlichtherle.io</groupId>
    <artifactId>truezip</artifactId>
    <version>6.6</version>
</dependency>

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>file-management</artifactId>
    <version>1.2.1</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>truezip-maven-plugin</artifactId>
    <version>1.0-beta-2</version>
    <scope>compile</scope>
</dependency>

That’s it.
I wish I could simply use TrueZip for that but, apparently, I’m not that smart to get it’s API. I just want to get my work done, you know …

Update: I’ve followed plugin’s code to see how DefaultTrueZip works but same API calls just didn’t work for me! I suppose it’s a mutual hatred from the first sight.

Advertisements
Categories: Maven Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: