长任务,显示进度条

package my;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Swing2
{
private static void createGUI()
{
JFrame frame = new MyFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class MyFrame extends JFrame
{
JButton okButton = new JButton("开始");
WaitDialog waitDialog = null;
public MyFrame(String title)
{
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new FlowLayout());
root.add(okButton);
okButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
onMouseClicked();
}
});
}
private void onMouseClicked()
{
File srcFile = new File("C:/Users/Administrator/Desktop/1.zip");
File dstFile = new File("C:/Users/Administrator/Desktop/2.zip");
CopyTask th = new CopyTask();
th.execute(srcFile, dstFile);
waitDialog = new WaitDialog(this);
waitDialog.exec();
}
private class CopyTask extends Thread
{
private File srcFile, dstFile;
public void execute(File srcFile, File dstFile)
{
this.srcFile = srcFile;
this.dstFile = dstFile;
start();
}
@Override
public void run()
{
try {
doInBackground();
}catch(Exception e)
{
e.printStackTrace();
}
try { sleep(100); }catch(Exception e) {}
SwingUtilities.invokeLater( ()->{
done();
});
}
protected void doInBackground() throws Exception
{
InputStream inputStream = null;
OutputStream outputStream = null;
long lastTime = System.currentTimeMillis();;
long fileSize = srcFile.length();
long count = 0;
try {
inputStream = new FileInputStream(srcFile);
outputStream = new FileOutputStream(dstFile);
byte[] buf = new byte[8192];
while(true)
{
int n = inputStream.read(buf);
if(n <= 0) break;
outputStream.write(buf, 0, n);
count += n;
long now = System.currentTimeMillis();
if(now - lastTime > 500)
{
lastTime = now;
updateProgress(count , fileSize);
Thread.sleep(5000);
}
}
}
finally {
try { inputStream.close();}catch(Exception e) {}
try { outputStream.close();} catch(Exception e) {}
}
}
protected void updateProgress(long count, long total)
{
SwingUtilities.invokeLater( ()->{
if(waitDialog!=null)
{
int percent = (int)(100 * count / total) ;
waitDialog.setProgress( percent );
}
});
}
protected void done()
{
if(waitDialog!=null)
{
waitDialog.setVisible(false);
waitDialog = null;
}
}
}
}
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class WaitDialog extends JDialog
{
public JLabel display = new JLabel("请等待...");
public JProgressBar pbar = new JProgressBar();
public WaitDialog(JFrame owner)
{
super(owner, "", true);
this.setUndecorated(true);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
root.setOpaque(true);
root.setBackground(new Color(0xF4F4F4));
Border b1 = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
Border b2 = BorderFactory.createEmptyBorder(4, 4, 4, 4);
Border b3 = BorderFactory.createCompoundBorder(b1, b2);
root.setBorder(b3);
display.setHorizontalAlignment(SwingConstants.CENTER);
root.add(display, BorderLayout.CENTER);
pbar.setMinimum(0);
pbar.setMaximum(100);
pbar.setPreferredSize(new Dimension(0,20));
root.add(pbar, BorderLayout.PAGE_END);
this.setSize(200, 80);
}
public void setProgress(int n)
{
pbar.setValue(n);
}
public void exec()
{
Rectangle frmRect = this.getOwner().getBounds();
int width = this.getWidth();
int height = this.getHeight();
int x = frmRect.x + (frmRect.width - width) / 2;
int y = frmRect.y + (frmRect.height - height) / 2;
this.setBounds(x, y, width, height);
this.setVisible(true);
}
}
zip查看目录

package my;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class TestUnzip
{
public static void main(String[] args) throws Exception
{
File scrFile = new File("1.zip");
ZipFile zipFile = new ZipFile(scrFile, Charset.forName("GBK"));
int fileCount = 0;
long totalSize = 0;
Enumeration<?> entries = zipFile.entries();
while(entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) entries.nextElement();
if(entry.isDirectory())
{
System.out.println("** " + entry.getName());
}
else
{
System.out.println(" " + entry.getName());
fileCount += 1;
totalSize += entry.getSize();
}
}
System.out.println("-----------------------");
System.out.println("文件总数: " + fileCount);
System.out.println("文件总数: " + totalSize);
zipFile.close();
}
}
zip解压缩

package my;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class TestUnzip
{
public static void main(String[] args) throws Exception
{
File scrFile = new File("1.zip");
File dstDir = new File("C:/Users/Administrator/Desktop/");
dstDir.mkdirs();
ZipFile zipFile = new ZipFile(scrFile, Charset.forName("GBK"));
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.isDirectory()) continue;
System.out.println("处理文件:" + entry.getName());
File dstFile = new File(dstDir, entry.getName());
dstFile.getParentFile().mkdirs();
InputStream inputStream = zipFile.getInputStream(entry);
OutputStream outputStream = new FileOutputStream(dstFile);
try
{
byte[] buf = new byte[4096];
while(true)
{
int n = inputStream.read(buf);
if(n <= 0)break;
outputStream.write(buf, 0, n);
}
}
finally
{
try{ inputStream.close(); } catch (Exception e){}
try{ outputStream.close(); } catch (Exception e){}
}
}
zipFile.close();
}
}
显示zip目录,解压缩操作(略)