解压缩文件不起作用

时间:2013-12-23 16:16:16

标签: java android unzip

我想在android上解压缩一个zip文件。我搜索并找到了这段代码;我试图实现它没有成功。有人能告诉我为什么吗?我似乎无法找到问题。

我复制了SD卡上的main.zip文件,但是在运行程序后,我在SD卡上找不到unzipFile文件夹。为什么会出现这种情况?

另外,如果我要解压缩资产文件夹的文件,我如何在资产中获取此文件的路径并将此路径保存在String变量中?感谢。

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.e("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:

public class UnZipFile extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.e("he", "heee");
    Toast.makeText(this, "in activity", Toast.LENGTH_LONG).show();
    String zipFile = Environment.getExternalStorageDirectory()
            + "/main.zip";// "file:///android_asset/main.zip";
    String unzipLocation = Environment.getExternalStorageDirectory()
            + "/unzipFile/";
    Decompress d = new Decompress(zipFile, unzipLocation);
    d.unzip();

}
 }

的manifest.xml:

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

1 个答案:

答案 0 :(得分:0)

也许,你无法访问SD卡。你检查一下,创建一个文件并检查是否存在。