22 package org.matsim.run.gui;
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
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;
35 import javax.swing.JFileChooser;
36 import javax.swing.JFrame;
37 import javax.swing.JOptionPane;
38 import javax.swing.SwingUtilities;
40 import org.apache.logging.log4j.LogManager;
41 import org.apache.logging.log4j.Logger;
48 private final static Logger log = LogManager.getLogger(GUnZipper.class);
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();
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();
62 doGzip(srcFile, destFile);
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();
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);
83 private static void doGzip(
final File srcFile,
final File destFile) {
84 AsyncFileInputProgressDialog gui =
new AsyncFileInputProgressDialog();
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));
101 },
"gzipper").start();
104 private static void doGunzip(
final File srcFile,
final File destFile) {
105 AsyncFileInputProgressDialog gui =
new AsyncFileInputProgressDialog();
107 try (FileInputStream srcStream =
new FileInputStream(srcFile);
108 FileOutputStream destStream =
new FileOutputStream(destFile);
109 BufferedInputStream bSrcStream =
new BufferedInputStream(
new GZIPInputStream(srcStream, 64 * 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));
122 },
"gunzipper").start();
126 private static void doCopy(InputStream src, OutputStream dest)
throws IOException {
127 byte[] buffer =
new byte[64 * 1024];
129 while ((bytesRead = src.read(buffer)) != -1) {
130 dest.write(buffer, 0, bytesRead);
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");
142 System.out.println(
"let's continue");
144 System.out.println(
"and we're done");