Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 第一个文件为空的Zip文件_Java - Fatal编程技术网

Java 第一个文件为空的Zip文件

Java 第一个文件为空的Zip文件,java,Java,我正在尝试创建一个包含多个文件的.zip文件。问题是,当我打开.zip文件时,第一个选择的文件(当使用JFileChooser拾取文件时)总是空的(该文件已创建,但大小较小),而其他文件则正常 我无法找出代码中的问题所在 fileName = fileChooser.getSelectedFile(); String zipFile="E:\\test.zip"; FileOutputStream fout = nu

我正在尝试创建一个包含多个文件的.zip文件。问题是,当我打开.zip文件时,第一个选择的文件(当使用JFileChooser拾取文件时)总是空的(该文件已创建,但大小较小),而其他文件则正常

我无法找出代码中的问题所在

fileName = fileChooser.getSelectedFile();
                    String zipFile="E:\\test.zip";
                    FileOutputStream fout = null;
                    try {
                        fout = new FileOutputStream(zipFile);
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                    ZipOutputStream zout=new ZipOutputStream(new BufferedOutputStream(fout));

                    for(int i=0;i<s.length;i++){

                    try {
                            outFilename = fileName.getAbsolutePath()+".zip";
                            System.out.println(s[i]);
                            FileInputStream in = new FileInputStream(s[i]);
                            zout.putNextEntry(new ZipEntry(s[i].getName()));
                            System.out.println(s[i].getName());
                        byte[] buf = new byte[1024];
                        int len;
                            while ((len = in.read(buf, 0 , 1024)) > 0)
                        {
                                zout.write(buf, 0, len);
                        }
                        zout.closeEntry();
                        in.close();

                    } catch (IOException e) {}}
                    try {
                        zout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
fileName=fileChooser.getSelectedFile();
String zipFile=“E:\\test.zip”;
FileOutputStream fout=null;
试一试{
fout=新文件输出流(zipFile);
}捕获(FileNotFoundException e1){
e1.printStackTrace();
}
ZipOutputStream zout=新的zipoutpstream(新的缓冲输出流(fout));
对于(int i=0;i 0)
{
写入(buf,0,len);
}
zout.closeEntry();
in.close();
}捕获(IOE){}
试一试{
zout.close();
}捕获(IOE异常){
e、 printStackTrace();
}

谢谢。

永远不要写空的
catch
块。至少,放置
e.printStackTrace()在那里。实际上,您应该删除所有的
catch
块,并将整个代码序列包含在一个try…catch(IOException)中。如果你不能打开zipFile,继续下去真的没有意义,是吗?如果我没有说清楚,我很抱歉。我可以打开zip文件,它是关于zip中的文件。第二个和第三个文件相应地工作,添加的第一个文件有问题。这就是它看起来的样子。如果您抑制IOExceptions,则完全可能是编写zip文件时出错,而您没有意识到这一点。