Search in sources :

Example 81 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project deeplearning4j 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")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(dest + File.separator + fileName);
            log.info("file unzip : " + newFile.getAbsoluteFile());
            //createComplex all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }
            fos.close();
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    } 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 = null;
        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;
                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")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }
    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 82 with TarArchiveInputStream

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

the class Loader method openArchiveIn.

private static ArchiveInputStream openArchiveIn(Path archive) throws IOException, IncorrectFormat {
    InputStream input = Files.newInputStream(archive);
    GzipCompressorInputStream compressor;
    try {
        compressor = new GzipCompressorInputStream(input);
    } catch (IOException e) {
        input.close();
        throw new IncorrectFormat(archive, e);
    }
    return new TarArchiveInputStream(compressor);
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 83 with TarArchiveInputStream

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

the class FileUtil method unTarUsingJava.

private static void unTarUsingJava(File inFile, File untarDir, boolean gzipped) throws IOException {
    InputStream inputStream = null;
    TarArchiveInputStream tis = null;
    try {
        if (gzipped) {
            inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile)));
        } else {
            inputStream = new BufferedInputStream(new FileInputStream(inFile));
        }
        tis = new TarArchiveInputStream(inputStream);
        for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; ) {
            unpackEntries(tis, entry, untarDir);
            entry = tis.getNextTarEntry();
        }
    } finally {
        IOUtils.cleanup(LOG, tis, inputStream);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 84 with TarArchiveInputStream

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

the class UnpackerResourceTest method testTarDocPicture.

@Test
public void testTarDocPicture() throws Exception {
    Response response = WebClient.create(endPoint + UNPACKER_PATH).type(APPLICATION_MSWORD).accept("application/x-tar").put(ClassLoader.getSystemResourceAsStream(TEST_DOC_WAV));
    Map<String, String> data = readArchiveFromStream(new TarArchiveInputStream((InputStream) response.getEntity()));
    assertEquals(JPG_MD5, data.get(JPG_NAME));
}
Also used : Response(javax.ws.rs.core.Response) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 85 with TarArchiveInputStream

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

the class Archiver method unpack.

@SneakyThrows
public void unpack(Path archive, Path dest, ArchiveType type) {
    switch(type) {
        case TAR_GZ:
            try (TarArchiveInputStream tar = new TarArchiveInputStream(IoStreams.in(archive, GZIP))) {
                ArchiveEntry entry;
                while ((entry = tar.getNextEntry()) != null) {
                    Path path = dest.resolve(entry.getName());
                    if (entry.isDirectory())
                        path.toFile().mkdirs();
                    else
                        IoStreams.write(path, PLAIN, tar);
                }
                tar.close();
            }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Path(java.nio.file.Path) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) SneakyThrows(lombok.SneakyThrows)

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