2012-03-01 4 views
1

저는 Java 메커니즘을 사용하여 zip 파일을 추출합니다. 제목에 액센트가있는 파일이 없으면 메커니즘이 잘 작동합니다. 포르투갈 출신 이래로, ç, ç, õ, é 등과 같은 문자는 대개 제 언어로 사용됩니다. 이 문자 중 하나라도 파일 이름에 있으면 IO 예외가 발생합니다.악센트가있는 zip 파일의 압축 해제

while (zipFileEntries.hasMoreElements()) { 
    ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();  
    File destFile = new File(unzipDestinationDirectory, currentEntry); 
    File destinationParent = destFile.getParentFile(); 

    // create the parent directory structure if needed 
    destinationParent.mkdirs(); 

    // extract file if not a directory 
    if (!entry.isDirectory()) {     
     BufferedInputStream is = 
      new BufferedInputStream(zip_file.getInputStream(entry)); 
     int currentByte;     
     byte data[] = new byte[BUFFER]; 

     // write the current file to disk 
     FileOutputStream fos = new FileOutputStream(destFile);     
     BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); 

     // read and write until last byte is encountered 
     while ((currentByte = is.read(data, 0, BUFFER)) != -1) { 
      dest.write(data, 0, currentByte); 
     } 

     dest.flush(); 
     dest.close(); 
     is.close(); 
    } 

그것은이 문제를 해결하기 위해 어떤 해결 방법을 알고 있습니까

java.io.IOException: Stream closed 
    at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134) 
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) 
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:258) 
    at java.io.BufferedInputStream.read(BufferedInputStream.java:317) 
    at parsers.ZipParser.decompressZipFile(ZipParser.java:83) 
    at poc.MainPOC.main(MainPOC.java:61) 

while((currentByte = is.read(data, 0, BUFFER)) != -1)에 충돌? 압축 해제하지 않고 압축 파일 안의 파일 이름을 변경할 수 있습니까?

답변

0

압축 및 압축 해제시 인코딩을 올바르게 설정해야한다고 생각합니다. ZIP 파일을 만들 때 UTF-8로 만들었습니까? 그렇지 않은 경우 시도해 보는 것이 좋습니다.

+0

저는 입력 zip 파일을 만드는 책임자가 아닙니다. 나는 zip 파일이 어떻게 생성되는지에 어떤 영향도 미치지 않습니다 ... – nunoaac

+0

당신은 그 영향력을 찾아야 만합니다. 그렇지 않으면 효과가 없습니다. 아마도 당신은 그들을 만들고있는 파트너와 대화해야 할 것입니다. – duffymo

+0

이것은 실제로 옵션이 아닙니다 :/java.util.zip을 Apache Commons IO lib로 대체합니다. – nunoaac

3

Java 7부터 문자 집합에 파일 이름으로 사용할 문자를 지정하는 새로운 구문이 ZipInputStream입니다. 설명서 here을 참조하십시오. 그것을 사용하는 방법에 대한 좀 더 많은 정보를 가지고

ZipInputStream zis = new ZipInputStream(new FileInputStream("your zip file"), Charset.forName("Encoding here")); 

참조 Charset :

그래서 당신은 같은 것을 사용하여 ZipInputStream을 만들 것입니다.

파일을 읽는 방식이 변경되지 않으므로 내용을 읽으려면 다른 해결 방법이 필요합니다. 그러나 자세한 내용은이 대답 Java zip character encoding을 참조하십시오. 아마 일부 코드를 다시 사용할 수 있습니다.

+1

당신이 제공 한 답변을 확인하고''Charset.forName ("ISO-8859-1")'을 프랑스어 억양에 사용했습니다. 타이 – WannaGetHigh

관련 문제