2013-10-03 3 views
1

압축 된 파일을 가져 와서 압축을 풀고 압축 해제 된 모든 파일이 들어있는 새 파일/디렉토리를 반환하는 방법을 찾고 있습니다. 목표는 그 디렉토리를 가져 와서 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는, 그래서 결코 내 장점을 할 수 있었다 좋은 :)

+0

아아그. 너 나야? 이것은 바로 내가 며칠 전에하고 있었던 일입니다. @ 존 스킷에서 아래의 답변은 그것을 해결합니다. – tom

답변

2

좋아, 이제이 문제라고 생각 :

ZipInputStream zis = new ZipInputStream(in); 
out = new FileOutputStream(entryDestination); 
out.write(zis.read()); 
out.flush(); 
out.close(); 

당신은 새 파일을 만들 , 그리고 싱글 바이트을 쓰십시오. 그것은 모든 설명의 유효한 Excel 파일이 될 수 없습니다. finally 블록을 사용하여 스트림을 닫지 못하고 있지만 다른 문제입니다. GuavaByteStreams 및보고 -

,이 모든 세부 사항을 숨기기 위해 제 3 자 라이브러리를 사용하여 더 나을이라고 말했다
byte[] buffer = new byte[8192]; 
int bytes; 
while ((bytes = input.read(buffer)) > 0) { 
    output.write(buffer, 0, bytes); 
} 

: 다른 하나 개의 스트림의 내용을 복사하려면, 당신은 뭔가처럼 원하는 Files 클래스를 예로들 수 있습니다.

그런데 한 걸음 물러서서이 문제를 직접 발견하지 못한 이유가 있습니다. 예를 들어 내가했던 첫 번째 작업은 파일의 압축이 풀린 디렉토리를보고 파일을 열려고하는 것입니다. 1 바이트짜리 파일을 보는 것만으로도 조금 선물이 될 것입니다. 문제를 진단하려고 할 때, 큰 문제를 작은 문제로 나눌 수 있고, 작은 문제가 잘못되었음을 알아내는 것이 중요합니다.

+0

그랬습니다, 정말로 고마워요! – rearden

관련 문제