Search in sources :

Example 26 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project stanbol by apache.

the class ConfigUtils method copyResource.

/**
     * Variant of the copyResource method that used an entry of an archive as source.
     * 
     * @param rootDir
     *            the directory used as target
     * @param archive
     *            the archive containing the parsed entry
     * @param entry
     *            the entry to copy to the target directory
     * @param context
     *            the context used to calculate the relative path of the resource within the target directory
     * @param override
     *            if an existing resource within the target directory should be deleted
     * @throws IOException
     *             in case of an error while reading or writing the resource
     */
private static void copyResource(File rootDir, ZipFile archive, ZipArchiveEntry entry, String context, boolean override) throws IOException {
    File file = prepairCopy(entry.getName(), rootDir, context);
    if (file != null) {
        boolean overrideState = false;
        if (file.exists() && override) {
            FileUtils.deleteQuietly(file);
            overrideState = true;
        }
        if (!file.exists()) {
            OutputStream os = null;
            InputStream is = null;
            try {
                os = FileUtils.openOutputStream(file);
                is = archive.getInputStream(entry);
                IOUtils.copy(is, os);
                log.debug(String.format(" > %s %s", overrideState ? "override" : "copy", file));
            } finally {
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(os);
            }
        }
    }
// else can not cppy logging already provided
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 27 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project stanbol by apache.

the class ResourceLoader method loadResource.

/**
     * Loads a resource from a file
     * @param file the file resource
     */
private void loadResource(String file) {
    synchronized (files) {
        //sync to files to avoid two threads loading the same file
        ResourceState state = files.get(file);
        if (state == null || state != ResourceState.REGISTERED) {
            log.info("Do not load File {} because of its state {} (null means removed from list)", file, state);
            //someone removed it in between
            return;
        } else {
            //set to loading
            setResourceState(file, ResourceState.LOADING, null);
        }
    }
    long startFile = System.currentTimeMillis();
    log.info(" > loading '{}' ...", file);
    String extension = FilenameUtils.getExtension(file);
    if (loadEntriesWithinZipArchives && ("zip".equalsIgnoreCase(extension) || "jar".equalsIgnoreCase(extension))) {
        log.info("  - processing {}-archive entries:", extension);
        ZipFile zipArchive;
        try {
            zipArchive = new ZipFile(file);
        } catch (IOException e) {
            zipArchive = null;
            setResourceState(file, ResourceState.ERROR, e);
        }
        if (zipArchive != null) {
            boolean isError = false;
            Enumeration<ZipArchiveEntry> entries = zipArchive.getEntries();
            while (entries.hasMoreElements()) {
                ZipArchiveEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    String entryName = entry.getName();
                    log.info("     o loading entry '{}'", entryName);
                    try {
                        ResourceState state = resourceImporter.importResource(zipArchive.getInputStream(entry), FilenameUtils.getName(entryName));
                        if (state == ResourceState.ERROR) {
                            isError = true;
                        }
                    } catch (IOException e) {
                        isError = true;
                    }
                }
            }
            //set the state for the Archive as a whole
            setResourceState(file, isError ? ResourceState.ERROR : ResourceState.LOADED, null);
        }
    } else {
        InputStream is;
        try {
            is = new FileInputStream(file);
            ResourceState state = resourceImporter.importResource(is, FilenameUtils.getName(file));
            setResourceState(file, state, null);
        } catch (FileNotFoundException e) {
            //during init it is checked that files exists and are files 
            //and there is read access so this can only happen if
            //someone deletes the file in between
            log.warn("Unable to load resource " + file, e);
            setResourceState(file, ResourceState.ERROR, e);
        } catch (IOException e) {
            log.error("Unable to load resource " + file, e);
            setResourceState(file, ResourceState.ERROR, e);
        } catch (Exception e) {
            log.error("Unable to load resource " + file, e);
            setResourceState(file, ResourceState.ERROR, e);
        }
    }
    log.info("   - completed in {} seconds", (System.currentTimeMillis() - startFile) / 1000);
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)27 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)21 IOException (java.io.IOException)10 File (java.io.File)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 ZipInputStream (java.util.zip.ZipInputStream)6 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 BufferedInputStream (java.io.BufferedInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 OutputStream (java.io.OutputStream)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)1