C# 在Android Xamarin mono 3.2.6中重新解压缩文件夹
我得到了一个需要递归解压文件夹的场景。 我已经通过了几个环节,但不符合我的要求。我正在使用Mono3.2.6,需要在android xamarin中递归提取 我使用了第三方工具ICSharpCode.SharpZipLib.Zip。在调试模式下,这在我的应用程序中运行良好。但是在发布模式中抛出一个错误。在实际设备中安装APK后 甚至尝试过,但我无法递归解压缩文件夹。 建议使用开源dll,但它主要集中在文件而不是文件夹上 另一个是java android中的解决方案,我正在寻找c#xamarin android中的相同类型。C# 在Android Xamarin mono 3.2.6中重新解压缩文件夹,c#,.net,xamarin,xamarin.android,xamarin-studio,C#,.net,Xamarin,Xamarin.android,Xamarin Studio,我得到了一个需要递归解压文件夹的场景。 我已经通过了几个环节,但不符合我的要求。我正在使用Mono3.2.6,需要在android xamarin中递归提取 我使用了第三方工具ICSharpCode.SharpZipLib.Zip。在调试模式下,这在我的应用程序中运行良好。但是在发布模式中抛出一个错误。在实际设备中安装APK后 甚至尝试过,但我无法递归解压缩文件夹。 建议使用开源dll,但它主要集中在文件而不是文件夹上 另一个是java android中的解决方案,我正在寻找c#xamarin
有没有办法做到这一点,即使是免费的第三方工具也可以。任何人都可以向我建议解决方案或提示,我非常感谢。如果它在发布模式中抛出错误,那么可能是因为链接器。要压缩和解压文件,还可以使用 java.util.zip 包裹。更多信息 编辑:示例代码 使用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是目标文件夹路径,也可以使用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);
}
公共静态异步任务extractodirectory(字符串strSourcePath、字符串strDestFolderPath、,
IProgress progessReporter)
{
等待任务。工厂。开始新建(()=>
{
使用(ZipArchive archive=new ZipArchive(File.Open(strSourcePath,FileMode.Open)))
{
双zipentriestracted=0;
双拉链;
zipEntries=archive.Entries.Count;
foreach(存档中的ZipArchiveEntry条目。条目)
{
尝试
{
字符串fullPath=Path.Combine(strDestFolderPath,entry.FullName);
if(String.IsNullOrEmpty(entry.Name))
{
CreateDirectory(完整路径);
}
其他的
{
var destFileName=Path.Combine(strDestFolderPath,entry.FullName);
使用(var fileStream=File.Create(destFileName))
{
使用(var entryStream=entry.Open())
{
CopyTo(fileStream);
}
}
}
zipentriestracted++;
ProgesReporter.报告((内部)((zipentriestracted/zipEntries)*100);
}
捕获(例外情况除外)
{
}
}
}
},TaskCreationOptions.LongRunning);
}
这对我来说很有效,另一个解决方案是添加将链接器内部化设置更改为“west”选项。安卓->构建->链接器->内部化