在Codename One中使用Zipme Library解压缩.zip文件夹

时间:2018-02-04 10:22:59

标签: codenameone unzip

这是我在这里提出的问题的后续跟进: What is the correct way to display a HTML file downloaded and saved in the file system using codename one?

我成功使用webview从文件夹中显示html文件。诀窍是添加" file:// "设置网址时如下所示:

browser.setURL(" file://" + str_homePath + str_filePath);

如果这不是您的设计,那么我可以提交RFE。尽管如此,接下来我遇到了一个解压缩挑战,我认为这可能与" file:// "上面的问题。

使用 Zipme cn1lib 解压缩.zip文件时,一切都在模拟器上提取,但在设备上失败。为了看看到底发生了什么,我不得不打电话。我发现有些文件没有被提取出来。

检查DDMS证实了我的发现。我经常看到这个错误:W / System.err:java.io.FileNotFoundException :(没有这样的文件或目录)

然后,当我尝试显示索引文件(因为上面丢失的文件而失败)时,我收到此错误: I / System.out:showKeyboard false I / System.out:showKeyboard false D / WebView:loadUrl = file:///data/user/0/ke.co.imedia.samplefilepath/files/17/rte.html W / cr_media:需要BLUETOOTH权限 E / libEGL:validate_display:99错误3008(EGL_BAD_DISPLAY)

以下是我用来解压缩的代码:

try
{
    System.out.println("Finding FILE TO UNZIP: ");
    InputStream zipFile = Storage.getInstance().createInputStream("folderName"+".zip");
    //InputStream zipFile = FileSystemStorage.getInstance().openInputStream(fs.getAppHomePath()+"folderName"+".zip");
    fs.mkdir(fs.getAppHomePath()+"folderName");
    System.out.println("ZIPPED FILE FOUND: ");
    Unzip(zipFile, "/"+ "folderName");
    System.out.println("UNZIPPED SUCCESFULLY");
}
catch (IOException ex)
{
    Log.p(ex.getMessage(), 0);
    ex.printStackTrace();
}
catch (Exception ex)
{
    ex.printStackTrace();
}

以上代码从下面的链接调用以下方法,我希望通过检查解压缩过程中的下一个条目是否是目录来允许提取到文件夹的一些编辑。 https://github.com/codenameone/ZipSupport

public void Unzip(InputStream is_zipFile, String str_dirDest) 
 {
    InputStream is;
    try 
    {
        is = is_zipFile;
        ZipInputStream zipStream = new ZipInputStream(is);
        ZipEntry entry;

        // create a buffer to improve copy performance later.
        byte[] buffer = new byte[2048];
        System.out.println("TRYING TO UZIP: ");

        while ((entry = zipStream.getNextEntry()) != null) 
        {
            FileSystemStorage fs = FileSystemStorage.getInstance();
            String str_name = entry.getName();
            String dir;
            String str_outdir = FileSystemStorage.getInstance().getAppHomePath();
            //FileOutputStream fileoutputstream;
            File newFile = new File(str_outdir, str_name);
            boolean overwrite = false;
            if (str_outdir.length() > 0) 
            {
                str_outdir = str_outdir + "/" + str_dirDest;
            }

            //extractFile(zin, outdir, name);
            String outpath = str_outdir + "/" + entry.getName();
            OutputStream output = null;

            try 
            {
                if (entry.isDirectory()) 
                {

                    fs.mkdir(str_outdir + "/" + str_name);
                    entry = zipStream.getNextEntry();
                    continue;
                } 
                else 
                {
                    File file = new File(str_outdir + File.separator + str_name);
                    File parent = file.getParentFile();
                    if (!parent.exists()) 
                    {
                        parent.mkdirs();
                    }

                }

                System.out.println("UNZIPPING:- " + str_name);
                output = FileSystemStorage.getInstance().openOutputStream(outpath);
                int len = 0;
                while ((len = zipStream.read(buffer)) > 0) 
                {
                    output.write(buffer, 0, len);
                }
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                //Dialog.show("Unzipping Error!", ""+e, "Ok", null);
            } 
            finally 
            {

                if (output != null) 
                {
                    output.close();
                }
            }
        }
    } catch (IOException ex) {
        Log.p(ex.getMessage(), 0);

    }
}

0 个答案:

没有答案