decompress file with JProgressBar - Java Swing

Java examples for Swing:JProgressBar

Description

decompress file with JProgressBar

Demo Code


//package com.java2s;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class Main {
    public static JLabel processLab;

    public static void decompress(String file, JProgressBar pbar) {
        //variable definition
        String newFile = file.substring(0, file.length() - 3);
        byte[] buf = new byte[1024];
        int inc;/*  ww  w .  j  a  v  a 2  s .co m*/

        try {
            GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(
                    file));
            FileOutputStream out = new FileOutputStream(newFile);

            int len;
            pbar.setMinimum(0);
            pbar.setMaximum((int) new File(file).length());
            inc = (int) (pbar.getMaximum() / 1024);
            processLab.setText("Uncompressing... "
                    + file.substring(file.lastIndexOf("\\") + 1));
            while ((len = gzis.read(buf)) > 0) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        pbar.setValue(pbar.getValue() + inc);
                    }
                });
                out.write(buf, 0, len);
            }
            pbar.setValue(pbar.getMaximum());
            gzis.close();
            out.close();
        } catch (IOException e) {
            System.out.println("Exception is" + e.getMessage());
        }
    }
}

Related Tutorials