
168
|
Chapter 4, File Choosers
#32 Preview ZIP and JAR Files
HACK
Before you read any further, I want to warn you that this is
one of the longest and most complicated hacks in the book. I
don’t want to scare you away, but the code is pretty dense.
Of course, you wouldn’t expect any less from a hacks book,
right? What you will learn from this hack, however, will let
you build custom filesystem views for any type of data-
source, including FTP, WebDAV, or even SQL databases. I
think the complexity is worth it. Plus, you’ll learn more about
how the
File object really works and how to hack it to pieces.
Build File Proxies
The JFileChooser uses a FileSystemView to access the real filesystem. This
view, unfortunately, assumes the existence of actual
java.io.File objects.
There is no way to represent a filesystem without
Files, which wouldn’t be a
problem except that
File is a real class with many methods, not a simple
interface. Fortunately,
File is not declared final. You can override each
method with your own version to redirect the calls to another object, and
that’s exactly how this hack works. By creating proxies around the real ZIP
file objects, you can fool the
FileSystemView into working with items that
aren’t real files.
ZIP files are represented in the
java.util.zip package by a ZipFile object
that contains one
ZipEntry for each compressed file it contains. To show the
compressed files in the chooser, each ...