我有.pkg文件,我想用Java提取文件的数据。每当我尝试通过ZipFile
提取数据时,它都会引发异常:
java.io.FileNotFoundException: C:\Users\kapilb\Desktop\kapilBaghel\dmserver.pkg (The system cannot find the file specified)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:214)
at java.util.zip.ZipFile.<init>(ZipFile.java:144)
at java.util.zip.ZipFile.<init>(ZipFile.java:158)
at dm.com.app.core.database.Testing.unZip(Testing.java:22)
at dm.com.app.core.database.Testing.main(Testing.java:61)
代码:
public static void unZip(File objZipFile, File objDestDir) {
try {
ZipFile zipFile = new ZipFile(objZipFile);
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = zipEntry.getName();
File file = new File(objDestDir, name);
if (name.endsWith("/")) {
file.mkdirs();
continue;
}
File parent = file.getParentFile();
if (parent != null) {
parent.mkdirs();
}
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
}
is.close();
fos.close();
}
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}