The Artima Developer Community
Sponsored Link

Java Buzz Forum
First rule: make it easy

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
First rule: make it easy Posted: Aug 25, 2003 2:37 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Chris Winters.
Original Post: First rule: make it easy
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement
One of the problems I have with many Java libraries is that they don't cater to the common usage. Take working with zip archives. Most of the time all you want to do is unpack it to a directory, preserving the directory structure instead of flattening them out. So you'd expect to find something in the java.util.zip API like:
unpack( File zipFile, File destinationDir )

I'm sorry, no such luck. Sure, it's very easy to write, but that's not the point. If you're creating a library, why not make it a one-liner for the most common actions? Compare this with the Archive::Zip module from CPAN:

my $zip_file = get_filename_from_somewhere( ... );
my $zip = Archive::Zip->new( $zip_file );
if ( $zip ) {
    $zip->extractTree( '/path/to/unpack' );
}
else {
    die "Read of $zip_file failed\n";
}

And here's what I have to do in Java, and this is even using a separate utility method for coping a stream to a file:

public static Collection unpackZip( File file, File destDir )
    throws IOException
{
    if ( ! file.isFile() )
    {
        throw new IllegalArgumentException(
                "File '" + file + "' not a valid file" );
    }
    List unzipped = new ArrayList();
    ZipFile zip = new ZipFile( file, ZipFile.OPEN_READ );
    for ( Enumeration enum = zip.entries(); enum.hasMoreElements(); )
    {
        ZipEntry entry = (ZipEntry)enum.nextElement();
        String name = entry.getName();
        File destFile = new File( destDir, name );
        File destParent = destFile.getParentFile();
        if ( ! destParent.isDirectory() )
        {
            if ( ! destParent.mkdirs() )
            {
                throw new IOException( "Cannot create directory '" + destParent + "'" );
            }
        }
        InputStream zipIn = zip.getInputStream( entry );
        copy( zipIn, destFile );
        zipIn.close();
        unzipped.add( destFile );
    }
    return unzipped;
}

Creating a zip file follows a similar pattern. Dumb dumb dumb.

Read: First rule: make it easy

Topic: What freeroller.net desperately needs Previous Topic   Next Topic Topic: Preaching to the wrong crowd?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use