下载一个zip文件并解压到内部存储Android Xamarin跨平台应用程序。

4
有没有办法使用Xamarin跨平台应用程序从远程服务器下载和解压文件呢? 我使用PC:Storage库与文件和文件夹进行交互。
IFolder rootFolder = FileSystem.Current.LocalStorage;
        IFolder folder = await rootFolder.CreateFolderAsync("MyAppRoot\\F1",CreationCollisionOption.OpenIfExists);
        IFile file = await folder.CreateFileAsync("firstfile.txt",
            CreationCollisionOption.ReplaceExisting);
        await file.WriteAllTextAsync("my content");

我创建了一个名为F1的文件夹并在应用根目录下创建了一个名为firstfile.txt的文件,并向其中写入了一些内容。但是如何使用zip文件做同样的事情呢?

下载zip文件,将其解压到文件夹中。

另外,我如何查看运行应用程序时创建的文件夹/文件?是否有类似于Xamarin Android模拟器的IsoStoreSpy工具可用?


你解决了吗? - Azure Terraformer
1
我有你问题的一个答案。如果你想查看模拟器的数据,你可以使用Android设备监视器(DDMS)来查看。在Visual Studio 2015中,你会在Android设备日志图标的极左边找到它。请记住,这只有在你的应用程序运行时才能工作。 - Ashish-BeJovial
1个回答

0

第一步,获取文件:

Xamarin 帮助 下载该文件

以下是他们的下载任务,以防链接失效:

public static async Task<int> CreateDownloadTask(string urlToDownload, IProgress<DownloadBytesProgress> progessReporter)
{
int receivedBytes = 0;
int totalBytes = 0;
WebClient client = new WebClient();

using (var stream = await client.OpenReadTaskAsync(urlToDownload))
{
    byte[] buffer = new byte[4096];
    totalBytes = Int32.Parse(client.ResponseHeaders[HttpResponseHeader.ContentLength]);

    for (;;)
    {
        int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
        if (bytesRead == 0)
        {
            await Task.Yield();
            break;
        }

        receivedBytes += bytesRead;
        if (progessReporter != null)
        {
            DownloadBytesProgress args = new DownloadBytesProgress(urlToDownload, receivedBytes, totalBytes);
            progessReporter.Report(args);
        }
    }
}
return receivedBytes;
}

你需要将正在下载的数据写入文件,因此在他们的方法中添加一个文件处理程序,然后执行类似以下操作:

await zipFile.WriteAsync(buffer, 0, bytesRead);

然后是解压缩,我知道以前已经下载过Microsofts Compression NuGet...浏览了一些代码,我使用了System.IO.Compression命名空间中的DeflateStream来解压缩东西。

既然你已经有处理本地文件的东西,我就不再提及了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接