2012-07-31 3 views
2

파일이 있습니다. test.zip. winrar와 같은 ZIP 도구를 사용하면 쉽게 추출 할 수 있습니다 (test.csv에 test.zip의 압축을 풉니 다). 그러나 test.csv는 UTF8 형식이 아닙니다. 내 문제는 여기에 자바를 사용하여 압축을 풀 때이 파일을 읽을 수 없다는 것입니다.Java에서 UTF8 형식이 아닌 파일의 압축을 해제하는 방법

ZipFile zf = new ZipFile("C:/test.zip"); 

예외가 발생하면 해당 파일을 열면 오류가 발생합니다.

On Java http://java.sun.com/developer/technicalArticles/Programming/compression/에는 데이터 형식 지정에 대한 내용이 없습니다. 아마도 전체 API는 UTF8 형식 데이터 용으로 만 설계되었을 것입니다. UTF8 형식 이외의 데이터를 압축 해제해야하는 경우 압축을 해제하는 방법은 무엇입니까? 특히 더 많은 공간 크기를 보유하는 일본어 및 중국어 문자 (UTF8 제외). 또한이 문제가 언급 된 http://truezip.java.net/6/tutorial.html에 API를 발견했습니다. 그러나, 나는 그것을 해결하는 방법에 대한 길을 얻지 못했습니다. 이 문제를 해결할 수있는 간단한 방법이 있습니까? 특히 JAVA 사양 요청에서 전달 된 API에서.

+0

어떤 종류의 "파일 열기 오류"입니까? –

+1

보고있는 오류를 설명하십시오. 또한 WinRar 대신 Winzip으로 파일을 열어보십시오. 귀하의 zip 파일은 아마도 처음에는 zip 형식이 아닙니다. 이것은 UTF-8과 아무 관련이 없습니다. –

답변

2

아니, zip 파일은 UTF-8 데이터를하지있습니다. Zip 파일은 파일 내의 데이터를 전혀 해석하지 않으며 Java API도 해석하지 않습니다.

ASCII가 아닌 파일의 이름은이지만 파일 내용 자체는 전혀 문제가되지 않습니다. 귀하의 경우 파일 이름이 단지 test.zip 인 것처럼 보이므로 이름 인코딩 문제가 발생하지 않아야합니다.

파일을 으로 열 수 없으면 다른 문제가있는 것 같습니다. 파일이 예상대로 존재하는지 확신합니까?

+0

UTF 8에있는 파일의 압축을 풀 때 코드가 작동합니다. 코드를 unfip 할 파일이 utf8 형식이 아닐 때 ZipInputStream zis = new ZipInputStream (new FileInputStream (zipFile)); ZipEntry ze = zis.getNextEntry(); ze가 null입니다. –

+0

@abishkar : 그럴 것 같지 않습니다. 문제를 보여주는 짧지 만 완전한 프로그램을 보여주십시오. –

0

나는 이것이 파일 이름이 UTF8로 인코딩되지 않은 경우에만 발생한다는 것을 기억합니다.

세 번째 구성 요소를 사용할 수없는 경우 Apache Zip API를 사용해보십시오.

import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile;

0

아래 코드를 사용해 볼 수 있습니까? 더 많은 예제 여기 http://java2novice.com/java-collections-and-util/zip/unzip/

FileInputStream fis = null; 
    ZipInputStream zipIs = null; 
    ZipEntry zEntry = null; 
    try { 
     fis = new FileInputStream(filePath); 
     zipIs = new ZipInputStream(new BufferedInputStream(fis)); 
     while((zEntry = zipIs.getNextEntry()) != null){ 
      try{ 
       byte[] tmp = new byte[4*1024]; 
       FileOutputStream fos = null; 
       String opFilePath = "C:/"+zEntry.getName(); 
       System.out.println("Extracting file to "+opFilePath); 
       fos = new FileOutputStream(opFilePath); 
       int size = 0; 
       while((size = zipIs.read(tmp)) != -1){ 
        fos.write(tmp, 0 , size); 
       } 
       fos.flush(); 
       fos.close(); 
      } catch(Exception ex){ 

      } 
     } 
     zipIs.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
0

확인 내가 모든 zip 파일

try 
    { 

     final ZipFile zf = new ZipFile("C:/Documents and Settings/satheesh/Desktop/POTL.Zip"); 

     final Enumeration<? extends ZipEntry> entries = zf.entries(); 
     ZipInputStream zipInput = null; 

     while (entries.hasMoreElements()) 
     { 
      final ZipEntry zipEntry=entries.nextElement(); 
      final String fileName = zipEntry.getName(); 
     // zipInput = new ZipInputStream(new FileInputStream(fileName)); 
      InputStream inputs=zf.getInputStream(zipEntry); 
      // final RandomAccessFile br = new RandomAccessFile(fileName, "r"); 
       BufferedReader br = new BufferedReader(new InputStreamReader(inputs, "UTF-8")); 
       FileWriter fr=new FileWriter(f2); 
      BufferedWriter wr=new BufferedWriter(new FileWriter(f2)); 

      while((line = br.readLine()) != null) 
      { 
       wr.write(line); 
       System.out.println(line); 
       wr.newLine(); 
       wr.flush(); 
      } 
      br.close(); 
      zipInput.closeEntry(); 
     } 


    } 
    catch(Exception e) 
    { 
     System.out.print(e); 
    } 
    finally 
    { 
     System.out.println("\n\n\nThe had been extracted successfully"); 

    } 

이 코드는 정말 좋은 방식으로 나를 작동을 추출하는 데 사용 whic이 코드를 사용해보십시오.

3

JDK6에는 java.util.zip 구현의 버그가 있습니다. USASCII 이외의 문자는 처리 할 수 ​​없습니다. 아파치 커먼즈 commons-compress-1.0.jar 라이브러리를 사용하여 수정했습니다. JDK7은 java.util.zip 구현을 수정했습니다. http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html

import java.io.*; 
import org.apache.commons.compress.archivers.ArchiveEntry; 
import org.apache.commons.compress.archivers.zip.*; 

public static int unzip(File inputZip, File outputFolder) throws IOException { 
    int count=0; 
    FileInputStream fis = null; 
    ZipArchiveInputStream zis = null; 
    FileOutputStream fos = null; 
    try { 
     byte[] buffer = new byte[8192]; 
     fis = new FileInputStream(inputZip); 
     zis = new ZipArchiveInputStream(fis, "Cp1252", true); // this supports non-USACII names 
     ArchiveEntry entry; 
     while ((entry = zis.getNextEntry()) != null) { 
      File file = new File(outputFolder, entry.getName()); 
      if (entry.isDirectory()) { 
       file.mkdirs(); 
      } else { 
       count++; 
       file.getParentFile().mkdirs(); 
       fos = new FileOutputStream(file); 
       int read; 
       while ((read = zis.read(buffer,0,buffer.length)) != -1) 
        fos.write(buffer,0,read); 
       fos.close(); 
       fos=null; 
      } 
     } 
    } finally { 
     try { zis.close(); } catch (Exception e) { } 
     try { fis.close(); } catch (Exception e) { } 
     try { if (fos!=null) fos.close(); } catch (Exception e) { } 
    } 
    return count; 
} 
관련 문제