Search in sources :

Example 1 with IORuntimeException

use of org.apache.aries.util.IORuntimeException in project aries by apache.

the class FileSystemImpl method getFSRoot.

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);
}
Also used : IORuntimeException(org.apache.aries.util.IORuntimeException) FileOutputStream(java.io.FileOutputStream) IDirectory(org.apache.aries.util.filesystem.IDirectory) IOException(java.io.IOException) IFile(org.apache.aries.util.filesystem.IFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with IORuntimeException

use of org.apache.aries.util.IORuntimeException in project aries by apache.

the class FileSystemImpl method isValidZip.

/**
 * 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);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) IORuntimeException(org.apache.aries.util.IORuntimeException) IOException(java.io.IOException)

Example 3 with IORuntimeException

use of org.apache.aries.util.IORuntimeException in project aries by apache.

the class NestedZipDirectory method getFile.

public IFile getFile(String name) {
    Map<String, ZipEntry> entries = new HashMap<String, ZipEntry>();
    ZipEntry ze;
    if (cache != null && !!!cache.isClosed()) {
        ZipFile zip = cache.getZipFile();
        String[] segments = name.split("/");
        StringBuilder path = new StringBuilder();
        for (String s : segments) {
            path.append(s).append('/');
            ZipEntry p = zip.getEntry(path.toString());
            if (p != null)
                entries.put(path.toString(), p);
        }
        ze = zip.getEntry(name);
    } else {
        ZipInputStream zis = null;
        try {
            zis = new ZipInputStream(archive.open());
            ze = zis.getNextEntry();
            while (ze != null && !!!ze.getName().equals(name)) {
                if (name.startsWith(ze.getName()))
                    entries.put(ze.getName(), ze);
                ze = zis.getNextEntry();
            }
        } catch (IOException e) {
            throw new IORuntimeException("IOException reading nested ZipFile", e);
        } finally {
            IOUtils.close(zis);
        }
    }
    if (ze != null) {
        NestedZipDirectory parent = buildParent(ze, entries);
        if (ze.isDirectory())
            return new NestedZipDirectory(archive, ze, parent, cache);
        else
            return new NestedZipFile(archive, ze, parent, cache);
    } else {
        return null;
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipFile(java.util.zip.ZipFile) IORuntimeException(org.apache.aries.util.IORuntimeException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 4 with IORuntimeException

use of org.apache.aries.util.IORuntimeException in project aries by apache.

the class NestedZipDirectory method getAllEntries.

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);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) IORuntimeException(org.apache.aries.util.IORuntimeException) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with IORuntimeException

use of org.apache.aries.util.IORuntimeException in project aries by apache.

the class FileSystemTest method testInvalidFSRoot.

@Test
public void testInvalidFSRoot() throws IOException {
    File baseDir = new File(getTestResourceDir(), "/app1");
    File manifest = new File(baseDir, "META-INF/APPLICATION.MF");
    try {
        FileSystem.getFSRoot(manifest);
        fail("Should have thrown an IORuntimeException");
    } catch (IORuntimeException e) {
    // good!
    }
}
Also used : IORuntimeException(org.apache.aries.util.IORuntimeException) ZipFile(java.util.zip.ZipFile) File(java.io.File) Test(org.junit.Test)

Aggregations

IORuntimeException (org.apache.aries.util.IORuntimeException)6 IOException (java.io.IOException)5 ZipFile (java.util.zip.ZipFile)4 ZipEntry (java.util.zip.ZipEntry)3 File (java.io.File)2 ZipInputStream (java.util.zip.ZipInputStream)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 JarInputStream (java.util.jar.JarInputStream)1 Manifest (java.util.jar.Manifest)1 IDirectory (org.apache.aries.util.filesystem.IDirectory)1 IFile (org.apache.aries.util.filesystem.IFile)1 Test (org.junit.Test)1