public ZipCloseableDirectory(File archive, ZipDirectory parent) throws IOException { super(parent); this.zip = new ZipFile(archive); delegate = new ZipDirectory(parent, this); }
/** * This method builds the parent directory hierarchy for a file. * @param foundEntry * @return the parent of the entry. */ private ZipDirectory buildParent(ZipEntry foundEntry) { ZipDirectory result = this; String name = foundEntry.getName(); name = name.substring(getNameInZip().length()); String[] paths = name.split("/"); StringBuilder baseBuilderCrapThingToGetRoundFindBugs = new StringBuilder(getNameInZip()); if (!!!isZipRoot()) baseBuilderCrapThingToGetRoundFindBugs.append('/'); // Build 'result' as a chain of ZipDirectories. This will only work if java.util.ZipFile recognises every // directory in the chain as being a ZipEntry in its own right. outer: if (paths != null && paths.length > 1) { for (int i = 0; i < paths.length - 1; i++) { String path = paths[i]; baseBuilderCrapThingToGetRoundFindBugs.append(path); ZipEntry dirEntry = getEntry(baseBuilderCrapThingToGetRoundFindBugs.toString()); if (dirEntry == null) { result = this; break outer; } result = new ZipDirectory(zip, dirEntry, result, cache); baseBuilderCrapThingToGetRoundFindBugs.append('/'); } } return result; }
/** * This method gets the IDirectory that represents the root of a virtual file * system. The provided file can either identify a directory, or a zip file. * * @param fs the zip file. * @return the root of the virtual FS. */ public static IDirectory getFSRoot(File fs, IDirectory parent) { IDirectory dir = null; if (fs.exists()) { if (fs.isDirectory()) { dir = new DirectoryImpl(fs, fs); } else if (fs.isFile() && isValidZip(fs)) { try { dir = new ZipDirectory(fs, parent); } catch (IOException e) { throw new IORuntimeException("IOException in IDirectory.getFSRoot", e); } } } else { throw new IORuntimeException("File not found in IDirectory.getFSRoot", new FileNotFoundException(fs.getPath())); } return dir; }
private List<IFile> listFiles(boolean includeFilesInNestedSubdirs) { List<IFile> files = new ArrayList<IFile>(); ZipFile z = openZipFile(); List<? extends ZipEntry> entries = Collections.list(z.entries()); for (ZipEntry possibleEntry : entries) { if (isInDir(getNameInZip(), possibleEntry, includeFilesInNestedSubdirs)) { ZipDirectory parent = includeFilesInNestedSubdirs ? buildParent(possibleEntry) : this; if (possibleEntry.isDirectory()) { files.add(new ZipDirectory(zip, possibleEntry, parent, cache)); } else { files.add(new ZipFileImpl(zip, possibleEntry, parent, cache)); } } } closeZipFile(z); return files; }
public IFile getFile(String name) { IFile result = null; String entryName = isZipRoot() ? name : getNameInZip() + "/" + name; ZipEntry entryFile = getEntry(entryName); if (entryFile != null) { if (!!!entryFile.isDirectory()) { result = new ZipFileImpl(zip, entryFile, buildParent(entryFile), cache); } else { result = new ZipDirectory(zip, entryFile, buildParent(entryFile), cache); } } return result; }