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);
}
}
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);
}
}
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);
}
}
}
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);
}
}
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);
}
});
}
}
Aggregations