2013-06-05 3 views
4

유니 코드 파일 이름으로 압축 파일을 압축 해제하는 방법은 무엇입니까? 이 내 코드입니다 :유니 코드 파일 이름을 사용하여 zip 파일 압축 해제

try { 
    ZipInputStream zis = new ZipInputStream(
      new FileInputStream(zipFile)); 
    ZipEntry ze = zis.getNextEntry(); 

    System.setProperty("file.encoding", "UTF-8"); 
    while (ze != null) { 
     String fileName = new String(ze.getName().getBytes("UTF-8")); 
     System.out.println(fileName); 
     File newFile = new File(outputFolder + File.separator + fileName); 

     BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(newFile)); 
     OutputStreamWriter osw = new OutputStreamWriter(outStream, Charset.forName("UTF-8")); 
     int ch; 
     StringBuffer buffer1 = new StringBuffer(); 
     while ((ch = zis.read()) > -1) { 
      buffer1.append((char) ch); 
     } 
     osw.write(buffer1.toString()); 
     osw.close(); 
     outStream.close(); 

     ze = zis.getNextEntry(); 
    } 

    zis.closeEntry(); 
    zis.close(); 
} catch (IOException ex) { 
    ex.printStackTrace(); 
} 

하지만 오류가 나타납니다 UTFDataFormatException :

06-05 08:46:33.394: W/System.err(777): java.io.UTFDataFormatException: bad second or third byte at 6 
06-05 08:46:33.394: W/System.err(777): at java.nio.charset.ModifiedUtf8.decode(ModifiedUtf8.java:56) 
06-05 08:46:33.426: W/System.err(777): at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:270) 
06-05 08:46:33.426: W/System.err(777): at com.learnlang.utility.ZipManager.unZipIt(ZipManager.java:62) 
06-05 08:46:33.434: W/System.err(777): at com.learnlang.HomeActivity$progressThread.run(HomeActivity.java:317) 

내 클래스는 ZipManager입니다.

이 문제를 해결하는 방법?

+0

예외의 전체 스택 추적을 제공하십시오. – Andremoniy

+0

06-05 08 : 46 : 33.394 : W/System.err (777) : java.io.UTFDataFormatException : 6 초에 잘못된 두 번째 또는 세 번째 바이트 06-05 08 : 46 : 33.394 : W/System.err (777) : \t at java.nio.charset.ModifiedUtf8.decode (ModifiedUtf8.java:56) 06-05 08 : 46 : 33.426 : W/System.err (777) : \t at java .util.zip.ZipInputStream.getNextEntry (ZipInputStream.java:270) 06-05 08 : 46 : 33.426 : com.learnlang.utility.ZipManager.unZipIt에서 W/System.err (777) : \t (ZipManager.java : 62) 06-05 08 : 46 : 33.434 : com.learnlang.HomeActivity $ progressThread.run (HomeActivity.java:317)에서 W/System.err (777) : \t 내 클래스가 ZipManager입니다. – shuttle1978

+0

파일 이름이 UTF-8로 인코딩 되었습니까? UTF-8 파일 이름을 디코딩하는 데 실패하는 것 같습니다. –

답변

0

예외에 따르면 파일이 실제로는 UTF-8 인코딩이 아닌 것 같습니다.

또한 파일 이름에는 문제가 없습니다. 이 줄은

String fileName = new String(ze.getName().getBytes("UTF-8")); 

입니다. ze.getName()은 이미 올바른 자바 문자열이므로 의미가 없습니다.

+0

내가 압축 해제하려고하는 파일 이름이 "de_Maße-und-Gewichte_de"인데 ze.getName()으로 만 시도했지만 작동하지 않습니다 – shuttle1978

+0

@ shuttle1978'작동하지 않는다는 것은 무엇을 의미합니까? '? 이 경우 어떤 종류의 예외가 발생합니까? – Andremoniy

+1

NewFile 파일 = 새로운 파일 사용하여 동일한 예외에는 UTFDataFormatException – shuttle1978