Java and zip files

I’ve just spent a day debugging what at first appeared to be a permissions problem. Im using the jakarta commons file upload with a tomcat / spring setup to allow users to upload zip files, which are then unpacked and the contents are processed into a database. Obviously using a zip should make it easier to upload multiple files and directory structures.

What was happening is that the directory entries I expected to be in the zip werent being created, so none of the files could be written when it was unpacked. Eventually i discovered that the problem was related to how different tools create zip files. I use winrar, which creates a seperate entry for a directory, as luck would have it the tool my client was using doesnt.

So, if you are writing java code to unpack zip files you will be better off doing something like this :

Enumeration entries = zip.entries();

    while (entries.hasMoreElements())
    {
        ZipEntry zipEntry = (ZipEntry) entries.nextElement();
        File file = new File(outputDirectory, zipEntry.getName());

        // check the parent of this entry to see if it exists
        if(!file.getParentFile().exists())
        {
            file.getParentFile().mkdir();
        }

	InputStream is = zip.getInputStream(zipEntry);
        FileOutputStream fos = new FileOutputStream(file);

	while (is.available() > 0)
        {
            fos.write(is.read());
        }

	fos.close();
	is.close();
}

rather than just checking if zipEntry.isDirectory() and then doing a mkdir.

Of course its wise to do both, because the zip may contain empty directories and you might rely on them being present later on.

last.fm recent tracks in flash

If you’ve been wondering when the code was going to return, well, here it is. Its a little app to show you your [tag]recently played tracks[/tag] from Last.fm. [tag]Last.fm[/tag] is a great site if you like music, and who doesnt like music? So whats it all about?

As they say ‘You get your own online music profile that you can fill up with the music you like. This information is used to create a personal [tag]radio[/tag] station and to find users who are similar to you’

One really nice feature is that a lot of the data is freely available from the audioscrobbler.net site in a variety of formats. This has prompted some people to create handy little scripts that generate sigs which show what you have just been listening to. Most of them have been made using php and GD to generate a graphic, or just plain ol html. So i thought this would be a perfect opportunity to do the same in [tag]Flash[/tag], but using only open source tools and libraries (You dont need the flash IDE installed to build it..)

You can download a zip of the code from here.

To build it you will need swfmill and MTASC, and to make your life easier i’ve included an ant build file.
The build file requires the as2 [tag]ant[/tag] tasks created by Simon Wacker which you can grab from here.

Also included in the zip is a lovely font created by the nice people at orgdot and a little php script that will cache the response from last.fm so you dont hammer their servers (they ask that you dont make more than 1 request a second..)

For now the implementation is very basic, im sure theres plenty of improvements and extras that can (and should) be added. Drop me a comment if you do something interesting with it.

and if you want to see it in action you can go here

Give it away / Take it back

I like this idea a lot. Its all too easy in these times of manufactured obsolescence to just throw away things without any consideration of their potential usefulness to others and to the potentially damaging effects on the environment caused by the disposal.

One thing that I have experienced which shows a stark contrast between a rich country (the UK, where im originally from) and a poorer country (Macedonia, where I am now) is the attitude towards consumption and disposal of goods. A lot of people in the UK think nothing of throwing away and replacing things which can be fixed, could be useful to others or have even just ‘gone out of style’, whereas here (and im sure the same applies to a lot of countries around the world) the attitude is more careful and considered (although the influence of unachievable aspirational advertising is starting to erode that attitude)

Which brings me nicely to art not ads, which ‘is the start of a campaign to replace intrusive advertising in public spaces with more thought-provoking content’

Check them out and offer some help if you can. Signing the art not ads pledge is a good start.

Step to Noam you get done in.

After a particularly shabby interview with Noam Chomsky, the Guardian have realised the error of their ways and published an apology.

About time too, I wonder how many times he’s going to have to deal with the simple misunderstanding that supporting the idea of free speech doesnt mean you agree with what the person chooses to say.

As he points out in the superb documentary Manufacturing Consent, freedom of speech means freedom for exactly the views and opinions you dont agree with, otherwise where is the freedom?

Realize.

Free Music

Everyone likes free stuff and these days theres a lot of music you can get *legally* for free. Irdial put out a lot of decent records and in an act of benevolence embraced the Free Music Philosophy

This is a good thing.

So go and grab yourself some classics like Neuropolitique and Aqua Regis.

While youre at it heres some other net labels putting out good music, get your ears round some of these

pathmusick
hippocamp
monotonik

sorted.

SWT and Flash

I’ve been thinking about ways in which to use flash within eclipse and had considered using XML Sockets, but after reading the API docs for the SWT Browser I noticed that you can execute javascript functions by calling the execute method (funnily enough), and I already knew about the Flash / Javascript Integration Kit. So I knew i could send messages from java into flash, but how could i get information the other way?

Fortunately I noticed that the SWT browser allows you to listen for changes in the status text, so as a proof of concept I made a little project. Its nothing too exciting, but it shows how you communicate both ways and with a bit more work and some clever serialization I should be able to pass some rudimentary data types back and forth.

If you want to have a play you can download a zip file of the eclipse project here.

You’ll need to make sure the SWT library is on your build path to get it to compile. I’d be interested to hear if anyone has any problems using this, or if anyone makes any interesting additions to the code so we can do more complex java and flash communication within SWT.

Source code searcher

Im writing my first RCP application at the moment and with any new programming challenge im having to do a lot of searching through help files and on the net. As you all know, sometimes an example is worth a 1000 articles, so finding the Koders site has been a real blessing. They even have an eclipse plugin so you dont have to leave home to find out how others are using IWorkbenchWindowActionDelegate. 🙂

Definitely worth adding to your toolkit.

Music Wants Out

Rather than letting this song fester on my hard drive for another 2 years i’ve decided that its best to release it into the wild. Who knows when i’ll get around to putting together another record, so without further ado, just go ahead, download it, share it, enjoy it, delete it, whatever you like. Just dont try and make money from it. thanks.

MDK – Inside

and, yeah, it needs a bit of mastering because the volume is a bit low, but, you gets what you pays for 🙂

Wake up, please

If anyone is ever reading around here, I urge you to head over to media lens and read some real quality journalism. Forget BBC, ITN, CNN blah blah blah, they will only tell you what the people in power want you to hear. For your own sake, and for those who are to come, have a read, then at least write to someone and say something. If we dont wake up soon there will be no internet to share the minutae of technology we all love (and hate)

Spring 1.2.4 released and Spring IDE

Its a good week for Spring users. A new version of the framework was released, its now 1.2.4. As always its worth reading through the changelog (or is only me that finds these interesting?)
Also for Eclipse users theres a new version of the Spring IDE which features a preview of the Beans XML editor, built on top of the Eclipse Web Tools Project or WTP to add another acronym to the never ending list.
I’ve installed the WTP from the complete bundle rather than dumping the plugins into my current setup. Im just finishing a big project at the moment so I didnt want some pre V1 tool to clart my nice working setup and leave my crying over my keyboard. I havent spent much time with it yet, but its looking good and will hopefully stop the never ending search for a good and free jsp editing plugin, among other things.

Full integration of the beans xml editor file with Eclipse will be a great brain saver from switching back and forth between the code and the config file. You cant beat a bit of auto-complete. If life came with CTRL-Space (and some nice templates) it’d be a damn sight easier.

What would really sort out my server development is that kind of integration for the openamf configuration file. I’ve considered some way of using xDoclet to generate the configuration, and may well experiment with that this week. Maybe by using a combination of xDoclet and the j2as project started by Ralf Bokelberg and the whole process of keeping the value / transfer object code in sync between java and actionscript will become fully automatic. Oh, and generating the advanced gateway definition from the service facade along with an actionscript interface would be damn handy. One thing that bothers me about using remoting as it comes (i.e. without a wrapper around the service object) is that you dont get any compile time type checking on your service method calls. If i can generate the service interface in actionscript that will save a lot of flicking back and forth between the java and as code. nice.

If anyone makes one before me, please let me know.

thanks.