2012-03-12 2 views
1

Mac OS X 10.7.3에서 zip 파일을 처리 할 때 몇 가지 문제가 있습니다.java.util.zip.ZipException : 유효하지 않은 압축 방법

처리해야하는 제 3 자로부터 zip 파일을 받고 있습니다. 내 코드는 ZipInputStream을 사용하여이 작업을 수행합니다. 이 코드는 아무 문제없이 여러 번 사용되었지만이 특정 zip 파일에서는 실패합니다. 다음과 같이 내가 오류는 다음과 같습니다

java.util.zip.ZipException: invalid compression method 
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:185) 
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105) 
    at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source) 
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) 
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source) 
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) 
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) 
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) 
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) 

그것에 대해 봤 내가 ZipInputStream, 예를 들어, 몇 가지 문제가 있음을 볼 수있다 this one.

또한 Stackoverflow와 관련된 몇 가지 관련 질문을 발견했습니다. this one. 그러나 합당하고, 받아 들일 수있는 대답은 없습니다.

  1. 사람이에 대한 구체적인 해결책을 발견했습니다 :

    나는 몇 가지 질문이? 어떤 lates을 갱신 또는 동일한 기능 이있는 모두 함께 다른 JAR하지만 문제이 link

  2. , 언급 사용자 phobuz1처럼 그 " 표준이 아닌 압축 방법 (방법 6) 만약" 이 경우 문제가 발생합니다. 어떤 압축 방법 이 사용되는지 알아낼 방법이 있습니까? 내가 실패의 이유를 확신 할 수 있도록?

로컬 컴퓨터에서 파일의 압축을 풀고 다시 압축하면 일부 사용자와 마찬가지로 완벽하게 작동합니다.

편집 1 :

파일 내가 얻고 .zip 형식으로, 나는 그들이 그것을 압축하기 위해 사용하고있는 OS가/유틸리티 프로그램 모르는 것입니다. 내 로컬 컴퓨터에 나는 맥 OS X를 함께 제공되는 내장 된 압축 유틸리티를 사용하고

+0

어떤 압축 방법을 사용하고 있습니까? 나는 그것을 알 수 있겠습니까? – Lion

+0

@Lion : 내가 얻는 파일은'.zip '형식으로되어 있는데 압축하는 데 사용하는 OS/유틸리티 프로그램을 모르겠습니다. 내 로컬 컴퓨터에서 Mac OS X에서 제공되는 내장 된 zip 유틸리티를 사용하고 있습니다. 질문을 업데이트하는 좋은 설명 질문을 주셔서 감사합니다. – Bhushan

답변

1

내 Windows XP OS에서 Java의 다음 코드를 사용하여 폴더를 압축합니다. 최소한 사이드 노트로 유용 할 수 있습니다.이 코드에서


private boolean zipFiles(String srcFolder, String destZipFile) throws Exception 
{ 
    boolean result=false; 
    System.out.println("Program Start zipping the given files"); 
    //send to the zip procedure 
    zipFolder(srcFolder,destZipFile); 
    result=true; 
    System.out.println("Given files are successfully zipped"); 
    return result; 
} 

//zip the folders 
private void zipFolder(String srcFolder, String destZipFile) throws Exception 
{ 
    //create the output stream to zip file result 
    FileOutputStream fileWriter = new FileOutputStream(destZipFile); 
    ZipOutputStream zip = new ZipOutputStream(fileWriter); 
    //add the folder to the zip 
    addFolderToZip("", srcFolder, zip); 
    //close the zip objects 
    zip.flush(); 
    zip.close(); 
} 
//recursively add files to the zip files 
private void addFileToZip(String path, String srcFile, ZipOutputStream zip,boolean flag)throws Exception 
{ 
    //create the file object for inputs 
    File folder = new File(srcFile); 
    //if the folder is empty add empty folder to the Zip file 
    if (flag==true) 
    { 
     zip.putNextEntry(new ZipEntry(path + "/" +folder.getName() + "/")); 
    } 
    else 
    { 
     //if the current name is directory, recursively traverse it to get the files 
     if (folder.isDirectory()) 
     { 
      addFolderToZip(path, srcFile, zip); //if folder is not empty 
     } 
     else 
     { 
      //write the file to the output 
      byte[] buf = new byte[1024]; 
      int len; 
      FileInputStream in = new FileInputStream(srcFile); 
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); 

      while ((len = in.read(buf)) > 0) 
      { 
       zip.write(buf, 0, len); //Write the Result 
      } 
     } 
    } 
} 


//add folder to the zip file 
private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception 
{ 
    File folder = new File(srcFolder); 

    //check the empty folder 
    if (folder.list().length == 0) 
    { 
     System.out.println(folder.getName()); 
     addFileToZip(path , srcFolder, zip,true); 
    } 
    else 
    { 
     //list the files in the folder 
     for (String fileName : folder.list()) 
     { 
      if (path.equals("")) 
      { 
       addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip,false); 
      } 
      else 
      { 
       addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip,false); 
      } 
     } 
    } 
} 
, 당신은 두 개의 매개 변수를 전달하여 위의 방법 zipFiles(String srcFolder, String destZipFile)를 호출해야합니다. 첫 번째 매개 변수는 압축 될 폴더를 나타내며 두 ​​번째 매개 변수 destZipFile은 대상 우편 폴더를 나타냅니다.


다음 코드는 압축 된 폴더의 압축을 푸는 것입니다.

private void unzipFolder(String file) throws FileNotFoundException, IOException 
{ 
    File zipFile=new File("YourZipFolder.zip"); 
    File extractDir=new File("YourDestinationFolder"); 

    extractDir.mkdirs(); 

    ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFile)); 

    try 
    { 
     ZipEntry entry; 
     while ((entry = inputStream.getNextEntry()) != null) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.append("Extracting "); 
      sb.append(entry.isDirectory() ? "directory " : "file "); 
      sb.append(entry.getName()); 
      sb.append(" ..."); 
      System.out.println(sb.toString()); 

      File unzippedFile = new File(extractDir, entry.getName()); 
      if (!entry.isDirectory()) 
      { 
       if (unzippedFile.getParentFile() != null) 
       { 
        unzippedFile.getParentFile().mkdirs(); 
       } 

       FileOutputStream outputStream = new FileOutputStream(unzippedFile); 

       try 
       { 
        byte[] buffer = new byte[1024]; 
        int len; 

        while ((len = inputStream.read(buffer)) != -1) 
        { 
         outputStream.write(buffer, 0, len); 
        } 
       } 
       finally 
       { 
        if (outputStream != null) 
        { 
         outputStream.close(); 
        } 
       } 
      } 
      else 
      { 
       unzippedFile.mkdirs(); 
      } 
     } 
    } 
    finally 
    { 
     if (inputStream != null) 
     { 
      inputStream.close(); 
     } 
    } 
}