1

Im try to make a program who take two folders and add to archive with ZipFile.

For now I taka one folder and make archive in the directory, and take second folder and make to archive in the directory.

But, I want to take this two folders and make it to archive ?

My code is:

            string Info = "";
            string startPath = @"D:\dosfiles\ValPoch\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD";
            string zipPath = @"D:\dosfiles\ValPoch\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip";

            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
                ZipFile.CreateFromDirectory(startPath, zipPath);
                //MessageBox.Show("Вашият архив е създаден в папка D:\\dosfiles\\ValPoch\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip");
                Info += "Вашият архив е създаден в папка D:\\dosfiles\\ValPoch\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip" + Environment.NewLine;
            }
            else
            {
                ZipFile.CreateFromDirectory(startPath, zipPath);
                //MessageBox.Show("Вашият архив е създаден в папка D:\\dosfiles\\ValPoch\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip");
                Info += "Вашият архив е създаден в папка D:\\dosfiles\\ValPoch\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip" + Environment.NewLine;
            }

            string startPath1 = @"D:\dosfiles\SVK\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD";
            string zipPath1 = @"D:\dosfiles\SVK\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip";

            if (File.Exists(zipPath1))
            {
                File.Delete(zipPath1);
                ZipFile.CreateFromDirectory(startPath1, zipPath1);
                //MessageBox.Show("Вашият архив е създаден в папка D:\\dosfiles\\SVK\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip");
                Info += "Вашият архив е създаден в папка D:\\dosfiles\\SVK\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip";
            }
            else
            {
                ZipFile.CreateFromDirectory(startPath1, zipPath1);
                //MessageBox.Show("Вашият архив е създаден в папка D:\\dosfiles\\SVK\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip");
                Info += "Вашият архив е създаден в папка D:\\dosfiles\\SVK\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + ".SVD.zip";
            }
            if (Info != "")
            {
                MessageBox.Show(Info);
            }

1 Answers1

0

First create the zip file

ZipFile.Open(@"Z:\test.zip", ZipArchiveMode.Create);

Add directories as required

    public void AddDirectory(string zipPath, string entryName)
    {
        entryName = entryName.Trim().Replace('\\', '/');

        if (entryName.Right(1) != "/")
            entryName += "/";

        using (ZipArchive zArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            zArchive.CreateEntry(entryName);
    }

Then add files

    public void AddFromFile(string zipPath, string entryName, string sourcePath)
    {
        using (ZipArchive zArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            zArchive.CreateEntryFromFile(sourcePath, entryName);
    }

Here is the definition for Right

    public static string Right(this string value, int length)
    {
        return value.Substring(length > value.Length ? 0 : value.Length - length);
    }
J45
  • 374
  • 3
  • 14