【问题标题】:Get percentage of extraction with XZ Java for android使用 XZ Java for android 获取提取百分比
【发布时间】:2015-01-18 06:25:07
【问题描述】:

我正在使用 XZ Java 库在 Android 上提取大小约为 16MB 的 .xz 文件。我将提取/解压缩代码作为AsyncTask 运行,因此我想通过onProgressUpdate(Integer ... values) 方法查看提取的百分比。

我的解压代码看起来像这样。

        byte[] buf = new byte[8192];
        String name = null;

        try {
            name = "my_archive.xz";
            InputStream in = getResources().openRawResource(R.raw.my_archive);//new FileInputStream(name); //
            FileOutputStream out = openFileOutput("my_archive.sqlite", Context.MODE_PRIVATE);

            label = (TextView)findViewById(R.id.textLabel);
            try {
                in = new XZInputStream(in);

                label.setText("Writing db file.");
                int size;
                while ((size = in.read(buf)) != -1) {
                    out.write(buf,0,size);
                    progress++;
                    publishProgress(progress);
                }

            }
            catch (Exception e)
            {
                System.err.println("Input Stream error: "+e.getMessage());
            }
            finally {
                // Close FileInputStream (directly or indirectly
                // via LZMAInputStream, it doesn't matter).
                in.close();
            }


        } catch (FileNotFoundException e) {
            System.err.println("LZMADecDemo: Cannot open " + name + ": "
                    + e.getMessage());
            System.exit(1);

        } catch (EOFException e) {
            System.err.println("LZMADecDemo: Unexpected end of input on "
                    + name);
            System.exit(1);

        } catch (IOException e) {
            System.err.println("LZMADecDemo: Error decompressing from "
                    + name + ": " + e.getMessage());
            System.exit(1);
        }

progress 变量实际上应该保存百分比值。如果有人使用过这个库,如果你想出了一个简单的方法来计算进度百分比,请在这里帮助我。

提前感谢您的帮助。

【问题讨论】:

    标签: java android android-asynctask xz


    【解决方案1】:

    我尝试使用输入流上的available() 方法获取存档文件的大小,如下所示。

    InputStream in = getResources().openRawResource(R.raw.my_archive);
    int fileSize = in.available();
    

    在提取过程中,我计算了如下进度:

                    int size;
                    int counter=0;
                    while ((size = in.read(buf)) != -1) {
                        out.write(buf,0,size);
                        counter++;
                        progress = (int) (counter*100*1024/(double)fileSize);
                        publishProgress(progress);
                    }
    

    但是,由于某种原因,这不会导致正确的进展。在完成之前,进度高达 108%。我知道我在这里做错了,所以请用正确的计算来改进这个答案。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多