View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.commons.vfs.mime.zip;
22  
23  import org.millscript.commons.alert.alerts.IOAlert;
24  import org.millscript.commons.vfs.AbstractVFile;
25  import org.millscript.commons.vfs.VFile;
26  import org.millscript.commons.vfs.VFolder;
27  import org.millscript.commons.vfs.alerts.ZipAlert;
28  import org.millscript.commons.vfs.mime.AbstractMIMEVVolume;
29  
30  import java.io.IOException;
31  import java.util.Enumeration;
32  import java.util.zip.ZipEntry;
33  import java.util.zip.ZipException;
34  import java.util.zip.ZipFile;
35  
36  /**
37   * This class implements a virtual filesystem for read-only access to a zip
38   * file.
39   */
40  public class ReadOnlyZipVolume extends AbstractMIMEVVolume {
41  
42      /**
43       * The ZIP file that backs this virtual filesystem volume.
44       */
45      private final ZipFile rootZipFile;
46  
47      /**
48       * Constructs a new local file volume with the specified file as it's root.
49       *
50       * @param sourceFile    the source virtual ZIP file
51       */
52      ReadOnlyZipVolume( final AbstractVFile< ? > sourceFile ) {
53          super( sourceFile );
54          try {
55              // Get the root ZIP file
56              this.rootZipFile = new ZipFile( this.getTempBackingFile().getFile() );
57          } catch ( ZipException ex ) {
58              throw new ZipAlert(
59                  "Could not open ZIP file"
60              ).culpritURI( this.getTempBackingFile().getURI() ).setParentThrowable( ex ).mishap();
61          } catch ( IOException ex ) {
62              throw new IOAlert(
63                  "There was a problem reading the ZIP file"
64              ).culpritURI( this.getTempBackingFile().getURI() ).setParentThrowable( ex ).mishap();
65          }
66      }
67  
68      /**
69       * Returns an enumeration over all this zip files entries.
70       *
71       * @return  an Enumeration over all this zip files entries
72       */
73      final Enumeration getEntries() {
74          return this.rootZipFile.entries();
75      }
76  
77      /**
78       * Returns the ZipEntry for the specified path.
79       *
80       * @param path  the path to get a ZipEntry for
81       * @return  the ZipFile for the root of this volume
82       */
83      final ZipEntry getZipEntry( final String path ) {
84          return this.rootZipFile.getEntry( path );
85      }
86  
87      /**
88       * @see org.millscript.commons.vfs.mime.AbstractMIMEVVolume#makeRootVFolder(org.millscript.commons.vfs.VFile)
89       */
90      @Override
91      protected VFolder makeRootVFolder( final VFile sourceFile ) {
92          return new ReadOnlyZipFolder( this, sourceFile.getParent(), sourceFile.getName() );
93      }
94  
95  }