解压时出现问题

发布于 2024-11-08 15:37:56 字数 1587 浏览 0 评论 0原文

嘿,我正在尝试使用此类解压缩文件:

 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!谁能告诉我如何解决这个问题?谢谢。

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 in Main.java

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2024-11-15 15:37:56

呵呵,

你正在使用与我完全相同的解压缩类,并且我的工作正常。

您是否 100% 确定您的 zipFile 变量正在访问实际文件?您是否确保将 android.permission.WRITE_EXTERNAL_STORAGE 添加到您的清单中,并且您的 SDCard 未安装到您的计算机上?

如果是这样,您可以尝试使用 zipFile 路径创建一个 File 对象并运行 file.exists() 只是为了确保一切都按应有的方式设置。祝你好运!

Huh,

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

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?

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文