簡體   English   中英

解壓縮時出現問題

[英]Problem when Unzipping

嘿,我正在嘗試使用此類解壓縮文件:

 public class Decompress {
 private String _zipFile; 
  private String _location; 

  public Decompress(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 

    _dirChecker(""); 
  } 

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

        if(ze.isDirectory()) { 
          _dirChecker(ze.getName()); 
        } else { 
          FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
          for (int c = zin.read(); c != -1; c = zin.read()) { 
            fout.write(c); 
          } 

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

      } 
      zin.close(); 
    } catch(Exception e) { 
      Log.e("Decompress", "unzip", e); 
    } 

  } 

  private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 

    if(!f.isDirectory()) { 
      f.mkdirs(); 
    } 
  } 

}

Main.java中的代碼

           String zipFile = Environment.getExternalStorageDirectory() + "/tmp.zip"; 

    String unzipLocation = Environment.getExternalStorageDirectory() + "/tmp/"; 
    Decompress d = new Decompress(zipFile, unzipLocation); 
    d.unzip(); 

問題出在這一行:while((ze = zin.getNextEntry())!= null){

zin.getNextEntry()始終為null! 誰能告訴我該如何解決? 謝謝。

咦,

您正在使用精確的解壓縮類,而我的工作正常。

您是否100%確定zipFile變量正在訪問實際文件? 您是否確定將android.permission.WRITE_EXTERNAL_STORAGE添加到清單中,並且SD卡未安裝到計算機上?

如果是這樣,您可以嘗試使用zipFile路徑創建File對象並運行file.exists(),以確保一切都按原樣設置。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM