tags:

views:

351

answers:

2

Hello,

I want to create a zip extractor for plugins that I create for asp.net sites. I want the client to be able to run the extractor and the files are placed into the correct folders that I specify. Are there any tools for this? I'm not sure how to do this, thanks

+2  A: 

See this: .NET Zip Library #ziplib (SharpZipLib)

Rubens Farias
+3  A: 

DotNetZip is a library that allows managed code apps to read or write zip files. One thing it allows you to do is produce a self-extracting archive (SFX).

In C#, the code to produce an SFX archive with DotNetZip looks like this:

using (ZipFile zip1 = new ZipFile())
{
    // zip up a directory
    zip1.AddDirectory("C:\\project1\\datafiles", "data");
    zip1.Comment = "This will be embedded into a self-extracting exe";
    zip1.AddEntry("Readme.txt", "This is content for a 'Readme' file that will appear in the zip.");
    zip1.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.WinFormsApplication);
}

You choose how to shape the folder hierarchy in the ZIP file. The only rule is, upon extracting, the extraction happens to a particular root or parent folder. (You cannot extract to 7 different directories scattered about the filesystem)

The resulting EXE requires the .NET Framework 2.0 or later in order to run, but nothing else.

The SFX/EXE is also a regular Zip file that you can read and extract with WinZip, or other zip tools, including Windows Explorer "Compressed Folders".

The SFX composes with the other zip features, including

  • WinZip AES encryption - so you can encrypt the thing and only allow unpacking for someone who knows the password.
  • ZIP64 for very large archives.
  • Unicode, for filenames outside of the normal ~ASCII range.
  • restoration of File attributes, timestamps, etc.

In DotNetZip, there are two possible flavors of SFX that you can generate: a console app or a WinForms app. The WinForms UI is generated for you - nothing to write or design. It's simple and utilitarian, and looks like this:

alt text

The console flavor is more suited to being used in a script, or in a headless (no UI) scenario.


There are a bunch of options when producing the SFX, like:

  • whether to open Explorer after unpacking
  • a command to execute after unpack (the command can be part of what was unpacked)
  • optionally remove all files after the 'execute on unpack' command completes successfully. Nice for a patch utility.
  • whether to overwrite existing files
  • the Win32 icon to use for the SFX EXE, or just accept the default
  • a default extract directory, which can be based on the user's environment, eg %USERPROFILE%.
  • "quiet" mode, which means when it runs, it offers either no UI at all if you are using a console app, or only a simple progress bar if using the WinForms flavor. There are no buttons to click - this SFX just unpacks automatically.

All these options for how the SFX will behave is accessible via the DotNetZip class library interface.


If you don't like the UI or the working model for the built-in SFX capability, there's an easy way to provide your own self-extractor UI + logic. For example you could build an app in WPF and make it all snazzy and visually dyanmic. The way it works is pretty simple: in the code that produces the SFX, copy your unzip stub EXE (WinForms, WPF, or whatever) to an output stream, and then, without closing the stream, save the Zip file to the same stream. It looks like this in VB:

Public Shared Function Main(ByVal args As String()) As Integer
    Dim sfxStub As String = "my-sfx-stub.exe"
    Dim outputFile As String = "my-sfx-archive.exe"
    Dim directoryToZip As String = "c:\directory\to\ZIP"
    Dim buffer As Byte() = New Byte(4000){}
    Dim n As Integer = 1
    Using output As System.IO.Stream = File.Open(outputFile, FileMode.Create)
        '' copy the contents of the sfx stub to the output stream
        Using input As System.IO.Stream = File.Open(sfxStub, FileMode.Open)
            While n <> 0
                n = input.Read(buffer, 0, buffer.Length)
                If n <> 0 Then
                    output.Write(buffer, 0, n)
                End If
            End While
        End Using
        '' now save the zip file to the same stream
        Using zp As New ZipFile
            zp.AddFiles(Directory.GetFiles(directoryToZip), False, "")
            zp.Save(output)
        End Using
    End Using
End Function

The resulting file created by this code is both an EXE and a ZIP file. The stub EXE must read from itself in order to extract. In the example from above, the code for the my-sfx-stub.exe program could be something as simple as this:

Dim a As Assembly = Assembly.GetExecutingAssembly
Try
    '' read myself as a zip file
    Using zip As ZipFile = ZipFile.Read(a.Location)
        Dim entry As ZipEntry
        For Each entry in zip
            '' extract here, or add to a listBox, etc. 
            '' listBox1.Items.Add(entry.FileName)
        Next
    End Using
Catch
    MessageBox.Show("-No embedded zip file.-")
End Try

A custom SFX constructed in this simple way will have a dependency on the DotNetZip DLL. To avoid this, and to produce a single self-contained EXE that can extract with no additional DLL dependencies, you can use ILMerge, or, embed the DotNetZip DLL as a resource and use an AssemblyResolver event to load it from the embedded resources.

If you have questions, you can ask on the DotNetZip forums.

Cheeso