This project is read-only.

How do I...?

Nov 22, 2010 at 8:27 PM
Edited Nov 22, 2010 at 8:29 PM

Hello. Thank you for a very well maintained and documented project.

I am just beginning trying to write a basic zipper unzipper in VB 2008 EXPRESS, and have come across a situation that I can't seem to find an answer for even after looking through the examples and documentation.

Basically I want to offer the ability to choose between zipping an entire directory (which I can do) or just a file or selection of files using two radio buttons on my form.

Dim FolderSource As New FolderBrowserDialog
IF radiobuttonFOLDER.checked = True then
 Dim FolderName As String = Me.TextBoxSourcePath.Text
        FolderSource.SelectedPath = IIf(Directory.Exists(FolderName), FolderName, "c:\")
        FolderSource.ShowNewFolderButton = False
        If (FolderSource.ShowDialog = DialogResult.OK) Then
            FolderName = FolderSource.SelectedPath
            Me.TextBoxSourcePath.Text = FolderName
            TextBoxTargetPath.Text = (FolderName & ".zip")
        End If
'Create zip file code in here ElseIF IF radiobuttonFILE.checked = True then 'Code for selecting and zipping the selected FILES. <-- This is what I need help with End if


I can see in the examples that adding multiple files is easy, IF you use strongly typed file paths and file names.

How do I add multiple files that the user has selected at runtime?

If it possible you could write an example of this in the Documentation/examples please?

Many thanks


Nov 23, 2010 at 1:45 AM
Robbie73 wrote:

I can see in the examples that adding multiple files is easy, IF you use strongly typed file paths and file names.

How do I add multiple files that the user has selected at runtime?

I think you mean "fully qualified file paths" , not "strongly typed file paths".  Strong typing is a characteristic of the type system in .NET.  "Strongly typed" is not a descriptor you would apply to a file path itself, or a single variable in a program.

Now, assuming you mean "fully qualified" -- I don't understand what that means.  In a program when you add a file to the ZipFile, you can specify the filename in a fully-qualfied fashion, starting with the drive letter, colon, slash, etc etc...., or, you can specify the filename without full qualification, in which case it is resolved relative to the current working directory.  For example, if your program is running in c:\users\Rob , and you specify a filename as path1\Filename.txt , then the actual file that the program will try to open is c:\users\Rob\path1\Filename.txt .

So I don't understand what you mean by "it's easy IF you use fully qualified file paths."  I don't understand what's hard about using relative paths.  If you have a specific question on that I can try to answer it, if you clarify.

You did pose a direct question - how to add multiple files that the user has selected at runtime?  That seems like a pretty straightforward question.  With respect to the DotNetZip library, doing this is no different than building a zip without direct user interaction.  The filename must be retained in a string variable in your program.  Then you need to call ZipFile.AddFile() , passing that string variable.

There are some tricky parts.  If you pass a fully-qualified path, then you will get the entier path in the ZipFile.  There's an overload to ZipFile.AddFile() that accepts 2-arguments, which allows you to explicitly specify the directory to use for the file within the zipfile.  In other words, you can pass c:\users\Rob\path1\Filename.txt for the filename, and "Foo\bar" for the directory path, and the zip entry will be called Foo\bar\Filename1.txt  .  This is not different from the case that does not involve user interaction .  Maybe you know all this.

The tricky part for you, then, is the user interaction.  How do you allow the user to select and specify which files to include in the zip file?  Some applications use drag-and-drop, to allow, for example, a user to drag filenames from Windows Explorer into the .NET application.  That kind of computer/user interaction is familiar, and so may be something you want to pursue.  For an example of how to do this, see the Windows Zip tool that is included in the DotNetZip source code.  (It's in the Tools directory, not the Examples directory).  On the other hand you may want to allow the user to select files without leaving your own application, in which case drag-and-drop maybe isn't the right thing.  

TThere are lots of options for the user interaction model.  I can't really advise or recommend which one would work best for you.  Whichever you choose, you can add the selected files into the ZipFile via AddFile method.