Java-Zip文件目录问题

Java-Zip文件目录问题,java,file,io,zip,Java,File,Io,Zip,我仍在使用另一个问题中的同一个应用程序,在开始使用ZipExtractToFile void之前,我一直都很好,代码可以处理zip根目录中的文件,但无法处理dir,以下是控制台的输出: Exception in thread "main" java.io.FileNotFoundException: minecraft\achievement\bg.png (The system cannot find the path specified) at java.io.FileOutputStrea

我仍在使用另一个问题中的同一个应用程序,在开始使用ZipExtractToFile void之前,我一直都很好,代码可以处理zip根目录中的文件,但无法处理dir,以下是控制台的输出:

Exception in thread "main" java.io.FileNotFoundException: minecraft\achievement\bg.png (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.hachisoftware.mmi.system.Util.ZipExtractToFile(Util.java:56)
at com.hachisoftware.mmi.MinecraftModInstaller.startSystem(MinecraftModInstaller.java:51)
at com.hachisoftware.mmi.MinecraftModInstaller.main(MinecraftModInstaller.java:27)
错误位于:

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outDir + "\\" + ze.getName())));

首先,您过早地使用了这一行:

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outDir + "\\" + ze.getName())));
这是因为
FileOutputStream
需要一个文件,而不是目录,在这个阶段,您不确定
outDir+“\\”+ze.getName()
是否是文件

你应该把那条线放在后面

if(ze.isDirectory()) {...}
其次,如果您知道您正在创建一个新目录或文件,并且您知道它的父目录或文件;最好使用此构造函数:

public File(File parent, String child)


如果不小心,使用像
outDir+“\\”+ze.getName()
这样的代码更可能导致错误。

对于初学者,不要使用字符串连接创建
文件
对象。使用
文件(字符串父级,字符串子级)
构造函数。如果您仍然遇到异常,请在问题中发布更新。
public File(File parent, String child)