c# - 是否可以并行下载和解压?

标签 c# parallel-processing zip dotnetzip

我正在下载一些大的 zip 文件,然后在我的程序中解压缩。性能很重要,我开始考虑的一个方向是是否可以开始下载,然后在数据到达时开始解压缩,而不是等待下载完成然后开始解压缩。这可能吗?根据我对DEFLATE的理解,理论上应该是可能的吧?

我目前正在使用 DotNetZip 作为我的 zip 库,但它拒绝对不可查找的流进行操作。

代码如下:

// HTTP Get the application from the server
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "GET";

Directory.CreateDirectory(localPath);
using (var response = (HttpWebResponse)request.GetResponse())
using (Stream input = response.GetResponseStream())
{
    // Unzip being some function which will start unzipping and
    // return when unzipping is done
    return Unzip(input, localPath);
}

最佳答案

I started thinking about was whether it was possible to start the download and then begin unzipping the data as it arrives, instead of waiting for the download to complete and then start unzipping. Is this possible?

如果您想在响应正文仍在下载时开始解压缩,您实际上无法执行此操作。

在 ZIP 文件中,中央目录记录位于 ZIP 文件的最末尾,其中包含 ZIP 文件中的文件列表。这将是您下载的最后一个内容。如果没有它,您将无法可靠地确定各个文件记录在 ZIP 文件中的位置。

这也可以解释为什么 DotNetZip 需要可查找流。它需要能够首先读取文件末尾的中央目录记录,然后跳回到前面的部分以读取有关各个 ZIP 条目的信息以提取它们。

如果您有非常具体的 ZIP 文件,您可以对这些单独文件记录的布局做出某些假设并手动提取它们,而无需向后查找,但它通常不会与 ZIP 文件广泛兼容。

关于c# - 是否可以并行下载和解压?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56829443/

相关文章:

c# - 可以在 .net 中扩展 String 类

c# - 无效操作异常 : An exception was thrown while attempting to evaluate a LINQ query parameter expression

c - 如何在没有主线程的情况下运行静态并行for循环

linux - 如何创建没有完整目录结构的 zip 文件

用于 Zipfile 的 Java ByteBuffer

c# - 如何使用 MSBuild 在输出文件夹中自动创建文件夹?

c# - 在 LINQ lambda 表达式中使用字符串变量

javascript - 循环遍历并行层

python - 使用Python在云端进行并行处理

linux - 从 zip 文件中删除 .svn 文件