从资产中解压缩zip文件

时间:2015-10-30 11:13:51

标签: android unzip

我尝试从外部存储资产中解压缩文件

public class Decompress {

public static void unzipFromAssets(Context context, String zipFile, String destination) {
    try {
        if (destination == null || destination.length() == 0)
            destination = context.getFilesDir().getAbsolutePath();
        InputStream stream = context.getAssets().open(zipFile);
        unzip(stream, destination);
    } catch (IOException e) {
        e.printStackTrace();
    }
}    

public static void unzip(InputStream stream, String destination) {
    dirChecker(destination, "");
    byte[] buffer = new byte[BUFFER_SIZE];
    try {
        ZipInputStream zin = new ZipInputStream(stream);
        ZipEntry ze = null;

        while ((ze = zin.getNextEntry()) != null) {
            Log.v(TAG, "Unzipping " + ze.getName());

            if (ze.isDirectory()) {
                dirChecker(destination, ze.getName());
            } else {
                File f = new File(destination + ze.getName());
                f.mkdirs();
                File file = new File(f, destination + ze.getName());

                    FileOutputStream fout = new FileOutputStream(file);
                    int count;
                    while ((count = zin.read(buffer)) != -1) {
                        fout.write(buffer, 0, count);
                    }
                    zin.closeEntry();
                    fout.close();
            }

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

}

private static void dirChecker(String destination, String dir) {
    File f = new File(destination + dir);

    if (!f.isDirectory()) {
        boolean success = f.mkdirs();
        if (!success) {
            Log.w(TAG, "Failed to create folder " + f.getName());
        }
    }
}

每次都会出现此错误:

  

java.io.FileNotFoundException:filewebViewControllers / aboutAs / index.html / filewebViewControllers / aboutAs / index.html:open failed:ENOENT(没有这样的文件或目录)

1 个答案:

答案 0 :(得分:0)

试试这个,

  

在创建对象时将zipfile位置和要保存解压缩文件的位置传递给此类...并且   调用解压缩方法......

public class Decompress { 
  private String zip; 
  private String loc; 

  public Decompress(String zipFile, String location) { 
    zip = zipFile; 
    loc = location; 

    dirChecker(""); 
  } 

  public void unzip() { 
    try  { 
      FileInputStream fin = new FileInputStream(zip); 
      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(loc + 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(); 
    } 
  } 
}

为ssdcard创建新文件夹以存放文件:

String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
    //create folder
    File folder = new File(dir); //folder name
    folder.mkdirs();

    //create file
    File file = new File(dir, "filename.extension");

请对您的清单文件授予以下权限:

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