2

我有个小问题!我使用 DotNetZip 库在 c# 中开发一个自制的 Minecraft 启动器。所以,这个启动器有一个更新选项,它从服务器下载一个 .zip 文件,并将所有文件从 zip 文件中提取到 minecraft.jar 中!但是一个错误指出“文件已经存在”或者它创建了一个名为 minecraft.jar 的文件夹......有没有可能的方法将文件从 zip 存档中直接提取到其他 zip 存档中?(因为 .jar 与 .zip 几乎相同)这里是下载和提取代码(不要想知道有些文本是德语的):

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 中仍然没有下载 zip 中的任何内容

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!";
    }


}
4

3 回答 3

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 ):
于 2013-01-23T12:41:47.343 回答
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);
于 2013-01-22T18:37:57.217 回答
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!";
于 2013-01-23T14:53:03.463 回答