2012-05-11 3 views
2

우리가 확인하는 방법 zip 파일 손상되거나 유효 Zip 파일 내 code` 그것을확인 ZipInputStream 여부를 zip 파일 유효 그것을 추출 가기 전에

를 추출하는

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

public void unzip() { 
     FileInputStream fin = null; 
     ZipInputStream zin = null; 
     OutputStream fout = null; 

    File outputDir = new File(_location); 
    File tmp = null; 

    try { 
     fin = new FileInputStream(_zipFile); 
     zin = new ZipInputStream(fin); 
     ZipEntry ze = null; 
     while ((ze = zin.getNextEntry()) != null) { 
      Log.d("Decompress", "Unzipping " + ze.getName()); 

      if (ze.isDirectory()) { 
       dirChecker(ze.getName()); 
      } else { 
       tmp = File.createTempFile("decomp", ".tmp", outputDir); 
       fout = new BufferedOutputStream(new FileOutputStream(tmp)); 
       DownloadFile.copyStream(zin, fout, _buffer, BUFFER_SIZE); 
       zin.closeEntry(); 
       fout.close(); 
       fout = null; 
       tmp.renameTo(new File(_location + ze.getName())); 
       tmp = null; 
      } 
     } 
     zin.close(); 
     zin = null; 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } finally { 
     if (tmp != null ) { try { tmp.delete();  } catch (Exception ignore) {;} } 
     if (fout != null) { try { fout.close();  } catch (Exception ignore) {;} } 
     if (zin != null ) { try { zin.closeEntry(); } catch (Exception ignore) {;} } 
     if (fin != null ) { try { fin.close();  } catch (Exception ignore) {;} } 
    } 
} 

`

이 가기 전에 여부 유효한 zipfile로 잘 작동하지만 아무 것도 생성하지 않는 예외는 throw하지 않습니다. 그러나 압축을 풀기 전에 zip 파일의 유효성을 확인해야합니다.

답변

1

Zip 파일이 두 가지 이유로 손상되었는지 확인하는 것은 거의 쓸모가 없다고 생각합니다.

  1. 일부 zip 파일에는 zip 부품보다 많은 바이트가 포함되어 있습니다. 예를 들어, 자동 압축 해제 아카이브는 실행 가능 부분을 가지고 있지만 여전히 유효한 zip입니다.
  2. 파일 크기가 변경되지 않고 손상 될 수 있습니다.

따라서 CRC가 손상되었는지 확인하는 방법으로 CRC를 계산하는 것이 좋습니다.

1

Zip 파일은 Zip 항목 카탈로그가있는 한 유효합니다. zip 명령을 사용하면 카탈로그가있는 한 찾아 볼 수 있습니다. 테스트에 사용되는 매개 변수는 추출 및 CRC 검사를 실제로 수행합니다.

당신이 할 수있는 일은 Java의 temp dir 생성 기능을 사용하여 임시 폴더에서 CRC 검사를 추출하고 수행하는 것입니다. 그런 다음 모두 성공적이면 temp dir에서 최종 대상으로 파일을 복사하여 압축을 푸십시오.