How to do it...

  1. Open Visual Studio 2017.
  2. Now open the solution that we built from the previous recipe.
  1. The Solution Explorer should look like this:
  1. Now, let's select the solution name and right-click.
  2. From the menu, select Add | New Project.
  3. In the New Project dialog box, expand the Visual C# node and select Windows Classic Desktop in the left-hand pane.
  4. In the right-hand pane, select Windows Forms App (.NET Framework):
  1. Now, in the Name: textbox, type Chapter3.Compress.ZipperWinApp, leave the Location: textbox as it is, and click OK.
  1. Now, the Solution Explorer should look like this:
  1. Let's rename the Form1.cs as MainForm.cs by selecting it and pressing F2.
  2. Answer Yes in the confirmation box to confirm the changing of the main class name as well.
  3. Now, select the Windows form from the designer window.
  4. Drag drop a TextBox, a Button, and a ListBox in the form.
  5. Also drag drop a FolderDialogBox control in the Windows form.
  1. Change the properties of the previous controls as per this table:
    Control Property Value
    TextBox Name FolderTextBox
    Button Name BrowseButton
    Button Text Browse...
    ListBox Name FileListBox
    Form Text Zipper WinApp
    FolderDialogBox Name ZipFolder
  2. Now, your designer area should look like this:
  1. Now, select the Chapter3.Compress.ZipperWinApp label in the Solution Explorer and expand it.
  2. Right-click on the References label and select Add Reference.
  3. Click on the Projects node in the Reference Manager dialog box.
  1. Check the checkbox in front of Chapter3.Compress.CompressLib from the project list in the right-hand pane.
  1.  Click OK.
  2. Now double-click on the Browse... button to open the code window.
  3. Scroll up in the code window and add the following using directive to the last line of all the using directives, at the top:
      using Chapter3.Compress.CompressLib;
  1. Now scroll down till you reach the button click event of the Browse... button and add the following code in between the curly brackets:
      if (ZipFolder.ShowDialog() == DialogResult.OK)
{
FolderTextBox.Text = ZipFolder.SelectedPath;

string zipFileName =
@"C:ProjectsChapter3TestFolder esult.zip";

var zipFile = new Zipper(zipFileName);
zipFile.CompressFile(FolderTextBox.Text);

MessageBox.Show("You folder has been zipped.",
"Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);

var fileList = zipFile.ListArchive(zipFileName);

FileListBox.Items.AddRange(fileList.ToArray());
}
  1. Let's press F5 and test our code. Your output should look like this:
  1. Let's click the Browse... button and navigate to a folder:
  1. Click OK. Now, the folder will be compressed and the output file will be result.zip.
  1. Click OK.
  2. Now you will see the list of files in the compressed ZIP file:
  1. Let's browse to the folder selected using Windows Explorer and have a look (you can copy and paste the path from the textbox in the application itself):
  1. Double-click on the file and you will see that the content of the ZIP file matches the list we had in step 29:
  1. Now close the app.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.130.24