2017-05-01 9 views
0

서버에서 내부 저장 장치로 zip 파일을 다운로드 중입니다. 그리고 다운로드 후 내부 저장 장치에 그 파일의 압축을 풀기를 원합니다. 내 파일이 내부 저장소에 다운로드되었으며 압축을 풀어 볼 수는 있지만 읽을 수는 없습니다. 아래는 내 unzipfile() 메소드입니다. 아무도 내가 잘못 가고 있다고 말할 수 있습니까?안드로이드는 내부 저장 장치에서 zip 파일을 압축합니다.

public void unzipfile() 
    { 
     try { 

      Log.d("unzipiing","unzipping"); 
      ContextWrapper cw = new ContextWrapper(context); 
      String name_="foldername"; //Folder name in device android/data/ 
      File directory = cw.getDir(name_, Context.MODE_PRIVATE); 
      File mypath=new File(directory,"dwnld"); 

      FileOutputStream fout = new FileOutputStream(mypath); 

      File yourFile = new File(directory,"dwnld.zip"); 
      Log.d("unzipiing","filepath -" + yourFile.getPath()); 

      FileInputStream fin = new FileInputStream(yourFile.getPath()); 
      ZipInputStream zin = new ZipInputStream(fin); 
      Log.d("unzipiing","zin size -" + zin.available()); 
      // zin.available() give -1 in console log 

      BufferedInputStream in = new BufferedInputStream(zin); 
      BufferedOutputStream out = new BufferedOutputStream(fout); 


     byte b[] = new byte[zin.available()]; 
     int n; 
      Log.d("unzip","n - " + in.read(b,0,1024)); 
     while ((n = in.read(b,0,1024)) >= 0) { 
      out.write(b,0,n); 
      Log.d("unzip byte"," - " + n); 
     } 

     out.flush(); 
     out.close(); 
      in.close(); 
      fin.close(); 
      zin.close(); 
     } 
     catch (Exception e){ 
     } 
    } 
+0

는 아직 명확하지 않다 경로에 대해이 방법. 대체 솔루션을 사용해보십시오. http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android –

+0

링크 및 도움에 감사드립니다. 그 해결책을 시도 할 것입니다. 하지만이 코드가 작동하지 않는 이유를 알고 싶습니다 .-- ( –

+0

잠시 동안 코드를 주석 처리하고 다음 세트의 솔루션으로 이동하고 무료로 코드를 디버그하려고 할 때 –

답변

0
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

사용 압축 해제

public void unzip(String _zipFile, String _targetLocation) { 

      //create target location folder if not exist 
      dirChecker(_targetLocatioan); 

      try { 
       FileInputStream fin = new FileInputStream(_zipFile); 
       ZipInputStream zin = new ZipInputStream(fin); 
       ZipEntry ze = null; 
       while ((ze = zin.getNextEntry()) != null) { 

        //create dir if required while unzipping 
        if (ze.isDirectory()) { 
         dirChecker(ze.getName()); 
        } else { 
         FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName()); 
         for (int c = zin.read(); c != -1; c = zin.read()) { 
          fout.write(c); 
         } 

         zin.closeEntry(); 
         fout.close(); 
        } 

       } 
       zin.close(); 
      } catch (Exception e) { 
       System.out.println(e); 
      } 
    } 

체크 정확히 문제가

public void dirChecker(String filepath) 
{ 
File file = new File(filePath); 
if(file.exists())  
//Do something 
else 
// Do something else. 
}