public SpecialZipInputStream(ZipEntry anEntry){ try{ this.zipFile = openZipFile(); this.is = zipFile.getInputStream(anEntry); } catch (ZipException e) { throw new IORuntimeException("ZipException in SpecialZipInputStream()", e); } catch (IOException e) { throw new IORuntimeException("IOException in SpecialZipInputStream()", e); } }
/** * Check whether a file is actually a valid zip * @param zip * @return */ public static boolean isValidZip(File zip) { try { ZipFile zf = new ZipFile(zip); zf.close(); return true; } catch (IOException e) { throw new IORuntimeException("Not a valid zip: "+zip, e); } }
public ICloseableDirectory toCloseable() { try { return new NestedCloseableDirectory(archive, this); } catch (IOException e) { throw new IORuntimeException("Exception while creating extracted version of nested zip file", e); } } }
public ICloseableDirectory toCloseable() { try { return new ZipCloseableDirectory(zip, this); } catch (IOException e) { throw new IORuntimeException("IOException opening zip file: " + this, e); } } }
void closeZipFile(ZipFile z){ if (cache != null && cache.getZipFile() == z) { // do nothing } else { try{ z.close(); } catch (IOException e) { throw new IORuntimeException("IOException in ZipFileImpl.closeZipFile", e); } } }
ZipFile openZipFile(){ ZipFile z = null; if (cache != null && !!!cache.isClosed()) { z = cache.getZipFile(); } else { try { z = new ZipFile(zip); } catch (IOException e) { throw new IORuntimeException("IOException in ZipFileImpl.openZipFile", e); } } return z; }
/** * Check whether a file is actually a valid zip * @param zip * @return */ public static boolean isValidZip(IFile zip) { ZipInputStream zis = null; try { // just opening the stream ain't enough, we have to check the first entry zis = new ZipInputStream(zip.open()); return zis.getNextEntry() != null; } catch (IOException e) { throw new IORuntimeException("Not a valid zip: "+zip, e); } finally { IOUtils.close(zis); } }
/** * 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; }
throw new IORuntimeException("IOException in BundleManifest.fromBundle(File)", e); throw new IORuntimeException("IOException in BundleManifest.fromBundle(File)", e);
private List<? extends ZipEntry> getAllEntries() { if (cache != null && !!!cache.isClosed()) { return Collections.list(cache.getZipFile().entries()); } else { ZipInputStream zis = null; try { zis = new ZipInputStream(archive.open()); List<ZipEntry> result = new ArrayList<ZipEntry>(); ZipEntry entry = zis.getNextEntry(); while (entry != null) { result.add(entry); entry = zis.getNextEntry(); } return result; } catch (IOException e) { throw new IORuntimeException("IOException reading nested ZipFile", e); } finally { IOUtils.close(zis); } } }
throw new IORuntimeException("IOException reading nested ZipFile", e); } finally { IOUtils.close(zis);
/** * Read a manifest from a jar input stream. This will find the manifest even if it is NOT * the first file in the archive. * * @param is the jar input stream * @return the bundle manifest */ public static BundleManifest fromBundle(InputStream is) { JarInputStream jarIs = null; try { jarIs = new JarInputStream(is); Manifest m = jarIs.getManifest(); if (m != null) return new BundleManifest(m); else { ZipEntry entry; while ((entry = jarIs.getNextEntry()) != null) { if (entry.getName().equals(MANIFEST_PATH)) return new BundleManifest(jarIs); } return null; } } catch (IOException e) { throw new IORuntimeException("IOException in BundleManifest()", e); } finally { IOUtils.close(jarIs); } }
public static ICloseableDirectory getFSRoot(InputStream is) { File tempFile = null; try { tempFile = File.createTempFile("inputStreamExtract", ".zip"); } catch (IOException e1) { throw new IORuntimeException("IOException in IDirectory.getFSRoot", e1); } FileOutputStream fos = null; try { fos = new FileOutputStream(tempFile); IOUtils.copy(is, fos); } catch (IOException e) { return null; } finally { IOUtils.close(fos); } IDirectory dir = getFSRoot(tempFile, null); if(dir == null) return null; else return new InputStreamClosableDirectory(dir, tempFile); } }
/** * Retrieve a BundleManifest from the given jar file * * @param f the bundle jar file * @return the bundle manifest */ public static BundleManifest fromBundle(IFile f) { InputStream is = null; try { if (f.isDirectory()) { IFile manFile = f.convert().getFile(MANIFEST_PATH); if (manFile != null) return new BundleManifest(manFile.open()); else return null; } else { is = f.open(); return fromBundle(is); } } catch (IOException e) { throw new IORuntimeException("IOException in BundleManifest.fromBundle(IFile)", e); } finally { IOUtils.close(is); } }