MATSIM
GUnZipper.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * GUnZipper.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22 package org.matsim.run.gui;
23 
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.util.zip.GZIPInputStream;
33 import java.util.zip.GZIPOutputStream;
34 
35 import javax.swing.JFileChooser;
36 import javax.swing.JFrame;
37 import javax.swing.JOptionPane;
38 import javax.swing.SwingUtilities;
39 
40 import org.apache.logging.log4j.LogManager;
41 import org.apache.logging.log4j.Logger;
42 
46 /*package*/ class GUnZipper {
47 
48  private final static Logger log = LogManager.getLogger(GUnZipper.class);
49 
50  public static void gzipFile() {
51  JFileChooser chooser = new JFileChooser();
52  int openResult = chooser.showOpenDialog(null);
53  if (openResult == JFileChooser.APPROVE_OPTION) {
54  File srcFile = chooser.getSelectedFile();
55 
56  chooser = new SaveFileSaver();
57  chooser.setSelectedFile(new File(srcFile.getParentFile(), srcFile.getName() + ".gz"));
58  int saveResult = chooser.showSaveDialog(null);
59  if (saveResult == JFileChooser.APPROVE_OPTION) {
60  File destFile = chooser.getSelectedFile();
61 
62  doGzip(srcFile, destFile);
63  }
64  }
65  }
66 
67  public static void gunzipFile() {
68  JFileChooser chooser = new JFileChooser();
69  int openResult = chooser.showOpenDialog(null);
70  if (openResult == JFileChooser.APPROVE_OPTION) {
71  File srcFile = chooser.getSelectedFile();
72 
73  chooser = new SaveFileSaver();
74  chooser.setSelectedFile(new File(srcFile.getParentFile(), srcFile.getName().replace(".gz", "")));
75  int saveResult = chooser.showSaveDialog(null);
76  if (saveResult == JFileChooser.APPROVE_OPTION) {
77  File destFile = chooser.getSelectedFile();
78  doGunzip(srcFile, destFile);
79  }
80  }
81  }
82 
83  private static void doGzip(final File srcFile, final File destFile) {
84  AsyncFileInputProgressDialog gui = new AsyncFileInputProgressDialog();
85  new Thread(() -> {
86  try (FileInputStream srcStream = new FileInputStream(srcFile);
87  FileOutputStream destStream = new FileOutputStream(destFile);
88  BufferedInputStream bSrcStream = new BufferedInputStream(srcStream, 4 * 1024 * 1024);
89  BufferedOutputStream bDestStream = new BufferedOutputStream(
90  new GZIPOutputStream(destStream, 64 * 1024), 4 * 1024 * 1024)) {
91  gui.observeProgress(srcStream);
92  doCopy(bSrcStream, bDestStream);
93  SwingUtilities.invokeLater(gui::dispose);
94  } catch (IOException e) {
95  log.error(e.getMessage(), e);
96  SwingUtilities.invokeLater(gui::dispose);
97  SwingUtilities.invokeLater(
98  () -> JOptionPane.showMessageDialog(null, e.getMessage(), "Error while gzipping",
99  JOptionPane.ERROR_MESSAGE));
100  }
101  }, "gzipper").start();
102  }
103 
104  private static void doGunzip(final File srcFile, final File destFile) {
105  AsyncFileInputProgressDialog gui = new AsyncFileInputProgressDialog();
106  new Thread(() -> {
107  try (FileInputStream srcStream = new FileInputStream(srcFile);
108  FileOutputStream destStream = new FileOutputStream(destFile);
109  BufferedInputStream bSrcStream = new BufferedInputStream(new GZIPInputStream(srcStream, 64 * 1024),
110  4 * 1024 * 1024);
111  BufferedOutputStream bDestStream = new BufferedOutputStream(destStream, 4 * 1024 * 1024)) {
112  gui.observeProgress(srcStream);
113  doCopy(bSrcStream, bDestStream);
114  SwingUtilities.invokeLater(gui::dispose);
115  } catch (IOException e) {
116  log.error(e.getMessage(), e);
117  SwingUtilities.invokeLater(gui::dispose);
118  SwingUtilities.invokeLater(
119  () -> JOptionPane.showMessageDialog(null, e.getMessage(), "Error while gunzipping",
120  JOptionPane.ERROR_MESSAGE));
121  }
122  }, "gunzipper").start();
123 
124  }
125 
126  private static void doCopy(InputStream src, OutputStream dest) throws IOException {
127  byte[] buffer = new byte[64 * 1024];
128  int bytesRead;
129  while ((bytesRead = src.read(buffer)) != -1) {
130  dest.write(buffer, 0, bytesRead);
131  }
132  dest.flush();
133  }
134 
135  public static void main(String[] args) throws Throwable {
136  SwingUtilities.invokeLater(() -> {
137  final JFrame frame = new JFrame();
138  frame.setBounds(100, 100, 600, 500);
139  frame.setVisible(true);
140  System.out.println("let's go");
141  gzipFile();
142  System.out.println("let's continue");
143  gunzipFile();
144  System.out.println("and we're done");
145  frame.dispose();
146  });
147  }
148 }