package org.apache.commons.vfs2.provider.zip;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs2.Capability;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.VfsLog;
import org.apache.commons.vfs2.provider.AbstractFileName;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.provider.UriParser;
public class ZipFileSystem extends AbstractFileSystem {
private static final Log LOG = LogFactory.getLog(ZipFileSystem.class);
private final File file;
private final Charset charset;
private ZipFile zipFile;
private final Map<FileName, FileObject> cache = new HashMap<>();
public ZipFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
final FileSystemOptions fileSystemOptions) throws FileSystemException {
super(rootName, parentLayer, fileSystemOptions);
file = parentLayer.getFileSystem().replicateFile(parentLayer, Selectors.SELECT_SELF);
this.charset = ZipFileSystemConfigBuilder.getInstance().getCharset(fileSystemOptions);
if (!file.exists()) {
zipFile = null;
return;
}
}
@Override
public void init() throws FileSystemException {
super.init();
try {
final List<ZipFileObject> strongRef = new ArrayList<>(getZipFile().size());
final Enumeration<? extends ZipEntry> entries = getZipFile().entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
final AbstractFileName name = (AbstractFileName) getFileSystemManager().resolveName(getRootName(),
UriParser.encode(entry.getName()));
ZipFileObject fileObj;
if (entry.isDirectory() && getFileFromCache(name) != null) {
fileObj = (ZipFileObject) getFileFromCache(name);
fileObj.setZipEntry(entry);
continue;
}
fileObj = createZipFileObject(name, entry);
putFileToCache(fileObj);
strongRef.add(fileObj);
fileObj.holdObject(strongRef);
ZipFileObject parent;
for (AbstractFileName parentName = (AbstractFileName) name
.getParent(); parentName != null; fileObj = parent, parentName = (AbstractFileName) parentName
.getParent()) {
parent = (ZipFileObject) getFileFromCache(parentName);
if (parent == null) {
parent = createZipFileObject(parentName, null);
putFileToCache(parent);
strongRef.add(parent);
parent.holdObject(strongRef);
}
parent.attachChild(fileObj.getName());
}
}
} finally {
closeCommunicationLink();
}
}
protected ZipFile getZipFile() throws FileSystemException {
if (zipFile == null && this.file.exists()) {
this.zipFile = createZipFile(this.file);
}
return zipFile;
}
protected ZipFileObject createZipFileObject(final AbstractFileName name, final ZipEntry entry)
throws FileSystemException {
return new ZipFileObject(name, entry, this, true);
}
protected ZipFile createZipFile(final File file) throws FileSystemException {
try {
return charset == null ? new ZipFile(file) : new ZipFile(file, charset);
} catch (final IOException ioe) {
throw new FileSystemException("vfs.provider.zip/open-zip-file.error", file, ioe);
}
}
@Override
protected void doCloseCommunicationLink() {
try {
if (zipFile != null) {
zipFile.close();
zipFile = null;
}
} catch (final IOException e) {
VfsLog.warn(getLogger(), LOG, "vfs.provider.zip/close-zip-file.error :" + file, e);
}
}
@Override
protected void addCapabilities(final Collection<Capability> caps) {
caps.addAll(ZipFileProvider.capabilities);
}
@Override
protected FileObject createFile(final AbstractFileName name) throws FileSystemException {
return new ZipFileObject(name, null, this, false);
}
@Override
protected void putFileToCache(final FileObject file) {
cache.put(file.getName(), file);
}
protected Charset getCharset() {
return charset;
}
@Override
protected FileObject getFileFromCache(final FileName name) {
return cache.get(name);
}
@Override
protected void removeFileFromCache(final FileName name) {
cache.remove(name);
}
@Override
public String toString() {
return super.toString() + " for " + file;
}
}