【问题标题】:Unzip folder reccursively in Android Xamarin mono 3.2.6在 Android Xamarin mono 3.2.6 中递归解压缩文件夹
【发布时间】:2014-12-27 11:41:20
【问题描述】:

我遇到了一个需要递归解压缩文件夹的场景。 我已经浏览了几个链接,但不符合我的要求。我使用的是mono 3.2.6,需要在android xamarin中递归提取。

我使用了第三方工具 ICSharpCode.SharpZipLib.Zip。这在我的调试模式下的应用程序中运行良好。但在发布模式下会引发错误。在真机上安装APK后。

即使尝试过Dotnet.Zip,但我无法递归解压缩文件夹。 here 建议开源 dll 但它集中在文件而不是文件夹上

另一个link 显示了java android 中的解决方案,我正在c# xamarin android 中寻找相同的类型。 有什么办法可以做到这一点,即使是免费的第三方工具也可以。任何人都可以建议我解决方案或提示。

【问题讨论】:

    标签: c# .net xamarin xamarin.android xamarin-studio


    【解决方案1】:

    如果它在发布模式下抛出错误,那么可能是因为链接器。要压缩和解压缩文件,您还可以使用

    java.util.zip

    包。更多信息here

    编辑:示例代码

    包括命名空间using Java.Util.Zip;

    using ( ZipInputStream s = new ZipInputStream ( File.OpenRead ( strSourcePath ) ) )
    {  
        ZipEntry theEntry;
        while ( ( theEntry = s.NextEntry ) != null )
        {
            string directoryName = Path.GetDirectoryName ( theEntry.Name );
            string fileName = Path.GetFileName ( theEntry.Name );
            directoryName = Path.Combine ( strDestFolderPath , directoryName );
            if ( directoryName.Length > 0 )
            {
                Directory.CreateDirectory ( directoryName );
            }
            if ( fileName != String.Empty )
            {
                using ( FileStream streamWriter = File.Create ( Path.Combine ( strDestFolderPath , theEntry.Name ) ) )
                {
                    int size = 2048;
                    byte [] data = new byte[size];
                    while ( true )
                    {
                        size = s.Read ( data , 0 , data.Length );
                        if ( size > 0 )
                        {
                            streamWriter.Write ( data , 0 , size );
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        } 
    }
    

    其中 strSourcePath 是源 zip 文件路径,strDestFolderPath 是目标文件夹路径

    【讨论】:

    • 这对我有用,另一个解决方案是添加将链接器内部化设置更改为“west”选项。 Android->Build->Linker->Internalization-west
    【解决方案2】:

    您还可以使用 System.IO.Compression 命名空间。

     public static async Task ExtractToDirectory(string strSourcePath, string strDestFolderPath,
            IProgress<int> progessReporter)
        {
    
            await Task.Factory.StartNew(() =>
            {
    
                using (ZipArchive archive = new ZipArchive(File.Open(strSourcePath, FileMode.Open)))
                {
                    double zipEntriesExtracted = 0;
                    double zipEntries;
    
                    zipEntries = archive.Entries.Count;
    
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        try
                        {
                            string fullPath = Path.Combine(strDestFolderPath, entry.FullName);
                            if (String.IsNullOrEmpty(entry.Name))
                            {
                                Directory.CreateDirectory(fullPath);
                            }
                            else
                            {
                                var destFileName = Path.Combine(strDestFolderPath, entry.FullName);
    
                                using (var fileStream = File.Create(destFileName))
                                {
                                    using (var entryStream = entry.Open())
                                    {
                                        entryStream.CopyTo(fileStream);
                                    }
                                }
                            }
    
                            zipEntriesExtracted++;
                            progessReporter.Report((int)((zipEntriesExtracted / zipEntries) * 100));
                        }
                        catch (Exception ex)
                        {
    
                        }
                    }
                }
    
            }, TaskCreationOptions.LongRunning);
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2012-01-02
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      相关资源
      最近更新 更多