use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project hive by apache.
the class CompressionUtils method unTar.
/**
* Untar an input file into an output file.
*
* The output file is created in the output folder, having the same name as the input file, minus
* the '.tar' extension.
*
* @param inputFileName the input .tar file
* @param outputDirName the output directory file.
* @throws IOException
* @throws FileNotFoundException
*
* @return The {@link List} of {@link File}s with the untared content.
* @throws ArchiveException
*/
public static List<File> unTar(final String inputFileName, final String outputDirName, boolean flatten) throws FileNotFoundException, IOException, ArchiveException {
File inputFile = new File(inputFileName);
File outputDir = new File(outputDirName);
final List<File> untaredFiles = new LinkedList<File>();
InputStream is = null;
try {
if (inputFileName.endsWith(".gz")) {
is = new GzipCompressorInputStream(new FileInputStream(inputFile));
} else {
is = new FileInputStream(inputFile);
}
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (!outputFile.toPath().toAbsolutePath().normalize().startsWith(outputDir.toPath().toAbsolutePath().normalize())) {
throw new IOException("Untarred file is not under the output directory");
}
if (entry.isDirectory()) {
if (flatten) {
// no sub-directories
continue;
}
LOG.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
final OutputStream outputFileStream;
if (flatten) {
File flatOutputFile = new File(outputDir, outputFile.getName());
LOG.debug(String.format("Creating flat output file %s.", flatOutputFile.getAbsolutePath()));
outputFileStream = new FileOutputStream(flatOutputFile);
} else if (!outputFile.getParentFile().exists()) {
LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getParentFile().getAbsoluteFile()));
if (!outputFile.getParentFile().getAbsoluteFile().mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getParentFile().getAbsolutePath()));
}
LOG.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
outputFileStream = new FileOutputStream(outputFile);
} else {
outputFileStream = new FileOutputStream(outputFile);
}
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
untaredFiles.add(outputFile);
}
debInputStream.close();
return untaredFiles;
} finally {
if (is != null)
is.close();
}
}
use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project packr by libgdx.
the class ArchiveUtils method createArchive.
/**
* Creates a new archive from the contents in {@code directoryToArchive}.
*
* @param archiveType the type of archive to create
* @param directoryToArchive the directory to archive the contents of
* @param archiveFile the file to write the archive to
* @throws IOException if an IO error occurs
* @throws ArchiveException if an archive error occurs
*/
public static void createArchive(ArchiveType archiveType, Path directoryToArchive, Path archiveFile) throws IOException, ArchiveException {
try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(archiveFile));
ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiveType.getCommonsCompressName(), fileOutputStream)) {
if (archiveType == ArchiveType.TAR) {
((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(LONGFILE_GNU);
}
Files.walkFileTree(directoryToArchive, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
createAndPutArchiveEntry(archiveType, archiveOutputStream, directoryToArchive, file);
archiveOutputStream.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (Files.isSameFile(dir, directoryToArchive)) {
return FileVisitResult.CONTINUE;
}
ArchiveEntry entry = archiveOutputStream.createArchiveEntry(dir.toFile(), getRelativePathString(dir, directoryToArchive));
archiveOutputStream.putArchiveEntry(entry);
archiveOutputStream.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
});
archiveOutputStream.finish();
}
}
use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project packr by libgdx.
the class ArchiveUtilsTest method testArchiveDuplicateEntry.
/**
* Adds the same entry to a Zip file to ensure that extraction handles duplicates properly.
*/
@Test
public void testArchiveDuplicateEntry(@TempDir Path tempDir) throws IOException, ArchiveException, CompressorException {
String someFilename = "some-file.txt";
Path someFilePath = tempDir.resolve(someFilename);
Files.write(someFilePath, "Hello world\n".getBytes(StandardCharsets.UTF_8));
Path archiveZip = tempDir.resolve("archive.zip");
// Create an archive, add entry, update file, add same entry
try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(archiveZip));
ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(ZIP.getCommonsCompressName(), fileOutputStream)) {
// Create an entry for some file
ArchiveEntry entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename);
archiveOutputStream.putArchiveEntry(entry);
Files.copy(someFilePath, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
// Update some file, and put it into the archive again
Files.write(someFilePath, "Good bye\n".getBytes(StandardCharsets.UTF_8));
entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename);
archiveOutputStream.putArchiveEntry(entry);
Files.copy(someFilePath, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
archiveOutputStream.finish();
}
Path extractionDirectory = tempDir.resolve("extract");
Files.createDirectories(extractionDirectory);
ArchiveUtils.extractArchive(archiveZip, extractionDirectory);
assertEquals(new String(Files.readAllBytes(tempDir.resolve(someFilename)), StandardCharsets.UTF_8), new String(Files.readAllBytes(extractionDirectory.resolve(someFilename)), StandardCharsets.UTF_8), "Extracted file contents should have matched original");
}
use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project quickutil by quickutil.
the class CompressUtil method decompressTarGz.
/**
* 解压tar.gz文件:
*
* @return 解压后的根目录路径
*/
public static String decompressTarGz(String sourcePath, String targetPath) {
String rootPath = null;
try (FileInputStream fInput = new FileInputStream(sourcePath);
BufferedInputStream bufInput = new BufferedInputStream(fInput);
GZIPInputStream gzipInput = new GZIPInputStream(bufInput);
ArchiveInputStream archiveInput = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInput)) {
// tar压缩文件条目
TarArchiveEntry entry;
boolean isRootPath = true;
while ((entry = (TarArchiveEntry) archiveInput.getNextEntry()) != null) {
String entryName = entry.getName();
// 转换为目标路径
if (targetPath != null) {
entryName = targetPath + File.separator + entryName;
}
if (isRootPath) {
rootPath = entryName;
isRootPath = false;
}
if (entry.isDirectory()) {
FileUtil.mkdirByFile(entryName);
} else if (entry.isFile()) {
FileUtil.stream2file(archiveInput, entryName, false);
}
}
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return rootPath;
}
use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project camel by apache.
the class TarFileDataFormat method unmarshal.
@Override
public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
if (usingIterator) {
return new TarIterator(exchange.getIn(), stream);
} else {
BufferedInputStream bis = new BufferedInputStream(stream);
TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis);
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
try {
TarArchiveEntry entry = tis.getNextTarEntry();
if (entry != null) {
exchange.getOut().setHeader(FILE_NAME, entry.getName());
IOHelper.copy(tis, osb);
}
entry = tis.getNextTarEntry();
if (entry != null) {
throw new IllegalStateException("Tar file has more than 1 entry.");
}
return osb.build();
} finally {
IOHelper.close(osb, tis, bis);
}
}
}
Aggregations