📄 myzip.java
字号:
zout.putNextEntry(ze);
zout.closeEntry();
}
tmpzf.close();
zout.close();
zin.close();
File tmpfile = new File(zipname);
String tmpname = tmpfile.getPath();
System.out.println(tmpfile.delete());
tmpzip.renameTo(new File(tmpname));
}
catch(IOException e)
{
System.out.println(e);
}
}
public String addFile()
{
String entryname = "" ;
String fullname = "";
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("C:\\"));
BufferedInputStream bin = null;
BufferedOutputStream bout = null;
ZipOutputStream zout = null;
ZipInputStream zin = null;
File tmpzip = null;
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return true;
}
public String getDescription()
{
return "*.*";
}
});
int r = chooser.showOpenDialog(MainForm.this);
if(r==JFileChooser.APPROVE_OPTION)
{
fullname = chooser.getSelectedFile().getPath();
try
{
tmpzip = File.createTempFile("zip",".tmp",new File(".\\"));
bin = new BufferedInputStream(new FileInputStream(fullname));
zin = new ZipInputStream(new FileInputStream(zipname));
zout = new ZipOutputStream(new FileOutputStream(tmpzip));
ZipEntry ze;
int len = 0;
byte[] b = new byte[10240];
while((ze=zin.getNextEntry())!=null)
{
zout.putNextEntry(new ZipEntry(ze.getName()));
while((len=zin.read(b))!=-1)
{
zout.write(b,0,len);
}
zout.closeEntry();
zin.closeEntry();
}
ze = new ZipEntry(new File(fullname).getName());
zout.putNextEntry(ze);
while((len = bin.read(b))!=-1)
{
zout.write(b,0,len);
}
zout.closeEntry();
bin.close();
zout.close();
zin.close();
File tmpfile = new File(zipname);
String tmpname = tmpfile.getPath();
tmpfile.delete();
tmpzip.renameTo(new File(tmpname));
}
catch(IOException e)
{
}
return new File(fullname).getName();
}
else
{
return "";
}
}
public void initMenu()
{
m1.add(NewItem);
NewItem.setToolTipText("Create New Zip file");
m1.add(OpenItem);
OpenItem.setToolTipText("Open Zip file");
m1.add(CloseItem);
CloseItem.setToolTipText("Close Zip file");
m1.addSeparator();
m1.add(ExitItem);
ExitItem.setToolTipText("Exit");
mb.add(m1);
NewItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newZipFile();
}
}
);
CloseItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
zipname = null;
removeRows();
}
}
);
ExitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
OpenItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openZipFile();
}
});
maction.add(madd);
madd.setToolTipText("Add file to Zip");
maction.add(mdelete);
mdelete.setToolTipText("Delete file in Zip");
maction.add(mextract);
mextract.setToolTipText("Extract file");
maction.add(mview);
mview.setToolTipText("View file");
mb.add(maction);
madd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(zipname!=null)
{
addFile();
removeRows();
scanZipFile(zipname);
}
}
});
mextract.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(zipname == null) return;
if(ed==null)ed = new ExtractDlg(MainForm.this);
Toolkit tk1 = Toolkit.getDefaultToolkit();
ed.setLocation(new Point((tk1.getScreenSize().width/2)-160,(tk1.getScreenSize().height/2)-125));
ed.show();
}
});
mview.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(zipname==null) return;
if(vd==null)
{
vd =new ViewDlg();
}
int[] i = table.getSelectedRows();
if(i[0]!=-1)
{
vd.viewText(zipname,(String)dm.getValueAt(i[0],0));
}
}
});
mdelete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int[] i = table.getSelectedRows();
if((zipname!=null) && (i!=null) && (i[0]!=1) &&(i[0]<table.getRowCount()))
{
deleteZipFile((String)dm.getValueAt(i[0],0));
reloadZipFile();
}
}
});
muigroup.add(muiwindow);
muigroup.add(muimetal);
muigroup.add(muimotif);
muimetal.setSelected(true);
muistyle.add(muiwindow);
muiwindow.setToolTipText("Windows Style");
muistyle.add(muimetal);
muimetal.setToolTipText("Java Metal Style");
muistyle.add(muimotif);
muimotif.setToolTipText("CDE/Motif Style");
muiwindow.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try{
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(MainForm.this);
SwingUtilities.updateComponentTreeUI(ad);
}
catch(Exception e1)
{
}
}
});
muimetal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
try{
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(MainForm.this);
SwingUtilities.updateComponentTreeUI(ad);
}
catch(Exception e2)
{
}
}
});
muimotif.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try{
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(MainForm.this);
SwingUtilities.updateComponentTreeUI(ad);
}
catch(Exception e3)
{
}
}
});
mb.add(muistyle);
mhelp.add(mabout);
mb.add(mhelp);
mabout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(ad==null)
{
ad = new AboutDlg(MainForm.this);
}
ad.setVisible(true);
Toolkit tk = Toolkit.getDefaultToolkit();
ad.setLocation(new Point((tk.getScreenSize().width/2)-150,(tk.getScreenSize().height/2)-100));
}
}
);
setJMenuBar(mb);
setSize(640,480);
}
public void reloadZipFile()
{
if(zipname!=null)
removeRows();
scanZipFile(zipname);
}
public int getZipedCount(String filename)
{
int count = 0;
try
{
ZipFile zf = new ZipFile(filename);
count = zf.size();
zf.close();
}
catch(IOException e)
{
}
return count;
}
public void newZipFile()
{
String fname = "";
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("C:\\"));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
if(f.getName().toLowerCase().endsWith(".zip") || f.getName().toLowerCase().endsWith(".jar") || f.isDirectory())
return true;
else
return false;
}
public String getDescription()
{
return "zip and jar file";
}
});
int r = chooser.showOpenDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
fname = chooser.getSelectedFile().getPath();
if(!fname.endsWith(".zip"))
{
fname = fname+".zip";
}
if(new File(fname).exists())
{
int selection = JOptionPane.showConfirmDialog(this,"File "+fname+" already exists overwrite?","Warning!",JOptionPane.YES_NO_OPTION);
if(selection==JOptionPane.YES_OPTION)
{
zipname = fname;
try
{
new File(zipname).delete();
new File(zipname).createNewFile();
}
catch(IOException e)
{
}
removeRows();
return;
}
else
{
return;
}
}
else
{
zipname = fname;
try
{
new File(zipname).createNewFile();
}
catch(IOException e)
{
}
removeRows();
return;
}
}
}
}
public class MyZip
{
public static void main(String[] args)
{
MainForm frame = new MainForm();
frame.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -