2013-07-02 3 views
0

파일을 압축 해제하려고 할 때 특수 문자가있는 파일이 있습니다.Java (ZipEntry) - 파일 이름에 체코 문자 (기타 등등)가 들어 있으면 다음 항목을 읽는 동안 오류가 발생합니다.

이미지 파일과 함께 zip 파일 갤러리가 있다고 가정 해 보겠습니다.

gallery.zip 
    - file01.jpg 
    - dařbuján.jpg 

내 방법은 시작 :

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream) 
     throws IOException { 
    List<File> files = new LinkedList<File>(); 
    ZipEntry entry = null; 
    int count; 
    byte[] buffer = new byte[BUFFER]; 

    while ((entry = inputStream.getNextEntry()) != null) { 

그것은 inputStream.getNextEntry() 나는 때문에 체코어 문자 "R"와 "A"의 파일 에게 dařbuján.jpg을 읽으려고하면 실패합니다. 공백이있는 다른 파일 (예 : 25.jpg 또는 단순히 file.jpg 등)과 잘 작동합니다. 도와 줄수있으세요?

+1

그리고 그것은 ... 정확히 어떻게? –

+0

java.lang.IllegalArgumentException –

+0

예, 그동안 귀하의 문제를 봤습니다. 쉽게 찾을 수 있습니다. –

답변

0

처럼

ZipInputStream(InputStream in, Charset charset) 

를 사용 캐릭터 세트 지정ZipInputStream 만들기, 나는 평민 - 압축과 그것을 해결. 만약 누군가가 내 방법에 관심이 있다면 :

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream, 
     File tempFile) throws IOException { 
    List<File> files = new LinkedList<File>(); 
    int count; 
    byte[] buffer = new byte[BUFFER]; 

    org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(tempFile, "UTF-8"); 
    Enumeration<?> entires = zf.getEntries(); 
    while(entires.hasMoreElements()) { 
     org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry = (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)entires.nextElement(); 
     if(entry.isDirectory()) { 
      unzipDirectoryZipEntry(files, entry); 
     } else {    
      InputStream zin = zf.getInputStream(entry);     

      File temp = File.createTempFile(entry.getName().substring(0, entry.getName().length() - 4) + "-", "." + entry.getName().substring(entry.getName().length() - 3, entry.getName().length()));          

      OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temp), BUFFER); 
      while ((count = zin.read(buffer, 0, BUFFER)) != -1) { 
       outputStream.write(buffer, 0, count); 
      } 
      outputStream.flush(); 
      zin.close(); 
      outputStream.close(); 
      files.add(temp);    
     } 
    } 
    zf.close(); 
    return files; 
} 
2

이 좋아

new ZipInputStream(inputStream, Charset.forName("UTF-8")); 
+0

그것을 시도 할 것이다. –

+0

음, ZipInputStream이 하나의 매개 변수만을 받아 들인다는 문제가 있습니다 ... InputStream in. –

+0

오, 알겠습니다. 이것은 Java 7입니다. –