Search in sources :

Example 31 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project kontraktor by RuedigerMoeller.

the class JNPM method unTar.

private static List<File> unTar(final InputStream is, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    final List<File> untaredFiles = new LinkedList<File>();
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        String name = entry.getName();
        if (name.startsWith("package/"))
            name = name.substring("package/".length());
        final File outputFile = new File(outputDir, name);
        if (entry.isDirectory()) {
            if (!outputFile.mkdirs()) {
                throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
            }
        } else {
            outputFile.getParentFile().mkdirs();
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();
    return untaredFiles;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 32 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project incubator-gobblin by apache.

the class AzkabanJobHelper method addFilesToZip.

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "OBL_UNSATISFIED_OBLIGATION", justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs")
private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException {
    try {
        @Cleanup OutputStream archiveStream = new FileOutputStream(zipFile);
        @Cleanup ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);
        for (File fileToAdd : filesToAdd) {
            ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName());
            archive.putArchiveEntry(entry);
            @Cleanup BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd));
            IOUtils.copy(input, archive);
            archive.closeArchiveEntry();
        }
        archive.finish();
    } catch (ArchiveException e) {
        throw new IOException("Issue with creating archive", e);
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Cleanup(lombok.Cleanup) File(java.io.File) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) FileInputStream(java.io.FileInputStream)

Example 33 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project packr by libgdx.

the class ArchiveUtils method extractGenericArchive.

/**
 * Extracts an archive using {@link ArchiveStreamFactory#createArchiveInputStream(InputStream)} with no special handling of symbolic links or file
 * permissions.
 *
 * @param inputStream        the archive input stream
 * @param extractToDirectory the directory to extract the archive into
 * @throws ArchiveException if an archive error occurs
 * @throws IOException      if an IO error occurs
 */
private static void extractGenericArchive(InputStream inputStream, Path extractToDirectory) throws ArchiveException, IOException {
    final ArchiveInputStream archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(inputStream);
    ArchiveEntry entry;
    while ((entry = archiveInputStream.getNextEntry()) != null) {
        if (!archiveInputStream.canReadEntryData(entry)) {
            LOG.error("Failed to read archive entry " + entry);
            continue;
        }
        Path entryExtractPath = extractToDirectory.resolve(getEntryAsPath(entry));
        if (entry.isDirectory()) {
            Files.createDirectories(entryExtractPath);
        } else {
            Files.createDirectories(entryExtractPath.getParent());
            Files.copy(archiveInputStream, entryExtractPath, StandardCopyOption.REPLACE_EXISTING);
        }
        Files.setLastModifiedTime(entryExtractPath, FileTime.fromMillis(entry.getLastModifiedDate().getTime()));
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry)

Example 34 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project tomee by apache.

the class ExecMojo method createExecutableJar.

private void createExecutableJar() throws Exception {
    mkdirs(execFile.getParentFile());
    final Properties config = new Properties();
    config.put("distribution", distributionName);
    config.put("workingDir", runtimeWorkingDir);
    config.put("command", DEFAULT_SCRIPT.equals(script) ? (skipArchiveRootFolder ? "" : catalinaBase.getName() + "/") + DEFAULT_SCRIPT : script);
    final List<String> jvmArgs = generateJVMArgs();
    final String catalinaOpts = toString(jvmArgs, " ");
    config.put("catalinaOpts", catalinaOpts);
    config.put("timestamp", Long.toString(System.currentTimeMillis()));
    // java only
    final String cp = getAdditionalClasspath();
    if (cp != null) {
        config.put("additionalClasspath", cp);
    }
    config.put("shutdownCommand", tomeeShutdownCommand);
    int i = 0;
    boolean encodingSet = catalinaOpts.contains("-Dfile.encoding");
    for (final String jvmArg : jvmArgs) {
        config.put("jvmArg." + i++, jvmArg);
        encodingSet = encodingSet || jvmArg.contains("-Dfile.encoding");
    }
    if (!encodingSet) {
        // forcing encoding for launched process to be able to read conf files
        config.put("jvmArg." + i, "-Dfile.encoding=UTF-8");
    }
    if (preTasks != null) {
        config.put("preTasks", toString(preTasks, ","));
    }
    if (postTasks != null) {
        config.put("postTasks", toString(postTasks, ","));
    }
    config.put("waitFor", Boolean.toString(waitFor));
    // create an executable jar with main runner and zipFile
    final FileOutputStream fileOutputStream = new FileOutputStream(execFile);
    final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, fileOutputStream);
    {
        // distrib
        os.putArchiveEntry(new JarArchiveEntry(distributionName));
        final FileInputStream in = new FileInputStream(zipFile);
        try {
            IOUtils.copy(in, os);
            os.closeArchiveEntry();
        } finally {
            IOUtil.close(in);
        }
    }
    {
        // config
        os.putArchiveEntry(new JarArchiveEntry("configuration.properties"));
        final StringWriter writer = new StringWriter();
        config.store(writer, "");
        IOUtils.copy(new ByteArrayInputStream(writer.toString().getBytes("UTF-8")), os);
        os.closeArchiveEntry();
    }
    {
        // Manifest
        final Manifest manifest = new Manifest();
        final Manifest.Attribute mainClassAtt = new Manifest.Attribute();
        mainClassAtt.setName("Main-Class");
        mainClassAtt.setValue(runnerClass);
        manifest.addConfiguredAttribute(mainClassAtt);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        manifest.write(baos);
        os.putArchiveEntry(new JarArchiveEntry("META-INF/MANIFEST.MF"));
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), os);
        os.closeArchiveEntry();
    }
    {
        // Main + utility
        for (final Class<?> clazz : asList(ExecRunner.class, Files.class, Files.PatternFileFilter.class, Files.DeleteThread.class, Files.FileRuntimeException.class, Files.FileDoesNotExistException.class, Files.NoopOutputStream.class, LoaderRuntimeException.class, Pipe.class, IO.class, Zips.class, JarLocation.class, RemoteServer.class, RemoteServer.CleanUpThread.class, OpenEJBRuntimeException.class, Join.class, QuickServerXmlParser.class, Options.class, Options.NullLog.class, Options.TomEEPropertyAdapter.class, Options.NullOptions.class, Options.Log.class, JavaSecurityManagers.class)) {
            addToJar(os, clazz);
        }
    }
    addClasses(additionalClasses, os);
    addClasses(preTasks, os);
    addClasses(postTasks, os);
    IOUtil.close(os);
    IOUtil.close(fileOutputStream);
}
Also used : Options(org.apache.openejb.loader.Options) JarLocation(org.apache.openejb.loader.JarLocation) Properties(java.util.Properties) ExecRunner(org.apache.openejb.maven.plugin.runner.ExecRunner) StringWriter(java.io.StringWriter) LoaderRuntimeException(org.apache.openejb.loader.LoaderRuntimeException) JavaSecurityManagers(org.apache.openejb.util.JavaSecurityManagers) Files(org.apache.openejb.loader.Files) RemoteServer(org.apache.openejb.config.RemoteServer) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) IO(org.apache.openejb.loader.IO) Join(org.apache.openejb.util.Join) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Pipe(org.apache.openejb.util.Pipe) Manifest(org.codehaus.plexus.archiver.jar.Manifest) FileInputStream(java.io.FileInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) QuickServerXmlParser(org.apache.tomee.util.QuickServerXmlParser) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Zips(org.apache.openejb.loader.Zips) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream)

Example 35 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project winery by eclipse.

the class Generator method packageProject.

/**
 * Packages the generated project into a ZIP file which is stored in outDir and has the name of the Project.
 *
 * @param projectDir the dir where the generated files reside in
 * @return ZIP file
 */
private File packageProject(Path projectDir) {
    try {
        File packagedProject = new File(this.outDir.getAbsoluteFile() + File.separator + this.name + ".zip");
        FileOutputStream fileOutputStream = new FileOutputStream(packagedProject);
        final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fileOutputStream);
        this.addFilesRecursively(projectDir.toAbsolutePath().toFile(), projectDir.toAbsolutePath() + File.separator, zos);
        zos.finish();
        zos.close();
        return packagedProject;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) IOException(java.io.IOException)

Aggregations

ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)38 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)18 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)14 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)11 InputStream (java.io.InputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 LinkedList (java.util.LinkedList)9 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)9 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)8 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)8 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)8 OutputStream (java.io.OutputStream)7 GZIPInputStream (java.util.zip.GZIPInputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Path (java.nio.file.Path)4