-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJarReader.java
More file actions
79 lines (67 loc) · 1.65 KB
/
JarReader.java
File metadata and controls
79 lines (67 loc) · 1.65 KB
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
/*
Reads data from a Jar/Zip file,
written by Shafik Amin
*/
import java.util.jar.*;
import java.io.*;
import java.util.*;
public class JarReader extends JarFile
{
public JarReader(String filename) throws IOException
{
super(filename);
}
/* get all file names in the zip/jar as a String[] */
public String[] getFileNames()
{
ArrayList arr = new ArrayList();
Enumeration e = entries();
while (e.hasMoreElements())
arr.add(e.nextElement().toString());
return (String[])(arr.toArray(new String[1]));
}
/* subfilename is a compressed file inside the zip */
public InputStream open(String subfilename) throws IOException
{
return getInputStream(getEntry(subfilename));
}
/* read a whole file into a string (Directories
will return an empty string) */
public String readIntoString(String subfilename)
{
String toRet = "";
try
{
InputStream in = open(subfilename);
BufferedInputStream bin = new BufferedInputStream(in);
char temp;
while ((temp = (char)(bin.read())) != (char)-1)
toRet += temp;
}
catch (Exception e)
{
System.out.println(e);
}
return toRet;
}
/* for convenience */
public StringBuffer readIntoStringBuffer(String subfilename)
{
return new StringBuffer(readIntoString(subfilename));
}
/* Tester */
public static void main(String[] args)
{
try
{
JarReader r = new JarReader("test.zip");
System.out.println(r.readIntoString("test.txt"));
String[] s = r.getFileNames();
for (int i =0; i < s.length; i++)
{
System.out.println(s[i]);
}
}
catch (Exception e) { System.out.println(e); }
}
}