[英]Error while extracting a zip file
我正在嘗試使用java.util.zip軟件包解壓縮壓縮文件夾:
現在我的壓縮文件夾結構是:我的壓縮文件夾名稱是classes.zip在這個zip文件夾中,我有一個classes文件夾,里面有子文件夾和文件:
如果您進一步進入www文件夾,則它又有一個子文件夾,它是一個Java包,而在包結構化文件夾中,我有.class文件。
現在,我正在嘗試解壓縮該壓縮文件夾,我的代碼是:package www.eor.com;
/**
* 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)
為什么會給出此異常,以及如何糾正此問題?
可能是由於文件classes/
的父classes/
不存在,所以無法在其中創建文件。
提取zip條目時,必須為該文件創建父文件夾。 zip不一定包含每個文件夾的條目。 但是zip中的每個條目都采用path/to/entry/filename.ext
因此您可以導出條目的父路徑並相應地創建父文件夾。
所以在提取文件之前
new File(filePath).getParent().mkdirs()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.