Java 提取zip文件时出错
我正在尝试使用java.util.zip包解压缩压缩文件夹: 现在,我的压缩文件夹结构是: 我的压缩文件夹名是classes.zip 在这个zip文件夹中,我有一个classes文件夹,里面有子文件夹和文件: 如果您进一步进入www文件夹,那么它有一个子文件夹,它是一个java包,在包结构的文件夹中,我有.class文件 现在我正在尝试解压缩此压缩文件夹,我的代码是: 包www.eor.comJava 提取zip文件时出错,java,Java,我正在尝试使用java.util.zip包解压缩压缩文件夹: 现在,我的压缩文件夹结构是: 我的压缩文件夹名是classes.zip 在这个zip文件夹中,我有一个classes文件夹,里面有子文件夹和文件: 如果您进一步进入www文件夹,那么它有一个子文件夹,它是一个java包,在包结构的文件夹中,我有.class文件 现在我正在尝试解压缩此压缩文件夹,我的代码是: 包www.eor.com /** * A console application that tests the UnzipU
/**
* A console application that tests the UnzipUtility class
*
*/
public class UnzipUtilityTest {
public static void main(String[] args) {
String zipFilePath = "D:/classes.zip";
String destDirectory = "D:/Dojo";
UnzipUtility unzipper = new UnzipUtility();
try {
unzipper.unzip(zipFilePath, destDirectory);
} catch (Exception ex) {
// some errors occurred
ex.printStackTrace();
}
}
}
支持类为:
package www.eor.com;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* This utility extracts files and directories of a standard zip file to
* a destination directory.
*/
public class UnzipUtility {
/**
* Size of the buffer to read/write data
*/
private static final int BUFFER_SIZE = 4096;
/**
* Extracts a zip file specified by the zipFilePath to a directory specified by
* destDirectory (will be created if does not exists)
* @param zipFilePath
* @param destDirectory
* @throws IOException
*/
public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
/**
* Extracts a zip entry (file entry)
* @param zipIn
* @param filePath
* @throws IOException
*/
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}
现在,当我运行UnzipUtilityTest类时,它会给我一个异常,如下所示:
java.io.FileNotFoundException: D:\Dojo\classes\camel-config-xml.xml (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at www.cognizant.com.UnzipUtility.extractFile(UnzipUtility.java:59)
at www.cognizant.com.UnzipUtility.unzip(UnzipUtility.java:41)
at www.cognizant.com.UnzipUtilityTest.main(UnzipUtilityTest.java:16)
java.io.FileNotFoundException:D:\Dojo\classes\camel-config-xml.xml(系统找不到指定的路径)
在java.io.FileOutputStream.open(本机方法)
位于java.io.FileOutputStream。(FileOutputStream.java:221)
位于java.io.FileOutputStream。(FileOutputStream.java:110)
www.cognition.com.unziputibility.extractFile(unziputibility.java:59)
www.cognition.com.unziputibility.unzip(unziputibility.java:41)
www.cognition.com.UnzipUtilityTest.main(UnzipUtilityTest.java:16)
为什么会出现此异常以及如何纠正此问题?可能是因为文件的父级
类/
不存在,因此无法在其中创建文件
提取zip条目时,必须为该文件创建父文件夹。zip不一定包含每个文件夹的条目。但是zip中的每个条目的格式都是path/to/entry/filename.ext
,因此您可以派生条目的父路径并相应地创建父文件夹
因此,在提取文件之前,请执行以下操作:
new File(filePath).getParent().mkdirs()
这可能是由于文件
classes/
的父级不存在,因此无法在其中创建文件
提取zip条目时,必须为该文件创建父文件夹。zip不一定包含每个文件夹的条目。但是zip中的每个条目的格式都是path/to/entry/filename.ext
,因此您可以派生条目的父路径并相应地创建父文件夹
因此,在提取文件之前,请执行以下操作:
new File(filePath).getParent().mkdirs()
错误消息显然告诉您找不到文件。检查给定位置是否存在该文件。查看第一个屏幕截图,该目录的名称似乎是
D:\classes\classes
,而不是D:\Dojo\classes
。尝试将ZipInputStream-zipIn=new-ZipInputStream(new-FileInputStream(zipFilePath))替换为ZipInputStream-zipIn=new-ZipInputStream(new-FileInputStream(new-File(zipFilePath));感谢Jesper和HolidayCoder的快速回复:)你们真是太棒了,错误消息显然告诉你们找不到文件。检查给定位置是否存在该文件。查看第一个屏幕截图,该目录的名称似乎是D:\classes\classes
,而不是D:\Dojo\classes
。尝试将ZipInputStream-zipIn=new-ZipInputStream(new-FileInputStream(zipFilePath))替换为ZipInputStream-zipIn=new-ZipInputStream(new-FileInputStream(new-File(zipFilePath));感谢Jesper和HolidayCoder的快速回复。:)你们真是太棒了谢谢Gerald。。你真是个救命恩人。。是的,这正是问题所在,你的指导为我解决了问题。非常感谢杰拉尔德。。你真是个救命恩人。。是的,这正是问题所在,你的指导为我解决了问题。。