The Artima Developer Community
Sponsored Link

Perl Buzz Forum
First rule: make it easy

0 replies.

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 flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded 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 3:06 PM
Reply to this message Reply

This post originated from an RSS feed registered with Perl Buzz by Chris Winters.
Original Post: First rule: make it easy
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/raw/cwinters_perl.rdf
Feed Description: Chris Winters on Perl, programming and technology
Latest Perl Buzz Posts
Latest Perl Buzz Posts by Chris Winters
Latest Posts From cwinters.com

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: First rule: make it easy Previous Topic   Next Topic Topic: Because One Can Never Get Enough Perl Idioms

Sponsored Links



Google
  Web Artima.com   

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