Newer
Older
adjustgpx-core / src / osm / jp / gpx / utils / TarGz.java
@haya4 haya4 on 18 Aug 2019 2 KB Java11 - AdjustTime2
  1. package osm.jp.gpx.utils;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9.  
  10. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  11. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  12. import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
  13.  
  14. /**
  15. * 「*.tar.gz」を解凍する。
  16. * ファイル更新日時をオリジナルと同じにします。
  17. * Apache Commons Compressライブラリ
  18. * commons-compress-1.14.jar
  19. */
  20. public abstract class TarGz
  21. {
  22. public static void main(String[] args) throws IOException {
  23. File baseDir = new File("testdata/cameradata");
  24. File tazFile = new File("testdata", "Sony20170518.tar.gz");
  25. TarGz.uncompress(tazFile, baseDir);
  26. }
  27. /**
  28. * *.tar.gz解凍
  29. * ファイル更新日時をオリジナルと同じにします。
  30. * @param tazFile 解凍する*.tar.gzファイル
  31. * @param dest 解凍先フォルダ
  32. * @throws IOException
  33. */
  34. public static void uncompress(File tazFile, File dest) throws IOException {
  35. dest.mkdir();
  36. TarArchiveInputStream tarIn = null;
  37. tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tazFile))));
  38.  
  39. TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
  40. while (tarEntry != null) {
  41. File destPath = new File(dest, tarEntry.getName());
  42. System.out.println("uncompress: " + destPath.getCanonicalPath());
  43. if (tarEntry.isDirectory()) {
  44. destPath.mkdirs();
  45. }
  46. else {
  47. File dir = new File(destPath.getParent());
  48. if (!dir.exists()) {
  49. dir.mkdirs();
  50. }
  51. destPath.createNewFile();
  52. byte[] btoRead = new byte[1024];
  53. try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
  54. int len = 0;
  55. while ((len = tarIn.read(btoRead)) != -1) {
  56. bout.write(btoRead, 0, len);
  57. }
  58. }
  59. destPath.setLastModified(tarEntry.getLastModifiedDate().getTime());
  60. btoRead = null;
  61. }
  62. tarEntry = tarIn.getNextTarEntry();
  63. }
  64. tarIn.close();
  65. }
  66. }