압축 된 파일을 가져 와서 압축을 풀고 압축 해제 된 모든 파일이 들어있는 새 파일/디렉토리를 반환하는 방법을 찾고 있습니다. 목표는 그 디렉토리를 가져 와서 Excel 문서를 추출한 다음 내가 만든 통합 문서 클래스로 변환하는 것입니다 (이것은 완전히 테스트되었고 잘 작동합니다). 문제는 내가 다음과 같은 예외 얻고 있다는 것입니다 :ZipException : zip 파일을 열 때 오류가 발생했습니다.
private Workbook createWorkbook(File file) {
File unZipedFile = unZip(file);
File[] files = unZipedFile.listFiles();
Workbook wBook = null;
for (int i = 0; i < files.length; i++) {
if (files[i].getName().contains(".xls")) {
try {
File f = files[i];
ZipFile zip = new ZipFile(f);
wBook = new Workbook(zip);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
return wBook;
}
private File unZip(File input) {
File output = new File("unzippedFile");
OutputStream out = null;
try {
ZipFile zipFile = new ZipFile(input);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File entryDestination = new File(output, entry.getName());
entryDestination.getParentFile().mkdirs();
InputStream in = zipFile.getInputStream(entry);
ZipInputStream zis = new ZipInputStream(in);
out = new FileOutputStream(entryDestination);
out.write(zis.read());
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
}
나는이 압축 해제 방법에 문제가 알고 여기
java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:215)
at java.util.zip.ZipFile.<init>(ZipFile.java:145)
at java.util.zip.ZipFile.<init>(ZipFile.java:159)
at com.atd.core.datamigrator.BulkImageUpload.createWorkbook(BulkImageUpload.java:54)
at com.atd.core.datamigrator.BulkImageUpload.importImages(BulkImageUpload.java:38)
at com.atd.core.datamigrator.BulkImageUpload.main(BulkImageUpload.java:236)
내 코드입니다 때문에 (파일 F = 새 파일을 사용할 때 "some path") 압축 해제 된 파일을 사용하는 대신 잘 동작합니다. 또한
, 파일 I/O는, 그래서 결코 내 장점을 할 수 있었다 좋은 :)
아아그. 너 나야? 이것은 바로 내가 며칠 전에하고 있었던 일입니다. @ 존 스킷에서 아래의 답변은 그것을 해결합니다. – tom