Search in sources :

Example 81 with TarArchiveEntry

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

the class TarUtil method archiveDir.

private static void archiveDir(File dir, TarArchiveOutputStream taos, String basePath) throws Exception {
    File[] files = dir.listFiles();
    if (files.length < 1) {
        TarArchiveEntry entry = new TarArchiveEntry(basePath + dir.getName() + File.separator);
        taos.putArchiveEntry(entry);
        taos.closeArchiveEntry();
    }
    for (File file : files) {
        // 递归归档
        archive(file, taos, basePath + dir.getName() + File.separator);
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 82 with TarArchiveEntry

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

the class TarUtil method archiveDir.

private static void archiveDir(File dir, TarArchiveOutputStream taos, String basePath) throws Exception {
    File[] files = dir.listFiles();
    if (files.length < 1) {
        TarArchiveEntry entry = new TarArchiveEntry(basePath + dir.getName() + File.separator);
        taos.putArchiveEntry(entry);
        taos.closeArchiveEntry();
    }
    for (File file : files) {
        // 递归归档
        archive(file, taos, basePath + dir.getName() + File.separator);
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 83 with TarArchiveEntry

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

the class TarUtil method dearchive.

private static void dearchive(File destFile, TarArchiveInputStream tais) throws Exception {
    TarArchiveEntry entry = null;
    while ((entry = tais.getNextTarEntry()) != null) {
        // 文件
        String dir = destFile.getPath() + File.separator + entry.getName();
        File dirFile = new File(dir);
        // 文件检查
        fileProber(dirFile);
        if (entry.isDirectory()) {
            dirFile.mkdirs();
        } else if (entry.isSymbolicLink()) {
            File target = new File(entry.getLinkName());
            Files.createSymbolicLink(dirFile.toPath(), target.toPath());
        } else {
            boolean executable = (entry.getMode() % 2) == 1;
            dearchiveFile(dirFile, tais);
            dirFile.setExecutable(executable);
        }
    }
}
Also used : File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 84 with TarArchiveEntry

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

the class Utils method unTarUsingJava.

private static void unTarUsingJava(File inFile, File targetDir, 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, targetDir);
            entry = tis.getNextTarEntry();
        }
    } finally {
        cleanup(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) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) 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 85 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project mycore by MyCoRe-Org.

the class MCRUtils method untar.

/**
 * Extracts files in a tar archive. Currently works only on uncompressed tar files.
 *
 * @param source
 *            the uncompressed tar to extract
 * @param expandToDirectory
 *            the directory to extract the tar file to
 * @throws IOException
 *             if the source file does not exists
 */
public static void untar(Path source, Path expandToDirectory) throws IOException {
    try (TarArchiveInputStream tain = new TarArchiveInputStream(Files.newInputStream(source))) {
        TarArchiveEntry tarEntry;
        FileSystem targetFS = expandToDirectory.getFileSystem();
        HashMap<Path, FileTime> directoryTimes = new HashMap<>();
        while ((tarEntry = tain.getNextTarEntry()) != null) {
            Path target = MCRPathUtils.getPath(targetFS, tarEntry.getName());
            Path absoluteTarget = expandToDirectory.resolve(target).normalize().toAbsolutePath();
            if (tarEntry.isDirectory()) {
                Files.createDirectories(expandToDirectory.resolve(absoluteTarget));
                directoryTimes.put(absoluteTarget, FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime()));
            } else {
                if (Files.notExists(absoluteTarget.getParent())) {
                    Files.createDirectories(absoluteTarget.getParent());
                }
                Files.copy(tain, absoluteTarget, StandardCopyOption.REPLACE_EXISTING);
                Files.setLastModifiedTime(absoluteTarget, FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime()));
            }
        }
        // restore directory dates
        Files.walkFileTree(expandToDirectory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Path absolutePath = dir.normalize().toAbsolutePath();
                Files.setLastModifiedTime(absolutePath, directoryTimes.get(absolutePath));
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Path(java.nio.file.Path) HashMap(java.util.HashMap) FileSystem(java.nio.file.FileSystem) FileTime(java.nio.file.attribute.FileTime) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)213 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)102 File (java.io.File)91 FileInputStream (java.io.FileInputStream)59 IOException (java.io.IOException)59 FileOutputStream (java.io.FileOutputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)40 InputStream (java.io.InputStream)32 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)32 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Test (org.junit.Test)24 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 OutputStream (java.io.OutputStream)18 ArrayList (java.util.ArrayList)18 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)16 HashMap (java.util.HashMap)12 GZIPInputStream (java.util.zip.GZIPInputStream)12