2012-06-15 2 views
3

저는 Zip 파일을 다운로드하고 로컬에서 압축을 해제하는 프로젝트를 진행하고 있습니다. 내가 부딪히는 문제는 압축 해제 프로세스가 5 % 시간처럼 작동한다는 것입니다.Android : 파일을 압축 해제하면 데이터 오류 또는 CRC 오류가 발생합니다.

때로는 작동하기 때문에이 시점에서는 나에게 미스터리이지만 대부분 데이터 또는 crc 오류가 발생합니다. zip 파일이 변경되지 않은 경우에도 erros로 전환됩니다.

형식이 잘못되었는지 궁금해하는 수많은 도구로 만든 zip 파일을 시도했습니다. 그러나 아무 소용이 없습니다. 터미널에서 생성 된 zip도 작동하지 않습니다.

여기 내 압축 풀기 코드입니다 :

try { 
    String _location = model.getLocalPath(); 
    FileInputStream fin = new FileInputStream(localFile); 
    ZipInputStream zin = new ZipInputStream(fin); 
    ZipEntry ze = null; 
    byte[] buffer = new byte[1024]; 
    while((ze = zin.getNextEntry()) != null) { 
     if(_cancel) break; 

     System.out.println("unzipping " + ze.getName()); 

     if(ze.isDirectory()) { 
      File f = new File(_location + ze.getName()); 
      f.mkdirs(); 
     } else { 

      FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
      for(int c = zin.read(buffer); c > 0; c = zin.read(buffer)) { 
       fout.write(buffer,0,c); 
      } 
      zin.closeEntry(); 
      fout.close(); 
     } 
    } 
    zin.close(); 

    if(_cancel) { 
     handler.post(dispatchCancel); 
     return; 
    } 

} catch(Exception e) { 
    System.out.println("UNZIP ERROR!"); 
    System.out.println(e.getMessage()); 
    System.out.println(e.toString()); 
    e.printStackTrace(); 
} 

그리고 여기 일반적으로 zip 파일을 만드는 방법은 다음과 같습니다.

java.util.zip.ZipException: CRC mismatch 
    at java.util.zip.ZipInputStream.readAndVerifyDataDescriptor(ZipInputStream.java:209) 
    at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:173) 
    at com.XX.XX.XXIssueDownloader$7.run(XXIssueDownloader.java:222) 
    at java.lang.Thread.run(Thread.java:1020) 

java.util.zip.ZipException: data error 
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:336) 
    at java.io.FilterInputStream.read(FilterInputStream.java:133) 
    at com.XX.XX.XXIssueDownloader$7.run(XXIssueDownloader.java:219) 
    at java.lang.Thread.run(Thread.java:1020) 

누구든지 나는 이러한 오류를 얻을 수 있습니다 이유는 어떤 생각을 가지고 여기에

$>zip -r myzip.zip myzip/ 

는 두 개의 오류 출력입니까? 나는 이것들과 함께 어디에도 가지 않을 것이다.

답변

6

Zip 파일을로드 할 때 매우 중요한 두 가지 사항이 있습니다.

  1. Accept-Encoding : 헤더가없는 요청 방법을 사용하고 있는지 확인하십시오. 요청에있는 경우 응답은 zip 파일이 아니며 gzip 압축 zip 파일입니다. 따라서 다운로드하는 동안 디스크에 직접 쓰는 경우 실제로는 zip 파일이 아닙니다.

    URL url = new URL(remoteFilePath); 
    URLConnection connection = url.openConnection(); 
    InputStream in = new BufferedInputStream(connection.getInputStream()); 
    FileOutputStream f = new FileOutputStream(localFile); 
    
    //setup buffers and loop through data 
    byte[] buffer = new byte[1024]; 
    long total = 0; 
    long fileLength = connection.getContentLength(); 
    int len1 = 0; 
    while((len1 = in.read(buffer)) != -1) { 
         if(_cancel) break; 
         total += len1; 
         _Progress = (int) (total * 100/fileLength); 
         f.write(buffer,0,len1); 
         handler.post(updateProgress); 
    } 
    f.close(); 
    in.close(); 
    
  2. 입력 및 출력 스트림을 사용하여 읽기 (버퍼)를 사용하거나 (버퍼) 방법, 당신은 (읽기/쓰기 사용해야 쓰지 않는다 : 당신은 zip 파일을로드하기 위해이 같은 것을 사용할 수 있습니다 버퍼, 0, len). 그렇지 않으면 당신이 쓰거나 읽는 것이 그 안에 쓰레기 데이터로 끝날 수 있습니다. 전자 (read (buffer))는 항상 전체 버퍼를 읽지 만 실제로는 전체 버퍼가 없을 수 있습니다. 예를 들어 루프의 마지막 반복 만 512 바이트를 읽는 경우입니다. 그래서 여기에 당신이 파일을 압축 해제 줄 방법은 다음과 같습니다

    String _location = model.getLocalPath(); 
    FileInputStream fin = new FileInputStream(localFile); 
    ZipInputStream zin = new ZipInputStream(fin); 
    ZipEntry ze = null; 
    
    while((ze = zin.getNextEntry()) != null) { 
         if(_cancel) break; 
         System.out.println("unzipping " + ze.getName()); 
         System.out.println("to: " + _location + ze.getName()); 
         if(ze.isDirectory()) { 
           File f = new File(_location + ze.getName()); 
           f.mkdirs(); 
         } else { 
           byte[] buffer2 = new byte[1024]; 
           FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
           for(int c = zin.read(buffer2); c > 0; c = zin.read(buffer2)) { 
             fout.write(buffer2,0,c); 
           } 
           zin.closeEntry(); 
           fout.close(); 
         } 
    } 
    zin.close(); 
    
+1

좋아, 내가 다운로드 zip 파일이 깨진해서,'CRC의 error'를 얻을. 내 코드에서'f.write (buffer, 0, len1);'대신'f.write (buffer)'를 사용하면이 문제가 해결됩니다. 감사. – wodong

+0

안드로이드 장치에서 apk 파일의 압축을 풀려고 할 때 crcError가 발생합니다. 여러분이 이야기하는 코드가 PC에서 Eclipse (또는 유사)를 사용한다고 생각합니다. Android 기기 (Android 4.1.2의 Samsung Galaxy Note 2)에서 물건을 지우려면이 오류를 어떻게 피할 수 있습니까? 감사합니다. :) –

+0

@ 우동 - 나는 이미 f.write (buffer, 0, len1)를 사용하고 있었다. 내 대답 코드 예제에서 – gngrwzrd

관련 문제