Android 中的解压数据库不可读

Unzipped DB in Android not readable

我正在尝试通过下载 SQLite 数据库并将其解压缩到正确位置来将其传输到应用程序中。解压缩后,我成功传输了数据库。我得到的错误是它找不到我查询的任何表。我也成功解压和阅读了普通的文本文件。

数据库有希伯来语和英语,但之前没有出现过问题。双语DB在未解压时复制成功,双语文本已成功解压阅读。尽管如此,仍有可能存在编码问题。这对我来说似乎很奇怪,因为正如您在下面的代码中看到的那样,我只是直接复制字节。

-编辑-

假设预压缩的数据库名为 test1.db。我压缩了它,将其放入应用程序中,解压缩并调用了 test2.db。当我对这两个运行 diff 命令时,没有区别。因此,android 读取文件的方式肯定存在技术问题/或者 android 上的编码问题可能在 pc 上不存在?

我讨厌做代码转储,但我会 post 我的 copyDatabase() 函数(有效)。这就是我之前在解压缩的数据库文件上运行它所使用的。我把它放在这里作为比较。现在我正在尝试使用 unzipDatabase() 函数(不起作用),并将其用于压缩的数据库文件。后一个函数是从 How to unzip files programmatically in Android?

复制的
private void copyDatabase() throws IOException{
    String DB_NAME = "test.db";
    String DB_PATH = "/data/data/org.myapp.myappname/databases/";
    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

private boolean unzipDatabase(String path)
{     
    String DB_NAME = "test.zip";
    InputStream is;
    ZipInputStream zis;
    try 
    {
        String filename;
        is = myContext.getAssets().open(DB_NAME);
        zis = new ZipInputStream(is);          
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) 
        {
            // write to a file
            filename = ze.getName();

            // Need to create directories if not exists, or
            // it will generate an Exception...
            if (ze.isDirectory()) {
                Log.d("yo",path + filename);
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
            }

            OutputStream fout = new FileOutputStream(path + filename);

            // reading and writing zip
            while ((count = zis.read(buffer)) != -1) 
            {
                fout.write(buffer, 0, count);             
            }
            
            fout.flush();
            fout.close();               
            zis.closeEntry();
        }

        zis.close();
    } 
    catch(IOException e)
    {
        e.printStackTrace();
        return false;
    }

    return true;
}

所以仍然不知道为什么,但是如果我先删除数据库的旧副本(位于DB_PATH + DB_NAME)然后在那里解压新的,问题就解决了.直接复制时我不需要这样做。

是的,这是一个文件覆盖问题...如果有人知道原因,请随时发表评论