Newer
Older
osmCoverage / test / tools / Compless.java
  1. package tools;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.util.zip.GZIPInputStream;
  9. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  10. import org.apache.commons.compress.utils.IOUtils;
  11. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  12.  
  13. public class Compless {
  14. /**
  15. * `.tar.gz`形式のファイルを指定のディレクトリにuncompless
  16. *
  17. * @param outDir
  18. * @param gzipFile
  19. * @throws FileNotFoundException
  20. * @throws IOException
  21. */
  22. public static void uncomplessTarGz(File outDir, File gzipFile) throws FileNotFoundException, IOException {
  23. try (FileInputStream fis = new FileInputStream(gzipFile)) {
  24. try (GZIPInputStream gis = new GZIPInputStream(fis)) {
  25. try (TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
  26. for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis.getNextTarEntry()) {
  27. if (entry.isDirectory()) {
  28. File newDir = new File(outDir, entry.getName());
  29. try {
  30. newDir.mkdirs();
  31. }
  32. catch(Exception e) {
  33. throw new IOException("directory '" + newDir.getAbsolutePath() + "' cannot create.");
  34. }
  35. }
  36. else {
  37. File newfile = new File(outDir, entry.getName());
  38. File parentDir = newfile.getParentFile();
  39. try {
  40. parentDir.mkdirs();
  41. }
  42. catch(Exception e) {
  43. throw new IOException("directory '" + parentDir.getAbsolutePath() + "' cannot create.");
  44. }
  45. try (FileOutputStream fos = new FileOutputStream(newfile)) {
  46. IOUtils.copy(tis, fos); // Apache common-io
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }