我从ZIP档案使用此代码(省略所有的catch语句和其他初始化语句)提取文件中提取的ZIP档案文件:错误在Java中
zipInputStream = new ZipInputStream(new FileInputStream(file));
zipFile = new ZipFile(file);
for (Enumeration<?> em = zipFile.entries(); em.hasMoreElements();) {
String extractedFileName = em.nextElement().toString();
ZipEntry outerZipEntry = zipInputStream.getNextEntry();
if (outerZipEntry.getName().contains(searchString)) {
extractedFile = new File(outputDir + outerZipEntry.getName());
out = new FileOutputStream(outputDir + extractedFileName);
byte[] buf = new byte[1024];
int len;
while ((len = zipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
break;
}
}
此代码工作正常时,在/archive.zip/file_i_need.txt中提取文件。
但是,当我试图从/archive.zip/folder1/file_i_need.txt中提取文件时,我尝试使用readLine()读取文件时收到异常java.lang.NullPointerException:
String line = null ;
BufferedReader input = new BufferedReader(newFileReader(extractedFile)) ;
while((line = input.readLine()) != null) {
...
}
我已经测试了这两种情况下,它似乎像当文件夹里面,因为extractedFileName是“文件夹/ file_i_need.txt”相比,只是“file_i_need.txt”这个代码将无法正常工作。
任何建议,你可以推荐?
谢谢!
发布完整堆栈跟踪。 – Wug 2012-08-13 19:22:20
错误实际上不会在代码中发生,它发生在我尝试从文件读取时发生。我正在更新帖子以获取更多信息。 – joshualan 2012-08-13 19:24:18
Dup http://stackoverflow.com/questions/1399126/java-util-zip-recreating-directory-structure – mazaneicha 2012-08-13 19:24:47