1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }