Newer
Older
adjustgpx-core / test / osm / jp / gpx / UnZip.java
@haya4 haya4 on 18 Aug 2019 2 KB Java11 - AdjustTime2
  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. try {
  22. File outDir = new File(aOutDir);
  23. outDir.mkdirs();
  24. fileIn = new FileInputStream(aZipFile);
  25. zipIn = new ZipInputStream(fileIn);
  26. ZipEntry entry = null;
  27. while ((entry = zipIn.getNextEntry()) != null) {
  28. if (entry.isDirectory()) {
  29. String relativePath = entry.getName();
  30. outDir = new File(outDir, relativePath);
  31. outDir.mkdirs();
  32. }
  33. else {
  34. String relativePath = entry.getName();
  35. File outFile = new File( outDir, relativePath );
  36. File parentFile = outFile.getParentFile();
  37. parentFile.mkdirs();
  38. fileOut = new FileOutputStream( outFile );
  39. byte[] buf = new byte[ 256 ];
  40. int size = 0;
  41. while ((size = zipIn.read(buf)) > 0){
  42. fileOut.write(buf, 0, size);
  43. }
  44. fileOut.close();
  45. fileOut = null;
  46. }
  47. zipIn.closeEntry();
  48. }
  49. }
  50. catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. finally {
  54. if (fileIn != null) {
  55. try {
  56. fileIn.close();
  57. }
  58. catch (IOException e) {}
  59. }
  60. if (fileOut != null) {
  61. try {
  62. fileOut.close();
  63. }
  64. catch(IOException e) {}
  65. }
  66. zipIn.close();
  67. }
  68. }
  69. }