Sunday, February 18, 2024

gzip tar extract


import java.nio.file.*; import java.io.*; import org.apache.commons.compress.archivers.tar.*; import org.apache.commons.compress.compressors.gzip.*; public class TarGzExtractor { public static void main(String[] args) throws IOException { Path tarGzFilePath = Paths.get("path/to/your/file.tar.gz"); try (InputStream is = Files.newInputStream(tarGzFilePath); BufferedInputStream bis = new BufferedInputStream(is); GzipCompressorInputStream gzis = new GzipCompressorInputStream(bis); TarArchiveInputStream taris = new TarArchiveInputStream(gzis)) { TarArchiveEntry entry; while ((entry = taris.getNextTarEntry()) != null) { Path destPath = Paths.get("destination/directory", entry.getName()); if (entry.isDirectory()) { Files.createDirectories(destPath); } else { Files.copy(taris, destPath, StandardCopyOption.REPLACE_EXISTING); } } } } }

No comments:

Post a Comment