Search in sources :

Example 46 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project coprhd-controller by CoprHD.

the class CustomServicesAnsibleResourceDAO method getPlaybooks.

private StringSet getPlaybooks(final byte[] archive) {
    try (final TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(archive)))) {
        TarArchiveEntry entry = tarIn.getNextTarEntry();
        final StringSet playbooks = new StringSet();
        while (entry != null) {
            if (entry.isFile() && entry.getName().toLowerCase().endsWith(".yml")) {
                final java.nio.file.Path playbookPath = FileSystems.getDefault().getPath(entry.getName()).normalize();
                if (null != playbookPath && playbookPath.getNameCount() >= 0)
                    playbooks.add(playbookPath.toString());
            }
            entry = tarIn.getNextTarEntry();
        }
        return playbooks;
    } catch (final IOException e) {
        throw InternalServerErrorException.internalServerErrors.genericApisvcError("Invalid ansible archive. The archive needs to be in 'tar.gz' format. " + "Create the tar using the command 'tar -zcvf tar_name directory_path_to_tar' and then upload", e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StringSet(com.emc.storageos.db.client.model.StringSet) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 47 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project coprhd-controller by CoprHD.

the class CustomServicesLocalAnsibleExecution method uncompressArchive.

private void uncompressArchive(final byte[] ansibleArchive, final List<String> fileList, final List<String> pathList) {
    try (final TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(ansibleArchive)))) {
        TarArchiveEntry entry = tarIn.getNextTarEntry();
        while (entry != null) {
            final File curTarget = new File(orderDir, entry.getName());
            if (entry.isDirectory()) {
                curTarget.mkdirs();
            } else {
                final File parent = curTarget.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                try (final OutputStream out = new FileOutputStream(curTarget)) {
                    IOUtils.copy(tarIn, out);
                }
                // Add file name and file path for softlinks
                fileList.add(curTarget.getName());
                pathList.add(curTarget.getAbsolutePath().replaceFirst(CustomServicesConstants.CHROOT_DIR + "/", ""));
            }
            entry = tarIn.getNextTarEntry();
        }
    } catch (final IOException e) {
        ExecutionUtils.currentContext().logError("customServicesOperationExecution.logStatus", step.getId(), step.getFriendlyName(), "Invalid Ansible archive");
        logger.error("Exception:", e);
        throw InternalServerErrorException.internalServerErrors.genericApisvcError("Invalid Ansible archive", e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 48 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project java by kubernetes-client.

the class Copy method copyDirectoryFromPodAsync.

public Future<Integer> copyDirectoryFromPodAsync(String namespace, String pod, String container, String srcPath, Path destination) throws IOException, ApiException {
    final Process proc = this.exec(namespace, pod, new String[] { "sh", "-c", "tar cz - " + srcPath + " | base64" }, container, false, false);
    try (InputStream is = new Base64InputStream(new BufferedInputStream(proc.getInputStream()));
        ArchiveInputStream archive = new TarArchiveInputStream(new GzipCompressorInputStream(is))) {
        for (ArchiveEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) {
            if (!archive.canReadEntryData(entry)) {
                log.error("Can't read: " + entry);
                continue;
            }
            String normalName = FilenameUtils.normalize(entry.getName());
            if (normalName == null) {
                throw new IOException("Invalid entry: " + entry.getName());
            }
            File f = new File(destination.toFile(), normalName);
            if (entry.isDirectory()) {
                if (!f.isDirectory() && !f.mkdirs()) {
                    throw new IOException("create directory failed: " + f);
                }
            } else {
                File parent = f.getParentFile();
                if (!parent.isDirectory() && !parent.mkdirs()) {
                    throw new IOException("create directory failed: " + parent);
                }
                try (OutputStream fs = new FileOutputStream(f)) {
                    Streams.copy(archive, fs);
                    fs.flush();
                }
            }
        }
    }
    return new ProcessFuture(proc);
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) File(java.io.File)

Example 49 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project flow by vaadin.

the class DefaultArchiveExtractor method extractGzipTarArchive.

private void extractGzipTarArchive(File archive, File destinationDirectory) throws IOException {
    try (FileInputStream fis = new FileInputStream(archive);
        GzipCompressorInputStream gis = new GzipCompressorInputStream(fis);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gis)) {
        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        String canonicalDestinationDirectory = destinationDirectory.getCanonicalPath();
        while (tarEntry != null) {
            // Create a file for this tarEntry
            final File destPath = new File(destinationDirectory + File.separator + tarEntry.getName());
            prepDestination(destPath, tarEntry.isDirectory());
            if (!startsWithPath(destPath.getCanonicalPath(), canonicalDestinationDirectory)) {
                throw new IOException("Expanding " + tarEntry.getName() + " would create file outside of " + canonicalDestinationDirectory);
            }
            copyTarFileContents(tarIn, tarEntry, destPath);
            tarEntry = tarIn.getNextTarEntry();
        }
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 50 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project arctic-sea by 52North.

the class FileDownloader method gunzipFile.

public static void gunzipFile(String filePath) throws IOException {
    File file = new File(filePath);
    String outPath = null;
    final byte[] buff = new byte[1024];
    if (!file.getName().endsWith("gz")) {
        throw new IOException("File is not ends with .gz extension");
    } else {
        outPath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 3);
    }
    try (FileOutputStream fout = new FileOutputStream(outPath);
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream bin = new BufferedInputStream(fin);
        GzipCompressorInputStream gzipin = new GzipCompressorInputStream(bin)) {
        int n = 0;
        while (-1 != (n = gzipin.read(buff))) {
            fout.write(buff, 0, n);
        }
        LOG.debug("Extracted file path {}", outPath);
    } catch (IOException e) {
        throw e;
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)60 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)47 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)41 IOException (java.io.IOException)30 FileInputStream (java.io.FileInputStream)27 BufferedInputStream (java.io.BufferedInputStream)24 File (java.io.File)24 FileOutputStream (java.io.FileOutputStream)21 InputStream (java.io.InputStream)18 OutputStream (java.io.OutputStream)12 Path (java.nio.file.Path)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArrayList (java.util.ArrayList)8 BufferedOutputStream (java.io.BufferedOutputStream)7 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)6 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)5 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 URL (java.net.URL)4