尽管 Java 解压缩 zip 文件时出现 FileNotFoundException
Getting FileNotFoundException while unzipping a zip file though Java
我有一个方法可以通过 Java.
解压缩 zip 文件
这个方法很管用。
但最近我试图使用此方法解压缩某个文件(final output.zip),但出现运行时异常。
下面是该方法的代码:
public static List<String> unzipFiles(File zipFile, File targetDirectory) {
List<String> files = new ArrayList<String>();
try (FileInputStream fileInputStream = new FileInputStream(zipFile); ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new BufferedInputStream(fileInputStream));){
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextZipEntry()) != null) {
int length;
byte data[] = new byte[bufferGlb];
String fileName = zipEntry.getName();
File opFile = new File(targetDirectory, fileName);
FileOutputStream fileOutputStream = new FileOutputStream(opFile);//This is the line where the exception is thrown
BufferedOutputStream dest = new BufferedOutputStream(fileOutputStream, bufferGlb);
while ((length = zipInputStream.read(data, 0, bufferGlb)) != -1) {
dest.write(data, 0, length);
}
dest.flush();
files.add(fileName);
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return files;
}
//library import used:
//import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
请务必注意,我能够使用 WinRar 成功解压缩此文件。
我得到异常的行在上面的代码中用注释标记了。
我得到的异常堆栈跟踪如下:
java.io.FileNotFoundException: final output/0123450.pdf (No such file or directory)
at java.io.FileOutputStream.open0(Native Method) ~[?:1.8.0_191]
at java.io.FileOutputStream.open(FileOutputStream.java:270) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:213) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:162) ~[?:1.8.0_191]
at ZipUtility.unzipFiles(ZipUtility.java:281)
此外,异常中提到的文件 0123450.pdf
实际上在 zip 文件中。
请帮忙。
我以前遇到过类似的问题。
异常中提到的文件:
java.io.FileNotFoundException: final output/0123450.pdf (No such file or directory)
at java.io.FileOutputStream.open0(Native Method) ~[?:1.8.0_191]
at java.io.FileOutputStream.open(FileOutputStream.java:270) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:213) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:162) ~[?:1.8.0_191]
at ZipUtility.unzipFiles(ZipUtility.java:281)
即 0123450.pdf 实际上在文件夹 final output
中,而不是直接在 zip 文件中。
尝试使用直接包含文件的 zip 文件
我有一个方法可以通过 Java.
解压缩 zip 文件这个方法很管用。
但最近我试图使用此方法解压缩某个文件(final output.zip),但出现运行时异常。
下面是该方法的代码:
public static List<String> unzipFiles(File zipFile, File targetDirectory) {
List<String> files = new ArrayList<String>();
try (FileInputStream fileInputStream = new FileInputStream(zipFile); ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new BufferedInputStream(fileInputStream));){
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextZipEntry()) != null) {
int length;
byte data[] = new byte[bufferGlb];
String fileName = zipEntry.getName();
File opFile = new File(targetDirectory, fileName);
FileOutputStream fileOutputStream = new FileOutputStream(opFile);//This is the line where the exception is thrown
BufferedOutputStream dest = new BufferedOutputStream(fileOutputStream, bufferGlb);
while ((length = zipInputStream.read(data, 0, bufferGlb)) != -1) {
dest.write(data, 0, length);
}
dest.flush();
files.add(fileName);
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return files;
}
//library import used:
//import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
请务必注意,我能够使用 WinRar 成功解压缩此文件。
我得到异常的行在上面的代码中用注释标记了。
我得到的异常堆栈跟踪如下:
java.io.FileNotFoundException: final output/0123450.pdf (No such file or directory)
at java.io.FileOutputStream.open0(Native Method) ~[?:1.8.0_191]
at java.io.FileOutputStream.open(FileOutputStream.java:270) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:213) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:162) ~[?:1.8.0_191]
at ZipUtility.unzipFiles(ZipUtility.java:281)
此外,异常中提到的文件 0123450.pdf
实际上在 zip 文件中。
请帮忙。
我以前遇到过类似的问题。 异常中提到的文件:
java.io.FileNotFoundException: final output/0123450.pdf (No such file or directory)
at java.io.FileOutputStream.open0(Native Method) ~[?:1.8.0_191]
at java.io.FileOutputStream.open(FileOutputStream.java:270) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:213) ~[?:1.8.0_191]
at java.io.FileOutputStream.<init>(FileOutputStream.java:162) ~[?:1.8.0_191]
at ZipUtility.unzipFiles(ZipUtility.java:281)
即 0123450.pdf 实际上在文件夹 final output
中,而不是直接在 zip 文件中。
尝试使用直接包含文件的 zip 文件