Search in sources :

Example 46 with TarArchiveEntry

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

the class TarUtils method addFileEntry.

private static void addFileEntry(TarArchiveOutputStream tarOut, String entryName, File file, long modTime) throws IOException {
    final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
    if (modTime >= 0) {
        tarEntry.setModTime(modTime);
    }
    tarOut.putArchiveEntry(tarEntry);
    try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
        final byte[] buf = new byte[BUF_SIZE];
        int r;
        while ((r = in.read(buf)) != -1) {
            tarOut.write(buf, 0, r);
        }
    }
    tarOut.closeArchiveEntry();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 47 with TarArchiveEntry

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

the class Utils method unTarUsingJava.

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

Example 48 with TarArchiveEntry

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

the class Utils method unpackEntries.

private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        File subDir = new File(outputDir, entry.getName());
        if (!subDir.mkdirs() && !subDir.isDirectory()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        if (!outputFile.getParentFile().mkdirs()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
    }
    int count;
    byte[] data = new byte[2048];
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    while ((count = tis.read(data)) != -1) {
        outputStream.write(data, 0, count);
    }
    outputStream.flush();
    outputStream.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 49 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry 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 50 with TarArchiveEntry

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

the class Utils method unpackEntries.

private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        File subDir = new File(outputDir, entry.getName());
        if (!subDir.mkdirs() && !subDir.isDirectory()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        if (!outputFile.getParentFile().mkdirs()) {
            throw new IOException("Mkdirs failed to create tar internal dir " + outputDir);
        }
    }
    int count;
    byte[] data = new byte[2048];
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    while ((count = tis.read(data)) != -1) {
        outputStream.write(data, 0, count);
    }
    outputStream.flush();
    outputStream.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

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