OSDN Git Service

fixed: GUIを一新した
[importpicture/importpicture.git] / src / test / java / osm / jp / gpx / UnZip.java
1 package osm.jp.gpx;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.zip.ZipEntry;
7 import java.util.zip.ZipInputStream;
8
9 public class UnZip {
10
11     /**
12      * Zipファイルを展開します
13      * @param aZipFile zipファイル
14      * @param aOutDir  出力先ディレクトリ
15      * @throws java.io.IOException
16      */
17     public static void decode(File aZipFile, String aOutDir) throws IOException {
18         FileInputStream  fileIn  = null;
19         FileOutputStream fileOut = null;
20         ZipInputStream zipIn = null;
21         
22         try {
23             File outDir = new File(aOutDir);
24             outDir.mkdirs();
25             
26             fileIn = new FileInputStream(aZipFile);
27             zipIn = new ZipInputStream(fileIn);
28             
29             ZipEntry entry = null;
30             while ((entry = zipIn.getNextEntry()) != null) {
31                 if (entry.isDirectory()) {
32                     String relativePath = entry.getName();
33                     outDir = new File(outDir, relativePath);
34                     outDir.mkdirs();
35                 }
36                 else {
37                     String relativePath = entry.getName();
38                     File outFile = new File( outDir, relativePath );
39                     
40                     File parentFile = outFile.getParentFile();
41                     parentFile.mkdirs();
42                     
43                     fileOut = new FileOutputStream( outFile );
44                     
45                     byte[] buf = new byte[ 256 ];
46                     int size = 0;
47                     while ((size = zipIn.read(buf)) > 0){
48                         fileOut.write(buf, 0, size);
49                     }
50                     fileOut.close();
51                     fileOut = null;
52                 }
53                 zipIn.closeEntry();
54             }
55         }
56         catch (IOException e) {
57             e.printStackTrace();
58         }
59         finally {
60             if (fileIn != null) {
61                 try {
62                     fileIn.close();
63                 }
64                 catch (IOException e) {}
65             }
66             if (fileOut != null) {
67                 try {
68                     fileOut.close();
69                 }
70                 catch(IOException e) {}
71             }
72             zipIn.close();
73         }
74     }
75 }