package net.sourceforge.javautil.common.io.impl;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.IVirtualArtifact;
import net.sourceforge.javautil.common.io.VirtualArtifactNotFoundException;
import net.sourceforge.javautil.common.io.IVirtualDirectory;
import net.sourceforge.javautil.common.io.VirtualDirectoryAbstract;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.IVirtualPath;
/**
* This allows one to treat a zip file as a virtual directory.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: ZippedDirectory.java 2297 2010-06-16 00:13:14Z ponderator $
*/
public class ZippedDirectory extends ZippedDirectoryAbstract {
protected final SystemFile realFile;
protected final ZipFile zipFile;
protected List<ZipEntry> entries;
public ZippedDirectory(SystemFile file) {
super(null, new SimplePath(""), "");
this.realFile = file;
try {
this.zipFile = new ZipFile(file.getRealArtifact());
} catch (ZipException e) {
throw ThrowableManagerRegistry.caught(e);
} catch (IOException e) {
throw ThrowableManagerRegistry.caught(e);
}
}
/**
* Clear the in-memory table of zip entries and close the {@link ZipFile} for this directory.
*/
public void close () {
try {
this.entries.clear();
this.zipFile.close();
} catch (IOException e) {
throw ThrowableManagerRegistry.caught(e);
}
}
@Override protected SystemFile getRealFile() { return realFile; }
@Override protected ZipFile getZipFile() { return zipFile; }
@Override protected List<ZipEntry> getEntries() {
if (this.entries == null) {
synchronized (this) {
if (this.entries == null) {
this.entries = new ArrayList<ZipEntry>();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
this.entries.add(entries.nextElement());
}
}
}
}
return this.entries;
}
public boolean remove() {
this.close();
return this.realFile.remove();
}
}