我試圖用java.util.zip包解壓壓縮文件夾:錯誤而提取zip文件
現在我的壓縮文件夾結構爲: 我的壓縮文件夾的名字是classes.zip 裏面這個zip文件夾我有一個類文件夾中,我有子文件夾以及文件:
如果你的WWW文件夾中走的更遠然後再它的子文件夾,這是一個Java包,我有包裝結構文件夾內。類文件。
現在我想解壓縮這個壓縮文件夾,我的代碼是: 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)
爲什麼它給這個異常以及如何糾正這個問題?
錯誤消息顯然會告訴你有一個文件無法找到。檢查文件是否存在於給定位置。看看你的第一張截圖,它看起來像是名爲'D:\ classes \ classes'而不是'D:\ Dojo \ classes'。 – Jesper
嘗試更換 ZipInputStream zipIn =新ZipInputStream(新的FileInputStream(zipFilePath)) 到 ** ZipInputStream zipIn =新ZipInputStream(新的FileInputStream(新文件(zipFilePath))); ** – emotionlessbananas
感謝您的快速回復加斯帕和HolidayCoder。 :)你們真棒 – Roy