ZIP文件不能读取!!大家帮帮忙!!
在程序运行时有如下信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ZipTestFrame.scanZipFile(ZipTest.java:78)
at ZipTestFrame$OpenAction.actionPerformed(ZipTest.java:70)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2013)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2336)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:260)
at javax.swing.AbstractButton.doClick(AbstractButton.java:375)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1689)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1733)
at java.awt.Component.processMouseEvent(Component.java:6100)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3288)
at java.awt.Component.processEvent(Component.java:5865)
at java.awt.Container.processEvent(Container.java:2110)
at java.awt.Component.dispatchEventImpl(Component.java:4461)
at java.awt.Container.dispatchEventImpl(Container.java:2168)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4466)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4130)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4060)
at java.awt.Container.dispatchEventImpl(Container.java:2154)
at java.awt.Window.dispatchEventImpl(Window.java:2555)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:605)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:276)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)
程序如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
public class lianxi {
public static void main(String[] args)
{
ZipTestFrame f=new ZipTestFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class ZipTestFrame extends JFrame
{
public ZipTestFrame()
{
setTitle("ZipTest");
setSize(100,100);
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("文件");
JMenuItem menu1=new JMenuItem("打开");
menu1.addActionListener(new OpenAction());
JMenuItem menu2=new JMenuItem("退出");
menu2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
menu.add(menu1);
menu.add(menu2);
menuBar.add(menu);
setJMenuBar(menuBar);
JTextArea filetext=new JTextArea();
final JComboBox fileCombo=new JComboBox();
fileCombo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
loadZipFile((String)fileCombo.getSelectedItem());
}
});
add(fileCombo,BorderLayout.SOUTH);
add(new JScrollPane(filetext),BorderLayout.CENTER);
}
private class OpenAction implements ActionListener
{
public void actionPerformed(ActionEvent event){
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
ExtensionFileFilter filter=new ExtensionFileFilter();
filter.addExtension(".zip");
filter.addExtension(".jar");
filter.setDescription("ZIP archives");
chooser.setFileFilter(filter);
int r=chooser.showOpenDialog(ZipTestFrame.this);
if(r==JFileChooser.APPROVE_OPTION){
zipname = chooser.getSelectedFile().getPath();
scanZipFile();
}
}
}
public void scanZipFile()
{
fileCombo.removeAllItems();
try
{
ZipInputStream zin=new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
while((entry=zin.getNextEntry())!=null);
{
fileCombo.addItem(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;
filetext.setText("");
while((entry=zin.getNextEntry())!=null)
{
if(entry.getName().equals(name))
{
BufferedReader in=new BufferedReader(new InputStreamReader(zin));
String line;
while((line=in.readLine())!=null)
{
filetext.append(line);
filetext.append("\n");
}
}
zin.closeEntry();
}
zin.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private JComboBox fileCombo;
private JTextArea filetext;
private String zipname;
}
class ExtensionFileFilter extends FileFilter
{
public void addExtension(String extension)
{
if(!extension.startsWith("."));
extension="."+extension;
extensions.add(extension.toLowerCase());
}
public void setDescription(String aDescription)
{
description=aDescription;
}
public String getDescription()
{
return description;
}
public boolean accept(File f)
{
if(f.isDirectory()) return true;
String name=f.getName().toLowerCase();
for(String e:extensions)
if (name.endsWith(e))
return true;
return false;
}
private String description="";
private ArrayList <String> extensions=new ArrayList <String>();
}
希望高手们帮帮忙!!我弄了很久了 不得其解!!
[解决办法]
好久以前根据网上做的,看看吧,
- Java code
package zip;//在classpath中引入ant.jar的包import java.io.*;import java.util.*;import java.io.File;import org.apache.tools.zip.*;public class CDGGzip{ //boolean packFrame = false; private File srcPath =new File("E:\\电影\\新建文件夹"); private String outFilename=new String("d:"+File.separator+"a.rar");private int len=srcPath.listFiles().length;private String[] filenames = new String[len];public void setSrcPath(String src){ srcPath=new File(src); } public File getSrcPath(){ return srcPath; }public void setOutFilename(String out){ outFilename=out; } public String getOutFilename(){ return outFilename; } public void gzip(){byte[] buf = new byte[1024]; try { File[] files = srcPath.listFiles(); for(int i=0;i<len;i++) { //if(!files[i].isDirectory())filenames[i]=srcPath.getPath()+File.separator+files[i].getName();} ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); for (int i=0; i<filenames.length; i++) { FileInputStream in = new FileInputStream(filenames[i]); out.putNextEntry(new org.apache.tools.zip.ZipEntry(files[i].getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); } catch (IOException e) { System.out.println(e); } } public static void main(String[] args) { CDGGzip f=new CDGGzip(); f.gzip(); }}
[解决办法]
你的代码问题点如下:
1,你声明了private JComboBox fileCombo;也准备用他了,但是你却一直没有new他.
你的这句代码final JComboBox fileCombo=new JComboBox();象是初始化fileCombo;但是你却又定义了一次,所以导致你的类对象没有得到初始化.
请删除上面代码的"final JComboBox ",这样你的类成员private JComboBox fileCombo就得到初始化了.这个空指针异常就没有了
2,scanZipFile()方法里面的while语句后面多了一个";",所一当你fileCombo.addItem(entry.getName()); 的时候肯定是空指针
请消除这个";"
所有问题都解决了!
[解决办法]
1,你声明了private JComboBox fileCombo;也准备用他了,但是你却一直没有new他.
你的这句代码final JComboBox fileCombo=new JComboBox();象是初始化fileCombo;但是你却又定义了一次,所以导致你的类对象没有得到初始化.
请删除上面代码的"final JComboBox ",这样你的类成员private JComboBox fileCombo就得到初始化了.这个空指针异常就没有了
2,scanZipFile()方法里面的while语句后面多了一个";",所一当你fileCombo.addItem(entry.getName()); 的时候肯定是空指针
请消除这个";"
