package org.eclipse.osgi.storage.bundlefile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.osgi.internal.debug.Debug;
import org.eclipse.osgi.internal.messages.Msg;
import org.eclipse.osgi.storage.BundleInfo;
import org.eclipse.osgi.util.NLS;
public class ZipBundleFile extends CloseableBundleFile<ZipEntry> {
volatile ZipFile zipFile;
public ZipBundleFile(File basefile, BundleInfo.Generation generation, MRUBundleFileList mruList, Debug debug) throws IOException {
super(basefile, generation, mruList, debug);
if (!BundleFile.secureAction.exists(basefile))
throw new IOException(NLS.bind(Msg.ADAPTER_FILEEXIST_EXCEPTION, basefile));
}
@Override
protected void doOpen() throws IOException {
zipFile = BundleFile.secureAction.getZipFile(this.basefile);
}
private ZipEntry getZipEntry(String path) {
if (path.length() > 0 && path.charAt(0) == '/')
path = path.substring(1);
ZipEntry entry = zipFile.getEntry(path);
if (entry != null && entry.getSize() == 0 && !entry.isDirectory()) {
ZipEntry dirEntry = zipFile.getEntry(path + '/');
if (dirEntry != null)
entry = dirEntry;
}
return entry;
}
@Override
protected BundleEntry findEntry(String path) {
ZipEntry zipEntry = getZipEntry(path);
if (zipEntry == null) {
if (path.length() == 0 || path.charAt(path.length() - 1) == '/') {
if (containsDir(path))
return new DirZipBundleEntry(this, path);
}
return null;
}
return new ZipBundleEntry(zipEntry, this);
}
@Override
protected void doClose() throws IOException {
zipFile.close();
}
@Override
protected void postClose() {
zipFile = null;
}
@Override
protected InputStream doGetInputStream(ZipEntry entry) throws IOException {
return zipFile.getInputStream(entry);
}
@Override
protected Iterable<String> getPaths() {
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
return new Iterator<String>() {
@Override
public boolean hasNext() {
return entries.hasMoreElements();
}
@Override
public String next() {
ZipEntry entry = entries.nextElement();
return entry.getName();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}