xamarin离子ZIP解压缩导致错误 [英] xamarin ionic zip unzip causes error

查看:377
本文介绍了xamarin离子ZIP解压缩导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法下载后解压缩的文件。但是,经济需求测试与误差

System.ArgumentException路径文件未知是System.IO.Directory.CreateDirectory System.String路径0x00000空在FILNAME ionic.zip.zipentry.internalExtract system.string BASEDIR的System.IO.Stream outstream system.string密码0x00000不得而知。

ZIP文件没有密码,可与Android的拉链被运行结束例如

 公共无效ExtractBilderZip()
        {
            字符串documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            串unzipziel = Path.Combine(documentsPathBILDER);            //如果BILDER不存在创建子目录
            如果(Directory.Exists(unzipziel)== FALSE)
            {
                变种目录名= Path.Combine(documentsPathBILDER);
                Directory.CreateDirectory(目录名);            }            ////////////////////////////////////////////////// ////
            字符串zipToUnpack = localPathB; // localPathB IST的压缩文件名,包括路径            //我试着用和wihout以下2线,不作任何diffence
            zipToUnpack = zipToUnpack.Replace('/',Path.DirectorySeparatorChar);
            unzipziel = unzipziel.Replace('/',Path.DirectorySeparatorChar);
            尝试
            {
                ZipFile中ZIP1 = ZipFile.Read(zipToUnpack);                zip1.ExtractAll(unzipziel,ExtractEx​​istingFileAction.OverwriteSilently);
            }
            赶上(System.Exception的EX1)
            {
                //System.Console.Error.WriteLine(\"exception:+ EX1);
                的for(int i = 0;我小于10;我++)
                {
                    Toast.MakeText(这一点,异常:+ EX1,ToastLength.Long).Show();
                }
            }        }


解决方案

我利用一个叫ZipStorer类,我发现通过googleing的解决我的问题。我发现在这里:zipstorer codeplex.com这是微小的,非常好用!

FileInfo的网络连接=新的FileInfo(zipToUnpack);

 的FileStream INFILE = fi.OpenRead();                //获取原始文件扩展名,
                //从report.doc.cmp例如文档。
            字符串curFile = fi.FullName;
            字符串origName = unzipziel;
            尝试
            {
                //打开现有的zip文件
                ZipStorer邮编= ZipStorer.Open(localPathB,FileAccess.Read);                //读取所有的目录内容
                清单< ZipStorer.ZipFileEntry> DIR = zip.ReadCentralDir();
                //提取在目标目录中的所有文件
                路径字符串;
                布尔结果;
                的foreach(在DIR ZipStorer.ZipFileEntry进入)
                {
                    PATH = Path.Combine(unzipziel,Path.GetFileName(entry.FilenameInZip));
                    结果= zip.ExtractFile(入门,路径);                    RunOnUiThread(()=>
                    {
                        按钮buttonUZ = FindViewById<按钮和GT;(Resource.Id.btnUnzip);
                        buttonUZ.Text =Entpacke:+ entry.FilenameInZip;                    });                }
                zip.Close();                File.Delete(@localPathB);            }
            赶上(例外EX1)
            {
                VAR errorActivity =新意图(此的typeof(ErrorActivity));
                errorActivity.PutExtra(ERRORex1.ToString());                StartActivity(errorActivity);            }

I have the following method to unzip a file after download. But it ents with an error

System.ArgumentException Path is empty at System.IO.Directory.CreateDirectory System.String path 0x00000 in file unknown at ionic.zip.zipentry.internalExtract system.string basedir system.io.stream outstream system.string password 0x00000 in filname unknown.

The zip File does not have a password and can be opend e.G with android zip

public void ExtractBilderZip()
        {
            string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string unzipziel = Path.Combine(documentsPath, "Bilder");

            // if Bilder does not exist create subdirectory
            if (Directory.Exists(unzipziel)==false)
            {
                var directoryname = Path.Combine(documentsPath, "Bilder");
                Directory.CreateDirectory(directoryname);

            }

            //////////////////////////////////////////////////////
            string zipToUnpack = localPathB;   //localPathB ist the zipFile name including path

            // I tried with and wihout the following 2 lines which makes no diffence
            zipToUnpack = zipToUnpack.Replace('/', Path.DirectorySeparatorChar);
            unzipziel = unzipziel.Replace('/', Path.DirectorySeparatorChar);


            try
            {


                ZipFile zip1 = ZipFile.Read(zipToUnpack);

                zip1.ExtractAll(unzipziel,ExtractExistingFileAction.OverwriteSilently);


            }
            catch (System.Exception ex1)
            {
                //System.Console.Error.WriteLine("exception: " + ex1);
                for (int i = 0; i < 10; i++)
                {
                    Toast.MakeText(this, "exception: " + ex1, ToastLength.Long).Show();
                }
            }





        }

解决方案

I solved my problem by use of a Class called ZipStorer which I found by googleing. I found it here: zipstorer.codeplex.com It is tiny and very easy to use!!

FileInfo fi = new FileInfo(zipToUnpack);

            FileStream inFile = fi.OpenRead();

                // Get original file extension, 
                // for example "doc" from report.doc.cmp.
            string curFile = fi.FullName;
            string origName = unzipziel;


            try
            {
                // Opens existing zip file
                ZipStorer zip = ZipStorer.Open(localPathB, FileAccess.Read);

                // Read all directory contents
                List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();


                // Extract all files in target directory
                string path;
                bool result;
                foreach (ZipStorer.ZipFileEntry entry in dir)
                {
                    path = Path.Combine(unzipziel, Path.GetFileName(entry.FilenameInZip));
                    result = zip.ExtractFile(entry, path);

                    RunOnUiThread(() =>
                    {
                        Button buttonUZ = FindViewById<Button>(Resource.Id.btnUnzip);
                        buttonUZ.Text = "Entpacke: " + entry.FilenameInZip;

                    });

                }
                zip.Close();

                File.Delete(@localPathB);

            }
            catch (Exception ex1)
            {
                var errorActivity = new Intent(this, typeof(ErrorActivity));
                errorActivity.PutExtra("ERROR", ex1.ToString());

                StartActivity(errorActivity);

            }

这篇关于xamarin离子ZIP解压缩导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆