1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.zip;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.charset.Charset;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Collection;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.commons.vfs2.Capability;
34 import org.apache.commons.vfs2.FileName;
35 import org.apache.commons.vfs2.FileObject;
36 import org.apache.commons.vfs2.FileSystemException;
37 import org.apache.commons.vfs2.FileSystemOptions;
38 import org.apache.commons.vfs2.Selectors;
39 import org.apache.commons.vfs2.VfsLog;
40 import org.apache.commons.vfs2.provider.AbstractFileName;
41 import org.apache.commons.vfs2.provider.AbstractFileSystem;
42 import org.apache.commons.vfs2.provider.UriParser;
43
44
45
46
47 public class ZipFileSystem extends AbstractFileSystem {
48
49 private static final char[] ENC = {'!'};
50
51 private static final Log LOG = LogFactory.getLog(ZipFileSystem.class);
52
53 private final File file;
54 private final Charset charset;
55 private ZipFile zipFile;
56
57
58
59
60 private final Map<FileName, FileObject> cache = new HashMap<>();
61
62
63
64
65
66
67
68
69
70 public ZipFileSystem(final AbstractFileName rootFileName, final FileObject parentLayer, final FileSystemOptions fileSystemOptions)
71 throws FileSystemException {
72 super(rootFileName, parentLayer, fileSystemOptions);
73
74
75 file = parentLayer.getFileSystem().replicateFile(parentLayer, Selectors.SELECT_SELF);
76 charset = ZipFileSystemConfigBuilder.getInstance().getCharset(fileSystemOptions);
77
78
79 if (!file.exists()) {
80
81 zipFile = null;
82 }
83 }
84
85
86
87
88 @Override
89 protected void addCapabilities(final Collection<Capability> caps) {
90 caps.addAll(ZipFileProvider.capabilities);
91 }
92
93
94
95
96 @Override
97 protected FileObject createFile(final AbstractFileName name) throws FileSystemException {
98
99 return new ZipFileObject(name, null, this, false);
100 }
101
102
103
104
105
106
107
108
109 protected ZipFile createZipFile(final File file) throws FileSystemException {
110 try {
111 return new ZipFile(file, charset);
112 } catch (final IOException ioe) {
113 throw new FileSystemException("vfs.provider.zip/open-zip-file.error", file, ioe);
114 }
115 }
116
117
118
119
120
121
122
123
124
125 protected ZipFileObject createZipFileObject(final AbstractFileName fileName, final ZipEntry entry) throws FileSystemException {
126 return new ZipFileObject(fileName, entry, this, true);
127 }
128
129 @Override
130 protected void doCloseCommunicationLink() {
131
132 try {
133 IOUtils.close(zipFile);
134 zipFile = null;
135 } catch (final IOException e) {
136
137 VfsLog.warn(getLogger(), LOG, "vfs.provider.zip/close-zip-file.error :" + file, e);
138 }
139 }
140
141
142
143
144
145
146 protected Charset getCharset() {
147 return charset;
148 }
149
150
151
152
153 @Override
154 protected FileObject getFileFromCache(final FileName name) {
155 return cache.get(name);
156 }
157
158
159
160
161
162
163
164 protected ZipFile getZipFile() throws FileSystemException {
165 if (zipFile == null && file.exists()) {
166 zipFile = createZipFile(file);
167 }
168 return zipFile;
169 }
170
171 @Override
172 public void init() throws FileSystemException {
173 super.init();
174
175 try {
176
177 final Enumeration<? extends ZipEntry> entries = getZipFile().entries();
178 while (entries.hasMoreElements()) {
179 final ZipEntry entry = entries.nextElement();
180 final AbstractFileName name = (AbstractFileName) getFileSystemManager().resolveName(getRootName(),
181 UriParser.encode(entry.getName(), ENC));
182
183
184 ZipFileObject fileObj;
185 if (entry.isDirectory() && getFileFromCache(name) != null) {
186 fileObj = (ZipFileObject) getFileFromCache(name);
187 fileObj.setZipEntry(entry);
188 continue;
189 }
190
191 fileObj = createZipFileObject(name, entry);
192 putFileToCache(fileObj);
193
194
195
196 ZipFileObject parent;
197 for (AbstractFileName parentName = (AbstractFileName) name
198 .getParent(); parentName != null; fileObj = parent, parentName = (AbstractFileName) parentName
199 .getParent()) {
200
201 parent = (ZipFileObject) getFileFromCache(parentName);
202 if (parent == null) {
203 parent = createZipFileObject(parentName, null);
204 putFileToCache(parent);
205 }
206
207
208 parent.attachChild(fileObj.getName());
209 }
210 }
211 } finally {
212 closeCommunicationLink();
213 }
214 }
215
216
217
218
219 @Override
220 protected void putFileToCache(final FileObject file) {
221 cache.put(file.getName(), file);
222 }
223
224
225
226
227 @Override
228 protected void removeFileFromCache(final FileName name) {
229 cache.remove(name);
230 }
231
232 @Override
233 public String toString() {
234 return super.toString() + " for " + file;
235 }
236
237
238
239
240
241 }