-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZipDirectory.java
105 lines (90 loc) · 2.05 KB
/
ZipDirectory.java
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
package org.apache.lucene.store;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import java.io.InputStream;
import java.io.IOException;
import java.util.zip.ZipException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Enumeration;
import java.util.LinkedList;
public class ZipDirectory extends Directory
{
private ZipFile file;
public ZipDirectory(String path)
throws IOException
{
try {
file = new ZipFile(path);
} catch(ZipException e) {
throw new IOException("Invalid zip file");
}
}
public String[] list()
throws IOException
{
Enumeration entries = file.entries();
LinkedList list = new LinkedList();
while(entries.hasMoreElements()) {
ZipEntry e = (ZipEntry)entries.nextElement();
list.add(e.getName());
}
String[] strings = new String[list.size()];
list.toArray(strings);
return strings;
}
public boolean fileExists(String name)
throws IOException
{
return file.getEntry(name) != null;
}
public long fileModified(String name)
throws IOException
{
return file.getEntry(name).getTime();
}
public void touchFile(String name)
throws IOException
{
fail("Touch");
}
public void deleteFile(String name)
throws IOException
{
fail("Delete");
}
/** @deprecated
*/
public void renameFile(String from, String to)
throws IOException
{
fail("Rename");
}
public long fileLength(String name)
throws IOException
{
return file.getEntry(name).getSize();
}
public IndexOutput createOutput(String name)
throws IOException
{
fail("CreateOutput");
return null;
}
public IndexInput openInput(String name)
throws IOException
{
return new ZipIndexInput(name, file);
}
public void close()
throws IOException
{
file.close();
}
private void fail(String operation)
throws IOException
{
throw new IOException(operation + " failed: Zip file is read-only");
}
}