C# 正在将.zip文件解压缩到文件夹C中#
当我试图创建代码以将.zip文件解压缩到文件夹中时出现问题,在我向您显示代码之前,我想告诉您我需要做什么 它很简单,我想写代码,这样当用户点击一个按钮时,它会删除一个目录,然后下载一个新的.zip文件,并在删除的相同目录和名称下提取它。。。这类似于将目录还原为默认形式 我成功地编写了删除目录和下载.zip文件的代码,但我无法编写解压缩该.zip文件的代码 这是密码C# 正在将.zip文件解压缩到文件夹C中#,c#,visual-studio-2017,C#,Visual Studio 2017,当我试图创建代码以将.zip文件解压缩到文件夹中时出现问题,在我向您显示代码之前,我想告诉您我需要做什么 它很简单,我想写代码,这样当用户点击一个按钮时,它会删除一个目录,然后下载一个新的.zip文件,并在删除的相同目录和名称下提取它。。。这类似于将目录还原为默认形式 我成功地编写了删除目录和下载.zip文件的代码,但我无法编写解压缩该.zip文件的代码 这是密码 private void button2_Click(object sender, EventArgs e) { // Is
private void button2_Click(object sender, EventArgs e)
{
// Is file downloading yet?
if (webClient != null)
return;
var sprdir = new DirectoryInfo(@"cstrike/sprites");
string sprzippath = @"cstrike/sprites.zip";
string extzippath = @"cstrike";
if (!sprdir.Exists)
{
webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"), @"cstrike/sprites.zip");
}
else
{
sprdir.Attributes = sprdir.Attributes & ~FileAttributes.ReadOnly;
sprdir.Delete(true);
webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"), @"cstrike/sprites.zip");
}
}
是的,我试过使用
System.IO
和System.IO.Compress
和ZipFile.ExtractToDirectory
和ExtractToDirectory
,没有人在工作。。。只需在文本下方画红线即可。因此,首先需要将assembly System.IO.Compression.FileSystem添加到项目中
第二件事是,您正在使用DownloadFileAsync,它可能尚未完成,因此提取失败(因为还不存在任何文件)
第三个问题是,如果该文件夹不存在,则不会创建该文件夹,这会导致WebClient.DownloadFileAsync失败
您需要注册到DownlodFileCompleted所在的事件和提取
以下是一个例子:
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.IO.Compression;
namespace Stack
{
public partial class Form1 : Form
{
WebClient webClient;// = new WebClient();
const string basPath = @"D:\test";
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
// Is file downloading yet?
if (webClient != null)
return;
//var sprdir = new DirectoryInfo(@"cstrike/sprites");
var sprdir = new DirectoryInfo(basPath);
string sprzippath = $"{basPath}/sprites.zip";
string extzippath = @"cstrike";
if (!sprdir.Exists)
{
Directory.CreateDirectory(basPath);
webClient = new WebClient();
webClient.DownloadFileCompleted += ExtratcZip;
webClient.DownloadFileAsync(
new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"),
$"{basPath}/sprites.zip");
}
else
{
sprdir.Attributes = sprdir.Attributes & ~FileAttributes.ReadOnly;
sprdir.Delete(true);
Directory.CreateDirectory(basPath);
webClient = new WebClient();
webClient.DownloadFileCompleted += ExtratcZip;
webClient.DownloadFileAsync(
new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"),
$"{basPath}/sprites.zip");
}
}
private void ExtratcZip(object sender, AsyncCompletedEventArgs e)
{
ZipFile.ExtractToDirectory($"{basPath}/sprites.zip", $"{basPath}");
}
}
}
希望有帮助。这能回答您的问题吗?我尝试使用System.IO和System.IO.Compress和ZipFile.ExtractToDirectory,这在代码中的位置?我尝试了您发送的内容,并删除了代码bcs不起作用。我会尝试,我会给您发送消息,谢谢:)再次出现相同的错误,你需要将程序集添加到你的项目中。啊,我忘了,谢谢,现在没有红线,我现在就去测试:)我想问题是你没有创建sprites目录,DownloadFileAsync也没有自动为你创建它。下载前请确保目标文件夹存在。