Search in sources :

Example 61 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project gradle by gradle.

the class TarTaskOutputPacker method unpack.

private UnpackResult unpack(SortedSet<ResolvedTaskOutputFilePropertySpec> propertySpecs, TarArchiveInputStream tarInput, TaskOutputOriginReader readOriginAction) throws IOException {
    Map<String, ResolvedTaskOutputFilePropertySpec> propertySpecsMap = Maps.uniqueIndex(propertySpecs, new Function<TaskFilePropertySpec, String>() {

        @Override
        public String apply(TaskFilePropertySpec propertySpec) {
            return propertySpec.getPropertyName();
        }
    });
    TarArchiveEntry tarEntry;
    OriginTaskExecutionMetadata originMetadata = null;
    ImmutableListMultimap.Builder<String, FileSnapshot> propertyFileSnapshots = ImmutableListMultimap.builder();
    long entries = 0;
    while ((tarEntry = tarInput.getNextTarEntry()) != null) {
        ++entries;
        String path = tarEntry.getName();
        if (path.equals(METADATA_PATH)) {
            // handle origin metadata
            originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
        } else {
            // handle output property
            Matcher matcher = PROPERTY_PATH.matcher(path);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached result format error, invalid contents: " + path);
            }
            String propertyName = unescape(matcher.group(2));
            ResolvedTaskOutputFilePropertySpec propertySpec = propertySpecsMap.get(propertyName);
            if (propertySpec == null) {
                throw new IllegalStateException(String.format("No output property '%s' registered", propertyName));
            }
            boolean outputMissing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            unpackPropertyEntry(propertySpec, tarInput, tarEntry, childPath, outputMissing, propertyFileSnapshots);
        }
    }
    if (originMetadata == null) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }
    return new UnpackResult(originMetadata, entries, propertyFileSnapshots.build());
}
Also used : Matcher(java.util.regex.Matcher) TaskFilePropertySpec(org.gradle.api.internal.tasks.TaskFilePropertySpec) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) DirectoryFileSnapshot(org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot) FileSnapshot(org.gradle.api.internal.changedetection.state.FileSnapshot) RegularFileSnapshot(org.gradle.api.internal.changedetection.state.RegularFileSnapshot) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 62 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project gradle by gradle.

the class CommonsTarPacker method unpack.

@Override
public void unpack(DataSource input, DataTargetFactory targetFactory) throws IOException {
    TarArchiveInputStream tarInput = new TarArchiveInputStream(input.openInput());
    while (true) {
        TarArchiveEntry entry = tarInput.getNextTarEntry();
        if (entry == null) {
            break;
        }
        PackerUtils.unpackEntry(entry.getName(), tarInput, buffer, targetFactory);
    }
    tarInput.close();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 63 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project meecrowave by apache.

the class MeecrowaveBundleMojo method tarGz.

private void tarGz(final TarArchiveOutputStream tarGz, final File f, final Path prefix) throws IOException {
    final String path = prefix.relativize(f.toPath()).toString().replace(File.separator, "/");
    final TarArchiveEntry archiveEntry = new TarArchiveEntry(f, path);
    if (isSh(path)) {
        archiveEntry.setMode(0755);
    }
    tarGz.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        tarGz.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                tarGz(tarGz, child, prefix);
            }
        }
    } else {
        Files.copy(f.toPath(), tarGz);
        tarGz.closeArchiveEntry();
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 64 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project Lucee by lucee.

the class CompressUtil method extractTar.

private static void extractTar(Resource tarFile, Resource targetDir) throws IOException {
    if (!targetDir.exists() || !targetDir.isDirectory())
        throw new IOException(targetDir + " is not a existing directory");
    if (!tarFile.exists())
        throw new IOException(tarFile + " is not a existing file");
    if (tarFile.isDirectory()) {
        Resource[] files = tarFile.listResources(new ExtensionResourceFilter("tar"));
        if (files == null)
            throw new IOException("directory " + tarFile + " is empty");
        extract(FORMAT_TAR, files, targetDir);
        return;
    }
    // read the zip file and build a query from its contents
    TarArchiveInputStream tis = null;
    try {
        tis = new TarArchiveInputStream(IOUtil.toBufferedInputStream(tarFile.getInputStream()));
        TarArchiveEntry entry;
        int mode;
        while ((entry = tis.getNextTarEntry()) != null) {
            // print.ln(entry);
            Resource target = targetDir.getRealResource(entry.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                Resource parent = target.getParentResource();
                if (!parent.exists())
                    parent.mkdirs();
                IOUtil.copy(tis, target, false);
            }
            target.setLastModified(entry.getModTime().getTime());
            mode = entry.getMode();
            if (mode > 0)
                target.setMode(mode);
        // tis.closeEntry() ;
        }
    } finally {
        IOUtil.closeEL(tis);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Resource(lucee.commons.io.res.Resource) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) lucee.aprint(lucee.aprint)

Example 65 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project linuxtools by eclipse.

the class CopyFromContainerCommandHandler method performCopyFromContainer.

private void performCopyFromContainer(final IDockerConnection connection, final IDockerContainer container, final String target, final List<ContainerFileProxy> files) {
    final Job copyFromContainerJob = new Job(CommandMessages.getFormattedString(COPY_FROM_CONTAINER_JOB_TITLE, container.name())) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(CommandMessages.getString(COPY_FROM_CONTAINER_JOB_TASK), files.size());
            try (Closeable token = ((DockerConnection) connection).getOperationToken()) {
                for (ContainerFileProxy proxy : files) {
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return Status.CANCEL_STATUS;
                    }
                    try {
                        monitor.setTaskName(CommandMessages.getFormattedString(COPY_FROM_CONTAINER_JOB_SUBTASK, proxy.getFullPath()));
                        monitor.worked(1);
                        InputStream in = ((DockerConnection) connection).copyContainer(token, container.id(), proxy.getLink());
                        /*
							 * The input stream from copyContainer might be
							 * incomplete or non-blocking so we should wrap it
							 * in a stream that is guaranteed to block until
							 * data is available.
							 */
                        TarArchiveInputStream k = new TarArchiveInputStream(new BlockingInputStream(in));
                        TarArchiveEntry te = null;
                        while ((te = k.getNextTarEntry()) != null) {
                            long size = te.getSize();
                            IPath path = new Path(target);
                            path = path.append(te.getName());
                            File f = new File(path.toOSString());
                            if (te.isDirectory()) {
                                f.mkdir();
                                continue;
                            } else {
                                f.createNewFile();
                            }
                            FileOutputStream os = new FileOutputStream(f);
                            int bufferSize = ((int) size > 4096 ? 4096 : (int) size);
                            byte[] barray = new byte[bufferSize];
                            int result = -1;
                            while ((result = k.read(barray, 0, bufferSize)) > -1) {
                                if (monitor.isCanceled()) {
                                    monitor.done();
                                    k.close();
                                    os.close();
                                    return Status.CANCEL_STATUS;
                                }
                                os.write(barray, 0, result);
                            }
                            os.close();
                        }
                        k.close();
                    } catch (final DockerException e) {
                        Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), CommandMessages.getFormattedString(ERROR_COPYING_FROM_CONTAINER, proxy.getLink(), container.name()), e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
                    }
                }
            } catch (InterruptedException e) {
            // do nothing
            } catch (IOException e) {
                Activator.log(e);
            } catch (DockerException e1) {
                Activator.log(e1);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    copyFromContainerJob.schedule();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IPath(org.eclipse.core.runtime.IPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) Closeable(java.io.Closeable) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ContainerFileProxy(org.eclipse.linuxtools.internal.docker.core.ContainerFileProxy) FileOutputStream(java.io.FileOutputStream) Job(org.eclipse.core.runtime.jobs.Job) File(java.io.File)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)65 File (java.io.File)33 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)31 FileInputStream (java.io.FileInputStream)24 FileOutputStream (java.io.FileOutputStream)20 IOException (java.io.IOException)20 InputStream (java.io.InputStream)15 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)15 BufferedInputStream (java.io.BufferedInputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 BufferedOutputStream (java.io.BufferedOutputStream)8 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 OutputStream (java.io.OutputStream)6 GZIPInputStream (java.util.zip.GZIPInputStream)5 ZipFile (java.util.zip.ZipFile)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3