我有一个 URI 上的 zip 文件(例如http://www.abc.com/a.zip),我想打开它并将文件从它保存到磁盘。在 C# 中有没有办法打开它而不将其保存到磁盘然后将文件从它保存到磁盘?
谢谢,
萨钦
我有一个 URI 上的 zip 文件(例如http://www.abc.com/a.zip),我想打开它并将文件从它保存到磁盘。在 C# 中有没有办法打开它而不将其保存到磁盘然后将文件从它保存到磁盘?
谢谢,
萨钦
使用ZipFile .Net Framework 4.5类或DotNetZip API。
ZipFile.ExtractToDirectory(zipPath, extractPath);
编辑:您可以通过类的方法准备一个流或获取 URL 的字节数组WebClient
。
string urlStr = "https://xyz.com/sample.zip";
using (WebClient client = new WebClient())
{
byte []bytes=client.DownloadData(urlStr);
using (MemoryStream ms = new MemoryStream(bytes))
{
using (ZipFile zip = ZipFile.Read(ms))
{
zip.ExtractAll(@"C:\csnet");
}
}
}
使用DotNetZip提取 zip 文件而不将存档保存到文件系统的示例。
private static void ExtractFromUrl(Uri uri, string directoryPath)
{
using (var webClient = new WebClient())
{
var data = webClient.DownloadData(uri);
using (var memoryStream = new MemoryStream(data))
using (var zipFile = ZipFile.Read(memoryStream))
{
zipFile.ExtractAll(directoryPath);
}
}
}