summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/util/path/ZipPathEntry.java
blob: ac0226290a54592cb1672138d958370616735bbc (plain)
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
// ZipPathEntry.java -- search path element for directories

/* Copyright (C) 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

/* Author: Kresten Krab Thorup <krab@gnu.org>  */

package gnu.gcj.util.path;

import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.net.*;


final class ZipPathEntry extends PathEntry {
    final ZipFile zip;
    final URL     file;

    public String toString () { return zip.getName (); }

    ZipPathEntry (File f) 
	throws MalformedURLException, ZipException, IOException
    {
	file = new URL ("file", "", f.getPath ());
	zip  = new ZipFile (f);
	zip.readDirectory ();
    }

    /* 
       The url for a zip-file resource is,
	   
       <code>file:///path/file.zip#name</code>
	   
       Then, it is URLConnection's problem to handle that.
    */

    URL getURL (String f) {

	ZipEntry ent = zip.getEntry (f);

	try {
	    if (ent != null)
		return new URL (file, "#"+f);
	    else
		return null;
	} catch (IOException x) {
	    return null;
	}
    }

    InputStream getStream (String f) {

	ZipEntry ent = zip.getEntry (f);

	try {
	    if (ent != null)
		return zip.getInputStream (ent);
	    else
		return null;
	} catch (IOException x) {
	    return null;
	}
    }

    byte[] getBytes (String f) {
	ZipEntry ent = zip.getEntry (f);

	try {
	    if (ent != null)
		return readbytes (zip.getInputStream (ent),
				  (int) ent.getSize ());
	    else
		return null;
	} catch (IOException x) {
	    return null;
	}
	    
    }
}