将文件从 zip 提取到 jar 存档中

本文关键字:jar 提取 文件 zip | 更新日期: 2023-09-27 17:55:19

我有一点问题!我使用DotNetZip库在c#中开发自制的Minecraft启动器。因此,此启动器具有更新选项,它从服务器下载.zip,并应将所有文件从zip提取到我的世界中.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下载 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!";
    }

}

将文件从 zip 提取到 jar 存档中

好的

,所以我为你制作了这段代码...

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 ):

已编辑:使用这个:

//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);

解决了这段代码的问题(感谢 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!";