Search in sources :

Example 91 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project AmazeFileManager by TeamAmaze.

the class TarExtractor method extractWithFilter.

@Override
protected void extractWithFilter(@NonNull Filter filter) throws IOException {
    long totalBytes = 0;
    ArrayList<TarArchiveEntry> archiveEntries = new ArrayList<>();
    TarArchiveInputStream inputStream = new TarArchiveInputStream(new FileInputStream(filePath));
    TarArchiveEntry tarArchiveEntry;
    while ((tarArchiveEntry = inputStream.getNextTarEntry()) != null) {
        if (filter.shouldExtract(tarArchiveEntry.getName(), tarArchiveEntry.isDirectory())) {
            archiveEntries.add(tarArchiveEntry);
            totalBytes += tarArchiveEntry.getSize();
        }
    }
    listener.onStart(totalBytes, archiveEntries.get(0).getName());
    inputStream.close();
    inputStream = new TarArchiveInputStream(new FileInputStream(filePath));
    for (TarArchiveEntry entry : archiveEntries) {
        if (!listener.isCancelled()) {
            listener.onUpdate(entry.getName());
            // TAR is sequential, you need to walk all the way to the file you want
            while (entry.hashCode() != inputStream.getNextTarEntry().hashCode()) ;
            extractEntry(context, inputStream, entry, outputPath);
        }
    }
    inputStream.close();
    listener.onFinish();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArrayList(java.util.ArrayList) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 92 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project docker-client by spotify.

the class DefaultDockerClientTest method testCopyContainer.

@Test
@SuppressWarnings("deprecation")
public void testCopyContainer() throws Exception {
    requireDockerApiVersionLessThan("1.24", "failCopyToContainer");
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    final ImmutableSet.Builder<String> files = ImmutableSet.builder();
    try (final TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.copyContainer(id, "/bin"))) {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            files.add(entry.getName());
        }
    }
    // Check that some common files exist
    assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/wc")));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ImmutableSet(com.google.common.collect.ImmutableSet) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 93 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project vespa by vespa-engine.

the class CompressedApplicationInputStreamTest method require_that_gnu_tared_file_can_be_unpacked.

@Test
public void require_that_gnu_tared_file_can_be_unpacked() throws IOException, InterruptedException {
    File tmpTar = File.createTempFile("myapp", ".tar");
    Process p = new ProcessBuilder("tar", "-C", "src/test/resources/deploy/validapp", "--exclude=.svn", "-cvf", tmpTar.getAbsolutePath(), ".").start();
    p.waitFor();
    p = new ProcessBuilder("gzip", tmpTar.getAbsolutePath()).start();
    p.waitFor();
    File gzFile = new File(tmpTar.getAbsolutePath() + ".gz");
    assertTrue(gzFile.exists());
    CompressedApplicationInputStream unpacked = CompressedApplicationInputStream.createFromCompressedStream(new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(gzFile))));
    File outApp = unpacked.decompress();
    assertTestApp(outApp);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 94 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project nd4j by deeplearning4j.

the class ArchiveUtils method unzipFileTo.

/**
 * Extracts files to the specified destination
 *
 * @param file the file to extract to
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte[] data = new byte[BUFFER];
    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        try (ZipInputStream zis = new ZipInputStream(fin)) {
            // get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(dest + File.separator + fileName);
                if (ze.isDirectory()) {
                    newFile.mkdirs();
                    zis.closeEntry();
                    ze = zis.getNextEntry();
                    continue;
                }
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(data)) > 0) {
                    fos.write(data, 0, len);
                }
                fos.close();
                ze = zis.getNextEntry();
                log.debug("File extracted: " + newFile.getAbsoluteFile());
            }
            zis.closeEntry();
        }
    } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {
        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
        TarArchiveEntry entry;
        /* Read the tar entries using the getNextEntry method **/
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            log.info("Extracting: " + entry.getName());
            if (entry.isDirectory()) {
                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            } else /*
                 * If the entry is a file,write the decompressed file to the disk
                 * and close destination stream.
                 */
            {
                int count;
                try (FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                    BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER)) {
                    while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                        destStream.write(data, 0, count);
                    }
                    destStream.flush();
                    IOUtils.closeQuietly(destStream);
                }
            }
        }
        // Close the input stream
        tarIn.close();
    } else if (file.endsWith(".gz")) {
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        try (GZIPInputStream is2 = new GZIPInputStream(fin);
            OutputStream fos = FileUtils.openOutputStream(extracted)) {
            IOUtils.copyLarge(is2, fos);
            fos.flush();
        }
    } else {
        throw new IllegalStateException("Unable to infer file type (compression format) from source file name: " + file);
    }
    target.delete();
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ZipEntry(java.util.zip.ZipEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream)

Example 95 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project systemml by apache.

the class ValidateLicAndNotice method getFilesFromTGZ.

/**
 * This will return the list of files from tgz file.
 *
 * @param	tgzFileName is the name of tgz file from which list of files with specified file extension will be returned.
 * @param	fileExt is the file extension to be used to get list of files to be returned.
 * @return	Returns list of files having specified extension from tgz file.
 */
public static List<String> getFilesFromTGZ(String tgzFileName, String fileExt) {
    TarArchiveInputStream tarIn = null;
    try {
        tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
    } catch (Exception e) {
        Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
        return null;
    }
    List<String> files = new ArrayList<String>();
    try {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.getName().endsWith("." + fileExt)) {
                int iPos = tarEntry.getName().lastIndexOf("/");
                if (iPos == 0)
                    --iPos;
                String strFileName = tarEntry.getName().substring(iPos + 1);
                files.add(strFileName);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (files);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)132 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)99 File (java.io.File)52 IOException (java.io.IOException)50 FileInputStream (java.io.FileInputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)46 InputStream (java.io.InputStream)35 FileOutputStream (java.io.FileOutputStream)34 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 OutputStream (java.io.OutputStream)16 Path (java.nio.file.Path)16 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)8