【问题标题】:Cannot unzip file in android programmatically无法以编程方式在android中解压缩文件
【发布时间】:2013-09-20 07:39:03
【问题描述】:

我正在尝试在 android 中创建一个移动字典应用程序。作为应用程序的一部分,我试图将压缩的数据库文件从资产文件夹复制到 /data/data/package_name/app_database/file__0/。我正在使用下面的代码来执行此操作。 以下代码无法解压缩并将数据库复制到所需位置,但在应用第二次启动时可以正常工作。

public class MainActivity extends DroidGap {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try
    {
        String pName = this.getClass().getPackage().getName();
    this.copy("0000000000000001.zip","/data/data/"+pName+"/app_database/file__0/");
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    super.setIntegerProperty("splashscreen", R.drawable.splash);
    super.loadUrl("file:///android_asset/www/index.html", 10000);
}

void copy(String file, String folder) throws IOException {
    File CheckDirectory;
    CheckDirectory = new File(folder);
    if (!CheckDirectory.exists())
    { 
        CheckDirectory.mkdir();
    }

    InputStream in = getApplicationContext().getAssets().open(file);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));
    try {
        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            String outputfilename = ze.getName();
            OutputStream out = new FileOutputStream(folder+outputfilename);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int count;
            while ((count = zis.read(buffer)) != -1) {
                baos.write(buffer, 0, count);
                byte[] bytes = baos.toByteArray();
                out.write(bytes);             
                baos.reset();
            }
            out.flush();
            out.close();
        }
    }
    finally {
        zis.close();
        in.close();
    }

}   

}

提前致谢。

【问题讨论】:

    标签: android sqlite cordova android-sqlite unzip


    【解决方案1】:

    我通过用 CheckDirectory.mkdirs() 替换 CheckDirectory.mkdir() 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多