【问题标题】:Download a zip file and unzip to internal storage Android Xamarin cross platform app下载 zip 文件并解压缩到内部存储 Android Xamarin 跨平台应用程序
【发布时间】:2014-11-27 09:39:26
【问题描述】:

有没有办法使用 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 文件,将内容解压缩到文件夹。

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

【问题讨论】:

  • 你弄明白了吗?
  • 我对你的问题有一个答案,如果你想查看模拟器的数据,你可以使用 Android Device Monitor (DDMS) 来查看,你会在 Android Device Logging 图标的最左侧找到它visual studio 2015,请记住,它仅在您的应用程序运行时才有效。

标签: xamarin xamarin.forms


【解决方案1】:

第一步,获取文件:

Xamarin 帮助 Downloading the file

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

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 来解压缩东西。

因为你已经有了处理本地文件的东西,所以我不会提及任何东西。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多