Saving the selected folder to extension zip c#

Hemanth B 886 Reputation points
2021-06-10T12:55:59.863+00:00

Hi, I created a zip folder application in c# where users can select their folder and zip it in their selected desired location. This is the code: but when I test it and run it and zip a folder it is not zipping into type Compressed(zipped) folder in file explorer. Example(if I zip folder "TestZip" to my desired location, it just saves the "TestZip" file to type "File" in file explorer instead of type "Compressed(zipped) folder". Please help me with this issue. And even on the Save File Dialog there are no options in the file filter.

 DialogResult result = saveFileDialog1.ShowDialog();


            if (result == DialogResult.OK)
            {
                if (comboBox1.SelectedItem == "Zip a Folder")
                {
                    ZipFile.CreateFromDirectory(label2.Text, saveFileDialog1.FileName);

                }
                else if (comboBox1.SelectedItem == "Zip a File")    
                {
                    string[] files = label2.Text.Split(',');
                    ZipArchive zip = ZipFile.Open(saveFileDialog1.FileName, ZipArchiveMode.Create);
                    foreach (string file in files)
                    {
                        zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
                    }
                    zip.Dispose();
                }
                MessageBox.Show("ZIP file created successfully!");
            }
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-06-11T08:46:03.893+00:00

    The generated file lacks a suffix. You can manually rename this file and add .zip at the end.

    But a better way is to add this sentence in the code:

                saveFileDialog1.Filter = "ZIP Folders (.ZIP)|*.zip";  
    

    Note Please use it before calling saveFileDialog, such as in the constructor or in the Form_load event.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.