summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/ZipArchive.java
blob: b3cd9e653856858cf23d3e698be52b1c62481d78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * This class implements an abstract directory backed by a zip
 * archive.
 */
public final class ZipArchive extends PlainFile {

    //########################################################################
    // Public Factories

    /** Returns "fromFile(new File(path))". */
    public static AbstractFile fromPath(String path) {
        return fromFile(new File(path));
    }

    /**
     * If the specified File exists and is a readable zip archive,
     * returns an abstract file backed by it. Otherwise, returns null.
     */
    public static AbstractFile fromFile(File file) {
        try {
            return new ZipArchive(file, new ZipFile(file));
        } catch (IOException exception) {
            return null;
        }
    }

    /**
     * Returns an abstract directory backed by the specified archive.
     */
    public static AbstractFile fromArchive(ZipFile archive) {
        return new ZipArchive(new File(archive.getName()), archive);
    }

    //########################################################################
    // Private Fields

    /** The zip archive */
    private final ZipFile archive;

    /** The root directory or null if not yet initialized */
    private DirEntry root;

    //########################################################################
    // Protected Constructors

    /**
     * Initializes this instance with the specified File and ZipFile.
     */
    protected ZipArchive(File file, ZipFile archive) {
        super(file);
        this.archive = archive;
        assert archive != null;
    }

    //########################################################################
    // Public Methods

    /** Returns true. */
    public boolean isDirectory() {
        return true;
    }

    /** Returns all abstract subfiles of this abstract directory. */
    public Iterator/*<AbstractFile>*/ list() {
        if (root == null) load();
        return root.list();
    }

    /**
     * Returns the abstract file in this abstract directory with the
     * specified name. If there is no such file, returns null. The
     * argument "directory" tells whether to look for a directory or
     * or a regular file.
     */
    public AbstractFile lookupName(String name, boolean directory) {
        if (root == null) load();
        return root.lookupName(name, directory);
    }

    //########################################################################
    // Private Methods

    /** Loads the archive and creates the root directory. */
    private void load() {
        this.root = new DirEntry("<root>", "/");
        // A path to DirEntry map
        HashMap/*<String,DirEntry>*/ dirs = new HashMap();
        dirs.put("/", root);
        for (Enumeration e = archive.entries(); e.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry)e.nextElement();
            String path = entry.getName();
            assert entry.isDirectory() == path.endsWith("/"): this +"-"+ path;
            if (entry.isDirectory()) {
                DirEntry dir = getDir(dirs, path);
                assert dir.entry == null: this + " - " + path;
                dir.entry = entry;
            } else {
                int index = path.lastIndexOf('/');
                String name = index < 0 ? path : path.substring(index + 1);
                String home = index < 0 ? "/"  : path.substring(0, index + 1);
                DirEntry parent = getDir(dirs, home);
                assert !parent.entries.containsKey(path): this + " - " + path;
                parent.entries.put(name, new FileEntry(name, path, entry));
            }
        }
    }

    /**
     * Lookups the specified table for a DirEntry with the specified
     * path. If successful, returns the found DirEntry. Otherwise
     * creates a new DirEntry, enters it into the table and in the
     * table of its parent ZipDir and returns it.
     */
    private DirEntry getDir(HashMap/*<String,DirEntry>*/ dirs, String path) {
        DirEntry dir = (DirEntry)dirs.get(path);
        if (dir == null) {
            int index = path.lastIndexOf('/', path.length() - 2);
            String name = index < 0 ? path : path.substring(index + 1);
            String home = index < 0 ? "/"  : path.substring(0, index + 1);
            DirEntry parent = getDir(dirs, home);
            dir = new DirEntry(name.substring(0, name.length() - 1), path);
            parent.entries.put(name, dir);
            dirs.put(path, dir);
        }
        return dir;
    }

    //########################################################################
    // Private Class - Entry

    /** Superclass of archive entries */
    private abstract class Entry extends VirtualFile {

        public Entry(String name, String path) {
            super(name, path);
        }

        public final String getPath() {
            return ZipArchive.this + "(" + super.getPath() + ")";
        }

    }

    //########################################################################
    // Private Class - DirEntry

    /** A directory archive entry */
    private final class DirEntry extends Entry {

        public final HashMap/*<String,Entry>*/ entries;
        public ZipEntry entry;

        public DirEntry(String name, String path) {
            super(name, path);
            this.entries = new HashMap();
        }

        public boolean isDirectory() {
            return true;
        }

	public long lastModified() {
	    return entry != null ? entry.getTime() : super.lastModified();
	}

        public Iterator/*<AbstractFile>*/ list() {
            return entries.values().iterator();
        }

        public AbstractFile lookupName(String name, boolean directory) {
            return (AbstractFile)entries.get(directory ? name + "/" : name);
        }

    }

    //########################################################################
    // Private Class - FileEntry

    /** A regular file archive entry */
    public final class FileEntry extends Entry {

        public final ZipEntry entry;

        public FileEntry(String name, String path, ZipEntry entry) {
            super(name, path);
            this.entry = entry;
        }

	public long lastModified() {
	    return entry.getTime();
	}

        public byte[] read() throws IOException {
            InputStream in = archive.getInputStream(entry);
            int rest = (int)entry.getSize();
            byte[] buf = new byte[rest];
            do {
                int res = in.read(buf, buf.length - rest, rest);
                if (res == -1)
                    throw new IOException("read error");
                rest -= res;
            } while (rest > 0);
            in.close();
            return buf;
        }

    }

    //########################################################################
}