⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extractdlg.java

📁 Myzip a soft ware by java which make a file to zip or jar
💻 JAVA
字号:
/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.awt.Toolkit;
class ExtractDlg extends JDialog implements ActionListener
{        
    final static int SELECTED_FILE = 0;
		final static int ALL_FILE = 1;
		final static int USER_FILE = 2;

    int extractMode = SELECTED_FILE;
		JButton b1 = new JButton("Ok");
	  JButton b2 = new JButton("Cancel");
		JButton b3 = new JButton("Browse");
		JLabel l1 = new JLabel("Extract to");
		CheckboxGroup cbg = new CheckboxGroup();
		JRadioButton rb1 = new JRadioButton("Selected files",true);
		JRadioButton rb2 = new JRadioButton("All files",false);
		JRadioButton rb3 = new JRadioButton("File",false);
		ButtonGroup rbg = new ButtonGroup();
		JPanel rbpanel = new JPanel();
		
		JCheckBox cb1 = new JCheckBox("Overwrite existing  files");
		JTextField text1 = new JTextField(30);
		JTextField text2 = new JTextField(20);
		boolean canOverWrite = false;
		MainForm parent;
	  ProgressDlg pd;
	private void addComponent(Container c)
	{
	      c.add(b1);
				c.add(b2);
				c.add(b3);
				c.add(l1);
				c.add(text1);
				c.add(rbpanel);
				rbpanel.setLayout(new GridLayout(4,1));
				rbpanel.add(rb1);
				rbpanel.add(rb2);
				rbpanel.add(rb3);
				rbpanel.add(text2);
				rbpanel.setBorder(new TitledBorder(new EtchedBorder(),"Title"));
				rbg.add(rb1);
				rbg.add(rb2);
				rbg.add(rb3);
				c.add(cb1);
	}
	
	public ExtractDlg(MainForm parent)
	{
	  super(parent,"Extract",true);
		this.parent = parent;
	  Container content = getContentPane();
		addComponent(content);
    GridBagLayout gdb = new GridBagLayout();
		content.setLayout(gdb);
		GridBagConstraints c = new GridBagConstraints();
    text2.setEnabled(false);
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		gdb.setConstraints(l1,c);
		
		c.gridx = 0;
		c.gridy = 1;
		gdb.setConstraints(text1,c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 5;	
		gdb.setConstraints(rbpanel,c);
		
		c.gridx = 0;
		c.gridy = 7;
		c.gridwidth = 1;
		c.gridheight = 1;	
		gdb.setConstraints(cb1,c);
		
				
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.insets = new Insets(0,0,0,0);	
		gdb.setConstraints(b3,c);
		b3.addActionListener(new ActionListener()
		{
		  public void actionPerformed(ActionEvent e)
			{
			    JFileChooser fc = new JFileChooser();
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					fc.setFileFilter(new FileFilter()
					{
					  public boolean accept(File f)
						{
						   return f.isDirectory();
	
						}
						public String getDescription()
						{
						  return "Directory";
						}
					});
				 int result = fc.showOpenDialog(ExtractDlg.this);
				 if(result==JFileChooser.APPROVE_OPTION)
					{
					  text1.setText(fc.getSelectedFile().getPath());
					}

			}
		});
		
		c.gridx = 1;
		c.gridy = 3;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.insets = new Insets(20,0,0,0);
		gdb.setConstraints(b1,c);
	  b1.addActionListener(new ActionListener()
		  {
			  public void actionPerformed(ActionEvent e)
				{
				 ExtractDlg.this.setVisible(false);
				 extractFile(); 
				 
			  }
			}
		);
		c.gridx = 1;
		c.gridy = 4;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.insets = new Insets(20,0,0,0);
		gdb.setConstraints(b2,c);
		b2.addActionListener(new ActionListener()
		  {
			  public void actionPerformed(ActionEvent e)
				{
				  
					ExtractDlg.this.setVisible(false);
			  }
			}
		);
    
		rb1.addActionListener(this);
		rb2.addActionListener(this);	
		rb3.addActionListener(this);
		cb1.addActionListener(this);
		text2.setBackground(Color.gray);
		setSize(320,250);
		
		addWindowListener(new WindowAdapter()
		{
		  public void windowClosing(WindowEvent e)
			{
			  	ExtractDlg.this.setVisible(false);
			}
		});

	}
	public boolean makeDir(String path)
	{
	  	int p = 0;
			int p1 = 0;
			String s = "";
			File f;
			while(((p1=path.indexOf('\\',p))!=-1))
			{
			  s =s + path.substring(p,p1)+"\\";
				f = new File(s);
				if(!f.exists())
				{
				  f.mkdir();
				}
				p = p1 + 1;
			}
		return true;
	}
	public boolean validPath(String path)
	{
	  File f = new File(path);
		if(!f.exists())
		{
		   int result = JOptionPane.showConfirmDialog(this,f.getName()+" not exist create new?","warning",JOptionPane.OK_CANCEL_OPTION);
		   if(result==JOptionPane.OK_OPTION)
				{
				  f.mkdir();
					return true;
				}
			 else
				{
				  return false;
				}
		}
		return true;
	}
	public void extractFile()
 {
	 String path = text1.getText();
	 if(path=="")
		{
		  path = ".\\";
		}
	 if(!path.endsWith("\\"))
		{
		  path = path + "\\";
		}
	 File f;
	 String fullname;
	 String s;
	 if(!validPath(path))
		{
		  return;
		}
	 if(!path.endsWith("\\"))
   path = path+"\\";	
	 switch(extractMode)
		{
		  case SELECTED_FILE:
			 {
				 int i = parent.table.getSelectedRow();
      	 if(i!=-1)
				{ 
				  
				  f = new File(path+(String)(parent.dm).getValueAt(i,0));
					 if(f.isDirectory()) return;
					if(f.exists() && (!canOverWrite))
					{
					  int result = JOptionPane.showConfirmDialog(this,f.getName()+" already exist overrite it?","warning",JOptionPane.OK_CANCEL_OPTION);
					  if(result==JOptionPane.OK_OPTION)
						{
						  parent.extractSelFile((String)(parent.dm).getValueAt(i,0),path+f.getName());
						}
					}
					else
					{
					 parent.extractSelFile((String)(parent.dm).getValueAt(i,0),path+f.getName());
				  }
				}
			 }
			 break;
			case ALL_FILE:
			  {

				//  if(pd==null) 
				//	  {
					//	  pd = new ProgressDlg(this.parent);
				//		}
         // pd.pb.setMinimum(0);
				//	pd.pb.setMaximum(parent.dm.getRowCount());
				//	pd.pb.setValue(0);
				//  pd.setLocation(new Point((Toolkit.getDefaultToolkit().getScreenSize().width/2)-150,(Toolkit.getDefaultToolkit().getScreenSize().height/2)-25));
				//	pd.setVisible(true);
				  for(int i=0;i<parent.dm.getRowCount();i++)
					{

						  s = (String)(parent.dm).getValueAt(i,0);
					  	s = s.replace('/','\\');
							makeDir(path+s);
							f = new File(path+(String)(parent.dm).getValueAt(i,0));
							if(f.isDirectory()) continue;
							if(f.exists() && (!canOverWrite))
							{
							    int result = JOptionPane.showConfirmDialog(this,f.getName()+" already exist overrite it?","warning",JOptionPane.OK_CANCEL_OPTION);
									if(result==JOptionPane.OK_OPTION)
					         	{
										//	pd.pb.setValue(pd.pb.getValue()+1);	
											parent.extractSelFile((String)(parent.dm).getValueAt(i,0),path+s);
										//	ExtractThread et = new ExtractThread(parent,(String)(parent.dm).getValueAt(i,0),path+s);
										//	et.start();
					          }
							}
							else
							{
							///pd.pb.setValue(pd.pb.getValue()+1);
							 parent.extractSelFile((String)(parent.dm).getValueAt(i,0),path+s);
							// ExtractThread et = new ExtractThread(parent,(String)(parent.dm).getValueAt(i,0),path+s);
							// et.start();
					    }
			  	}
					//pd.setVisible(false);
				}
			 break;
			case USER_FILE:
			  {
				  String username = text2.getText();
					String entryname = "";
					boolean r = false;
					File fn;
					File fn1;
				  for(int i=0;i<parent.dm.getRowCount();i++)
					{
					  fn = new File(username);
						fn1 = new File(((String)(parent.dm).getValueAt(i,0)).replace('/','\\'));
						if(fn.getName()==fn1.getName())
						{
						  r = true;
							entryname = (String)(parent.dm).getValueAt(i,0);
						  break;
						}
					}
					if(r)
					{
					   f = new File(path+username);
				  		if(f.exists() && (!canOverWrite))
							{
							    int result = JOptionPane.showConfirmDialog(this,f.getName()+" already exist overrite it?","warning",JOptionPane.OK_CANCEL_OPTION);
									if(result==JOptionPane.OK_OPTION)
					         	{
						          
											parent.extractSelFile(entryname,path+username);
					          }
							}
							else
							{
							 parent.extractSelFile(entryname,path+username);
					    }
					}
					else
					{
					  JOptionPane.showMessageDialog(this,username+" not exists in zip file","error",JOptionPane.ERROR_MESSAGE);
					}
				}
			 break;
		}

	 
	 
 }
	public void actionPerformed(ActionEvent evt)
		{
		  Object source = evt.getSource();
		if(source==rb1)
			{
			  text2.setEnabled(false);
				text2.setBackground(Color.gray);
				extractMode = SELECTED_FILE;
			}
		if(source==rb2)
			{
			  text2.setEnabled(false);
				text2.setBackground(Color.gray);
				extractMode = ALL_FILE;
			}
    if(source==rb3)
			{
			  text2.setEnabled(true);
				text2.setBackground(Color.white);
				extractMode = USER_FILE;
			}
		if(source==cb1)
			{
			  canOverWrite = cb1.isSelected(); 
			}
}
  public static void main(String[] args)
	{
	  ExtractDlg dlg = new ExtractDlg(null);
		dlg.setVisible(true);
	}
}

class ExtractThread extends Thread
{
  String n;
	String p;
	MainForm parent;
	ExtractThread(MainForm parent,String fname,String fpath)
	{
	   n = fname;
		 p = fpath;
		 this.parent = parent; 
	}
	public void run()
	{
	  parent.extractSelFile(n,p); 
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -