我有点问题! 我使用DotNetZip库在c#中使用Selfmade Minecraft启动器。 所以,这个Launcher有一个更新选项,它从服务器下载.zip,并应该从zip中提取所有文件到minecraft.jar!但是错误表明“文件已经存在”或者它创建了一个名为minecraft.jar的文件夹...... 有没有办法将zip存档中的文件直接提取到其他zip存档中? (因为.jar与.zip几乎相同) 这里是下载和extraxt代码(不要错过一些德语文本):
private void button3_Click(object sender, EventArgs e)
{
progressBar1.Visible = true; //Dient nur zur Deko
label1.Text = "Download......";
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("https://dl.dropbox.com/u/97421059/Test.zip"), @"Test.zip"); //Der Link sollte für die Zukünftigen Versionen immer gleich sein!
button3.Visible = false;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label1.Text = "Entpacken....."; //In der Box
progressBar1.Visible = false;
button3.Visible = true;
MessageBox.Show("Download abgeschlossen!!\n\rBitte warte bis der Launcher die Dateien entpackt hat."); // Erklärt sich von selbst
string ExistingZipFile = @"Test.zip";
string sourceDir = AppDomain.CurrentDomain.BaseDirectory;
string TargetDirectory = (sourceDir + "minecraft.jar");
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
// ab hier komm der restliche script
// bei dem man eig. nix einstellen soll
foreach (ZipEntry ze in zip)
{
ze.Extract(TargetDirectory, ExtractExistingFileAction.OverwriteSilently);
}
MessageBox.Show("Entpacken der Dateien abgeschlossen!");
label1.Text = "Entpacken abgeschlossen!";
}
}
在我粘贴GemHunter1代码之后(我希望我把名字填入正确的位置)我没有错误,但是在minecraft.jar中仍然没有来自下载拉链
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label1.Text = "Entpacken....."; //In der Box
progressBar1.Visible = false;
button3.Visible = true;
MessageBox.Show("Download abgeschlossen!!\n\rBitte warte bis der Launcher die Dateien entpackt hat."); // Erklärt sich von selbst
string ExistingZipFile = @"Test.zip";
string sourceDir = AppDomain.CurrentDomain.BaseDirectory;
string TargetDirectory = (sourceDir + "minecraft.jar");
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
// ab hier komm der restliche script
// bei dem man eig. nix einstellen soll
if (zip.ContainsEntry("Test.zip"))
{
zip.RemoveEntry("Test.zip");
}
MessageBox.Show("Entpacken der Dateien abgeschlossen!");
label1.Text = "Entpacken abgeschlossen!";
}
}
答案 0 :(得分:2)
好的,所以我为你制作了这段代码......
if (Directory.Exists(temp))
{
Directory.Delete(temp, true);
Directory.CreateDirectory(temp);
}
using (ZipFile jar = ZipFile.Read(appdata + "\\.minecraft\\bin\\minecraft.jar"))
{
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
zip.ExtractAll(temp, ExtractExistingFileAction.OverwriteSilently);
}
foreach (string file in Directory.GetFiles(temp))
{
if (jar.ContainsEntry(file))
{
jar.RemoveEntry(file);
}
jar.AddFile(file, "\\");
}
jar.Save();
MessageBox.Show("Entpacken der Dateien abgeschlossen!");
label1.Text = "Entpacken abgeschlossen!";Solved the problem with this code(thanks to GemHunter1 :D ):
答案 1 :(得分:1)
<强>编辑:强> 用这个:
//filename_you_are_going_to_copy is string with name of file with extension, not full path
if (zip.ContainsEntry(filename_you_are_going_to_copy))
{
zip.RemoveEntry(filename_you_are_going_to_copy);
}
编辑2: 在上面的代码之后写下这个:
mod.AddFile(filename_you_are_going_to_copy);
答案 2 :(得分:0)
解决了这段代码的问题(感谢GemHunter1:D):
if (Directory.Exists(temp))
{
Directory.Delete(temp, true);
Directory.CreateDirectory(temp);
}
using (ZipFile jar = ZipFile.Read(appdata + "\\.minecraft\\bin\\minecraft.jar"))
{
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
zip.ExtractAll(temp, ExtractExistingFileAction.OverwriteSilently);
}
foreach (string file in Directory.GetFiles(temp))
{
if (jar.ContainsEntry(file))
{
jar.RemoveEntry(file);
}
jar.AddFile(file, "\\");
}
jar.Save();
MessageBox.Show("Entpacken der Dateien abgeschlossen!");
label1.Text = "Entpacken abgeschlossen!";