2015-01-18 6 views
1

XZ Java 라이브러리를 사용하여 크기가 약 16MB 인 Android에서 .xz 파일을 추출합니다. 추출/압축 풀기 코드를 AsyncTask으로 실행 중이므로 onProgressUpdate(Integer ... values) 메서드를 통해 추출의 백분율을보고 싶습니다.android의 XZ Java 추출 비율 얻기

내 압축 풀기 코드는 다음과 유사합니다.

 byte[] buf = new byte[8192]; 
     String name = null; 

     try { 
      name = "my_archive.xz"; 
      InputStream in = getResources().openRawResource(R.raw.my_archive);//new FileInputStream(name); // 
      FileOutputStream out = openFileOutput("my_archive.sqlite", Context.MODE_PRIVATE); 

      label = (TextView)findViewById(R.id.textLabel); 
      try { 
       in = new XZInputStream(in); 

       label.setText("Writing db file."); 
       int size; 
       while ((size = in.read(buf)) != -1) { 
        out.write(buf,0,size); 
        progress++; 
        publishProgress(progress); 
       } 

      } 
      catch (Exception e) 
      { 
       System.err.println("Input Stream error: "+e.getMessage()); 
      } 
      finally { 
       // Close FileInputStream (directly or indirectly 
       // via LZMAInputStream, it doesn't matter). 
       in.close(); 
      } 


     } catch (FileNotFoundException e) { 
      System.err.println("LZMADecDemo: Cannot open " + name + ": " 
        + e.getMessage()); 
      System.exit(1); 

     } catch (EOFException e) { 
      System.err.println("LZMADecDemo: Unexpected end of input on " 
        + name); 
      System.exit(1); 

     } catch (IOException e) { 
      System.err.println("LZMADecDemo: Error decompressing from " 
        + name + ": " + e.getMessage()); 
      System.exit(1); 
     } 

변수 progress은 실제로 백분율 값을 보유해야합니다. 누구든지이 라이브러리를 사용하고 있으며 진행률을 계산할 수있는 쉬운 방법을 찾으면 여기에서 나를 도와주세요.

도움을 미리 감사드립니다.

답변

1

아래와 같이 입력 스트림에서 available() 메서드를 사용하여 보관 파일의 크기를 가져 오려고했습니다.

InputStream in = getResources().openRawResource(R.raw.my_archive); 
int fileSize = in.available(); 

그리고 추출 과정에서

, 나는 아래와 같이 진행 상황을 계산 :

   int size; 
       int counter=0; 
       while ((size = in.read(buf)) != -1) { 
        out.write(buf,0,size); 
        counter++; 
        progress = (int) (counter*100*1024/(double)fileSize); 
        publishProgress(progress); 
       } 

그러나, 이것은 어떤 이유에 대한 올바른 진행을 초래하지 않습니다. 완료는 108 %까지 진행됩니다. 내가 여기서 잘못된 것을하고 있다는 것을 알고 있으므로 올바른 계산으로이 대답을 개선하십시오.

감사합니다.