1 package osm.jp.gpx.utils;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
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;
16 * ファイル更新日時をオリジナルと同じにします。
17 * Apache Commons Compressライブラリ
18 * commons-compress-1.14.jar
20 public abstract class TarGz
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);
30 * ファイル更新日時をオリジナルと同じにします。
31 * @param tazFile 解凍する*.tar.gzファイル
35 public static void uncompress(File tazFile, File dest) throws IOException {
38 TarArchiveInputStream tarIn = null;
39 tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tazFile))));
41 TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
42 while (tarEntry != null) {
43 File destPath = new File(dest, tarEntry.getName());
44 System.out.println("uncompress: " + destPath.getCanonicalPath());
45 if (tarEntry.isDirectory()) {
49 File dir = new File(destPath.getParent());
53 destPath.createNewFile();
54 byte[] btoRead = new byte[1024];
55 try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
57 while ((len = tarIn.read(btoRead)) != -1) {
58 bout.write(btoRead, 0, len);
61 destPath.setLastModified(tarEntry.getLastModifiedDate().getTime());
64 tarEntry = tarIn.getNextTarEntry();