📄 myzipframe.java~184~
字号:
package myzip;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
/**
* <p>Title: zip</p>
* <p>Description: zip files</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: qlsc</p>
* @author Wang Tao
* @version 1.0
*/
public class MyZipFrame extends JFrame implements ListSelectionListener {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
BorderLayout borderLayout1 = new BorderLayout();
JMenuItem jMenuFileOpenFile = new JMenuItem();
JMenuItem jMenuFileOpenDir = new JMenuItem();
JMenuItem jMenuFileOpenZip = new JMenuItem();
JPanel buttonPanel = new JPanel();
FlowLayout flowLayout1 = new FlowLayout();
JButton zipButton = new JButton();
JButton unzipButton = new JButton();
JButton stopButton=new JButton();
JPanel listPanel = new JPanel();
JPanel centerPanel=new JPanel();
JPanel barPanel = new JPanel();
JTextArea textArea=new JTextArea();
JScrollPane textPane;
JButton startButton = new JButton();
TitledBorder titledBorder1;
JLabel messageLabel = new JLabel();
JProgressBar progressBar = new JProgressBar();
ImageIcon zipIcon=new ImageIcon("zip.jpg");
ImageIcon unzipIcon=new ImageIcon("unzip.jpg");
//ImageIcon openIcon=new ImageIcon("open.jpg");
//JList playlist;
//DefaultListModel plvector;
//JScrollPane pljsp;
String currentDirectory,selectedFile,zipname,firstFile,choosen;
String[] openFile_files,openDir_files,record=new String[100];
boolean isCompress,isDecompress,dirZip,stop=false,cancel=false;
Thread thread=null;
int count,unZipSize,zipSize;
//Construct the frame
public MyZipFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(500, 400));
this.setTitle("Java Zip");
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new MyZipFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("帮助");
jMenuHelpAbout.setText("关于");
jMenuHelpAbout.addActionListener(new MyZipFrame_jMenuHelpAbout_ActionAdapter(this));
jMenuFileOpenFile.setText("压缩文件");
jMenuFileOpenFile.addActionListener(new MyZipFrame_jMenuFileOpenFile_actionAdapter(this));
jMenuFileOpenZip.setText("打开压缩文件");
jMenuFileOpenZip.addActionListener(new MyZipFrame_jMenuFileOpenZip_actionAdapter(this));
buttonPanel.setBorder(BorderFactory.createRaisedBevelBorder());
buttonPanel.setToolTipText("操作板");
buttonPanel.setLayout(flowLayout1);
zipButton.setBackground(Color.lightGray);
zipButton.setMaximumSize(new Dimension(67, 67));
zipButton.setMinimumSize(new Dimension(67, 67));
zipButton.setPreferredSize(new Dimension(62, 63));
zipButton.setToolTipText("添加压缩文件");
zipButton.setIcon(zipIcon);System.out .println(zipIcon.getDescription()+"\n"+zipButton.getIcon().toString());
zipButton.setText("");
zipButton.addActionListener(new MyZipFrame_zipButton_actionAdapter(this));
unzipButton.setBackground(Color.lightGray);
unzipButton.setPreferredSize(new Dimension(65, 63));
unzipButton.setToolTipText("释放压缩文件");
unzipButton.setIcon(unzipIcon);
unzipButton.setText("");
unzipButton.addActionListener(new MyZipFrame_unzipButton_actionAdapter(this));
startButton.setBorder(BorderFactory.createRaisedBevelBorder());
startButton.setActionCommand("start");
startButton.setBorderPainted(true);
startButton.setBackground(Color.lightGray);
startButton.setSize(100,100);
startButton.setText(" 开始 ");
stopButton.setText(" 停止 ") ;
stopButton.addActionListener(new MyZipFrame_stopButton_actionAdapter(this));
startButton.addActionListener(new MyZipFrame_startButton_actionAdapter(this));
messageLabel.setText("欢迎使用myZip 进度:");//考虑加入文件信息
jMenuFileOpenDir.setText("压缩目录");
jMenuFileOpenDir.addActionListener(new MyZipFrame_jMenuFileOpenDir_actionAdapter(this));
//listPanel.setPreferredSize(new Dimension(295,400));
textArea.setPreferredSize(new Dimension(800,600));
jMenuFile.add(jMenuFileOpenFile);
jMenuFile.add(jMenuFileOpenDir);
jMenuFile.add(jMenuFileOpenZip);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
textArea.setForeground(Color.BLUE);
//plvector=new DefaultListModel();
//playlist=new JList(plvector);
//playlist.setPreferredSize(new Dimension(800,600)) ;
//playlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//playlist.addListSelectionListener(this);
//playlist.setBackground(Color.WHITE );
//playlist.setForeground(Color.BLACK);
//playlist.addMouseListener(new MouseAdapter() {
/**
* if double clicked then play the selected file
* @param e MouseEvent
*/
//public void mouseClicked(MouseEvent e) {
//if (e.getClickCount() == 2) {
//处理,文件夹->打开,文件->压缩,压缩文件->解压
//if(selectedFile.endsWith(".zip")){
//解压缩
//}
// else{
//压缩
//}
//}
// }
// });
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
//pljsp=new JScrollPane(playlist,v,h);
//pljsp.setWheelScrollingEnabled(true);
//listPanel.setLayout(new BorderLayout()) ;
//listPanel.add(pljsp,BorderLayout.CENTER);
this.setJMenuBar(jMenuBar1);
buttonPanel.add(startButton, null);
buttonPanel.add(stopButton,null) ;
buttonPanel.add(zipButton, null);
buttonPanel.add(unzipButton, null);
textArea.setEditable(false);
textPane =new JScrollPane(textArea,v,h);
textPane.setPreferredSize(new Dimension(295,400));
textPane.setWheelScrollingEnabled(true);
textPane.setAutoscrolls(true);
contentPane.add(buttonPanel, BorderLayout.NORTH);
centerPanel.setLayout(new BorderLayout());
//centerPanel.add(listPanel,BorderLayout.WEST);
centerPanel.add(textPane,BorderLayout.CENTER );
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(barPanel, BorderLayout.SOUTH);
barPanel.add(messageLabel, null);
barPanel.add(progressBar, null);
}
public void valueChanged(ListSelectionEvent e) {
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
MyZipFrame_AboutBox dlg = new MyZipFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
//将当前zip文件中的子文件显示在列表中
/* public void scanZipFile()
{
plvector.removeAllElements();
try
{
ZipInputStream zin=new ZipInputStream(new
FileInputStream(zipname));
ZipEntry entry;
while ((entry = zin.getNextEntry()) !=null)
{
plvector.addElement(entry.getName());
zin.closeEntry();
}
zin.close();
}
catch (IOException e)
{
e.printStackTrace() ;
}
}
public void loadZipFile(String name)
{
try
{
ZipInputStream zin =new ZipInputStream(new
FileInputStream(zipname));
ZipEntry entry;
while ((entry = zin.getNextEntry()) !=null)
{
if (entry.getName().equals(name))
{
BufferedReader in =new BufferedReader(new
InputStreamReader(zin));
//DataOutputStream out
String line;
while ((line = in.readLine())!=null)
{
}
}
zin.closeEntry();
}
zin.close();
}
catch(IOException e)
{
e.printStackTrace() ;
}
}*/
class Zip implements Runnable
{
public void run()
{
if(isCompress)
{
if(dirZip)
{
zipFiles(openDir_files);
if(stop)
{
File tarFile = new File(choosen + "\\" +
openDir_files[0].substring(openDir_files[0].lastIndexOf("\\") + 1) +
".zip");
tarFile.delete();
}
}
else
{
zipFiles(openFile_files);
if(stop)
{
File tarFile = new File(choosen + "\\" +
openFile_files[0].substring(openFile_files[0].lastIndexOf("\\") + 1) +
".zip");
tarFile.delete();
}
}
}
else if(isDecompress)
{
zipExtracting(selectedFile, firstFile);
if(stop)
{
File tarFile = new File(choosen+"\\"+selectedFile.substring(0,selectedFile.length()-4));
tarFile.delete();
}
}
}
/**
*<br>方法说明:实现文件的压缩处理
*<br>输入参数:压缩的文件数组
*<br>返回类型:
*/
public void zipFiles(String[] fs){
int mount=0;unZipSize=zipSize=0;
try{
//String fileName = fs[0];
String fileName = choosen+"\\"+fs[0].substring(fs[0].lastIndexOf("\\")+1);////////////////???
System.out.println("filename:!!!!!!! "+fileName);
FileOutputStream f =
new FileOutputStream(fileName+".zip",true);
//使用输出流检查
CheckedOutputStream cs =
new CheckedOutputStream(f,new Adler32());
//声明输出zip流
ZipOutputStream out =
new ZipOutputStream(new BufferedOutputStream(cs));
//写一个注释
out.setComment("A test of Java Zipping");
//对多文件进行压缩
for(int i=1;i<fs.length&&fs[i]!=null;i++){
System.out.println("Write file "+fs[i]);
File file=new File(fs[i]);
unZipSize+=(int)file.length();
textArea.append("正在压缩: "+
new File(fs[i]).getAbsolutePath()+
"\n");
DataInputStream in =
new DataInputStream(
new FileInputStream(fs[i]));
if(!fs[0].equalsIgnoreCase(fs[1]))
out.putNextEntry(new ZipEntry(fs[i].substring(fs[0].length()+1)));
else
out.putNextEntry(new ZipEntry(fs[i].substring(fs[0].lastIndexOf("\\")+1)));
int c;
while((c=in.read())!=-1)
{
mount++;
if(stop)
{
in.close();
out.close();
textArea.append("----------------------------"+"\n"
+"压缩已取消!"+"\n");
return;
}
out.write(c);
if(mount>=1000&&mount%1000==0)
{
progressBar.setValue(mount);
}
}
in.close();
}
//关闭输出流
out.close();
File zipFile=new File(fileName+".zip");
zipSize=(int)zipFile.length();
textArea.append("压缩完毕!"+"\n"
+"-----------压缩信息-----------"+"\n"
+"源文件大小: "+unZipSize+" Bytes\n"
+"压缩后大小: "+zipSize+" Bytes\n"
+"压缩比: "+(float)zipSize/(float)unZipSize*100+"%\n");
System.out.println("Checksum::"+cs.getChecksum().getValue());
}catch(Exception e){
System.err.println(e);
}
}
void zipExtracting(String filePath, String zipFile)
{
int BUFFER = 2048,mount=0;
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry = null;
ZipFile zipfile = null;
Enumeration enu = null;
try
{
zipfile = new ZipFile(zipFile);
enu = zipfile.entries();
String lastDir=choosen+"\\"+selectedFile.substring(0,selectedFile.length()-4);
new File(lastDir).mkdirs();
while (enu.hasMoreElements())
{
entry = (ZipEntry) enu.nextElement();
//logger.debug("extracting file name is" + entry.getName());
if (entry.isDirectory())
{System.out.println("in if entryName: "+entry.getName());
//new File(choosen+"\\"+selectedFile.substring(0,selectedFile.length()-4)+"\\"+entry.getName()).mkdirs();
continue;
}
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];System.out.println("entruName: "+entry.getName());
//FileOutputStream fos = new FileOutputStream(filePath.substring(0,filePath.length()-4)+"\\" + entry.getName());
File outputFile=new File(new File(choosen).getAbsolutePath()+"\\"+selectedFile.substring(0,selectedFile.length()-4)+"\\"+
entry.getName());
if(!outputFile.exists() )
{//int r=lastDir.indexOf(outputFile.getParent());
lastDir = outputFile.getParent();System.out.println("lastDbbbb: "+lastDir);
//lastDir = lastDir.substring(0, r + 1);System.out.println("lastDaaaa: "+lastDir);
new File(lastDir).mkdirs();
}
textArea.append("正在解压缩: "+outputFile.getAbsolutePath()+
"\n");
FileOutputStream fos = new FileOutputStream(choosen+"\\"+selectedFile.substring(0,selectedFile.length()-4)+"\\"+entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1)
{
mount++;
if(stop)
{
dest.close();
is.close();
textArea.append("--------------------------"+"\n"
+"解压缩已取消!"+"\n");
return;
}
System.out.println("mount::::::::::: "+mount);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -