解压的问题时, [英] Problem when Unzipping

查看:141
本文介绍了解压的问题时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个类来解压缩文件:

Hey, I'm trying to unzip a file using this class:

 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(); 
    } 
  } 

}

code在Main.java

Code in Main.java

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

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

问题是在这条线:
           而((ZE = zin.getNextEntry())!= NULL){

The problem is in this line: while ((ze = zin.getNextEntry()) != null) {

zin.getNextEntry()总是空!谁能告诉我如何解决这一问题?谢谢你。

zin.getNextEntry() is always null! Can anyone tell me how to fix this? Thanks.

推荐答案

咦,

您正在使用的确切DECOM preSS类作为我和我的工作正常。

You're using the exact decompress class as I am and mine is working fine.

您100%确保您的zip文件变量访问实际的文件?你确定到android.permission.WRITE_EXTERNAL_STORAG​​E添加到您的清单,你的SD卡未安装到您的计算机?

Are you 100% sure that your zipFile variable is accessing an actual file? Did you make sure to add the android.permission.WRITE_EXTERNAL_STORAGE to your manifest and your SDCard is not mounted to your computer?

如果这样你可以尝试使用创建的压缩文件路径的File对象并运行file.exists()只是为了确保一切都设置了理所应当的。祝你好运!

If so you might try creating a File object using the zipFile path and run file.exists() just to be sure everything is set up as it should be. Good luck!

这篇关于解压的问题时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆