DotNetZip - ASP.NET VB Example
ASP.NET Example in VB
Here's an Example ASP.NET page, using VB, that dynamically creates a zip file and saves it to Response.OutStream. From the browser, the user will be prompted with the familiar download dialog box, allowing Open, Save, or Cancel of the generated zip file.
CopyASP.NET example in VB
1<%@ Page 2 Language="VB" 3 Debug="true" 4%> 5 6<%@ Import Namespace="System.Text" %> 7<%@ Import Namespace="System.IO" %> 8<%@ Import Namespace="Ionic.Zip" %> 9<%@ Import Namespace="System.Collections.Generic" %> 10 11<script language="VB" runat="server"> 12 13' ZipExample.aspx 14' 15' This .aspx page demonstrates how to use the DotNetZip library from within ASP.NET. 16' 17' To run it, 18' 1. drop the Ionic.Zip.dll into the \bin directory of your asp.net app 19' 2. create a subdirectory called "fodder" in your web app directory. 20' 3. copy into that directory a variety of random files. 21' 4. insure your web.config is properly set up (See below) 22' 23' 24' notes: 25' This requies the .NET Framework 3.5 - because it uses the ListView control that is 26' new for ASP.NET in the .NET Framework v3.5. 27' 28' To use this control, you must add the new web controls. Also, you must use the v3.5 compiler. 29' Here's an example web.config that works with this aspx file: 30' 31' <configuration> 32' <system.web> 33' <trust level="Medium" /> 34' <compilation defaultLanguage="c#" /> 35' <pages> 36' <controls> 37' <add tagPrefix="asp" namespace="System.Web.UI.WebControls" 38' assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 39' </controls> 40' </pages> 41' </system.web> 42' <system.codedom> 43' <compilers> 44' <compiler language="c#;cs;csharp" 45' extension=".cs" 46' warningLevel="4" 47' type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 48' <providerOption name="CompilerVersion" value="v3.5" /> 49' <providerOption name="WarnAsError" value="false" /> 50' </compiler> 51' 52' <compiler language="vb;vbs;visualbasic;vbscript" 53' extension=".vb" 54' warningLevel="4" 55' type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 56' <providerOption name="CompilerVersion" value="v3.5" /> 57' <providerOption name="OptionInfer" value="false" /> 58' <providerOption name="WarnAsError" value="false" /> 59' </compiler> 60' 61' </system.codedom> 62' </configuration> 63' 64' 65 66 67 68Dim width as String = "100%" 69 70Public Sub Page_Load (ByVal sender As Object, ByVal e As System.EventArgs) 71 Try 72 If Not ( Page.IsPostBack ) Then 73 ' populate the dropdownlist 74 ' must have a directory called "fodder" in the web app 75 Dim homeDir as String = Server.MapPath(".") 76 Dim sMappedPath as String= Server.MapPath("fodder") 77 78 Dim fqFilenames As New List(Of String)(System.IO.Directory.GetFiles(sMappedPath)) 79 80 Dim filenames as List(Of String) = _ 81 fqFilenames.ConvertAll (Function(s) s.Replace(sMappedPath & "\", "")) 82 83 ErrorMessage.InnerHtml = "" 84 85 FileListView.DataSource = filenames 86 FileListView.DataBind() 87 End If 88 89 Catch 90 ' Ignored 91 End Try 92 93End Sub 94 95 96 97Public Sub btnGo_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) 98 99 ErrorMessage.InnerHtml ="" ' debugging only 100 Dim filesToInclude as New System.Collections.Generic.List(Of String)() 101 Dim sMappedPath as String= Server.MapPath("fodder") 102 Dim source As DataKeyArray= FileListView.DataKeys 103 104 For Each item As ListViewDataItem in FileListView.Items 105 106 Dim chkbox As CheckBox= CType(item.FindControl("include"), CheckBox) 107 Dim lbl As Label = CType(item.FindControl("label"), Label) 108 109 If Not (chkbox Is Nothing OR lbl Is Nothing) Then 110 If (chkbox.Checked) Then 111 ErrorMessage.InnerHtml = ErrorMessage.InnerHtml & _ 112 String.Format("adding file: {0}<br/>\n", lbl.Text) 113 filesToInclude.Add(System.IO.Path.Combine(sMappedPath,lbl.Text)) 114 End If 115 End If 116 Next 117 118 If (filesToInclude.Count=0) Then 119 ErrorMessage.InnerHtml = ErrorMessage.InnerHtml & "You did not select any files?<br/>\n" 120 Else 121 Response.Clear 122 Response.BufferOutput= false 123 124 Dim enc as Ionic.Zip.EncryptionAlgorithm = Ionic.Zip.EncryptionAlgorithm.None 125 If (chkUseAes.Checked) Then 126 enc = EncryptionAlgorithm.WinZipAes256 127 End If 128 129 Dim c As System.Web.HttpContext = System.Web.HttpContext.Current 130 Dim ReadmeText As String= String.Format("README.TXT\n\nHello!\n\n" & _ 131 "This is a zip file that was dynamically generated at {0}\n" & _ 132 "by an ASP.NET Page running on the machine named '{1}'.\n" & _ 133 "The server type is: {2}\n" & _ 134 "The password used: {3}\n", _ 135 "Encryption: {4}\n", _ 136 System.DateTime.Now.ToString("G"), _ 137 System.Environment.MachineName, _ 138 c.Request.ServerVariables("SERVER_SOFTWARE"), _ 139 tbPassword.Text, _ 140 enc.ToString ) 141 Dim archiveName as String= String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")) 142 Response.ContentType = "application/zip" 143 Response.AddHeader("content-disposition", "filename=" + archiveName) 144 145 Using zip as new ZipFile() 146 ' the Readme.txt file will not be password-protected. 147 zip.AddEntry("Readme.txt", ReadmeText, Encoding.Default) 148 If Not String.IsNullOrEmpty(tbPassword.Text) Then 149 zip.Password = tbPassword.Text 150 zip.Encryption = enc 151 End If 152 153 ' filesToInclude is a string[] or List<String> 154 zip.AddFiles(filesToInclude, "files") 155 156 zip.Save(Response.OutputStream) 157 End Using 158 Response.Close 159 160 End If 161 162End Sub 163 164 165</script> 166 167 168 169<html> 170 <head> 171 <link rel="stylesheet" href="style/basic.css"> 172 </head> 173 174 <body> 175 176 <form id="Form" runat="server"> 177 178 <h3> <span id="Title" runat="server" />Zip Files from ASP.NET </h3> 179 180 <p>This page uses the .NET Zip library (see <a 181 href="http://DotNetZip.codeplex.com">http://DotNetZip.codeplex.com/</a>) 182 to dynamically create a zip archive, and then download it to the 183 browser through Response.OutputStream. This page is implemented 184 in VB.NET.</p> 185 186 <span class="SampleTitle"><b>Check the boxes to select the files, set a password if you like, 187 then click the button to zip them up.</b></span> 188 <br/> 189 <br/> 190 Password: <asp:TextBox id="tbPassword" Password='true' Text="" AutoPostBack runat="server"/> 191 <span style="color:Red">(Optional)</span> 192 <br/> 193 <br/> 194 Use AES?: <asp:CheckBox id="chkUseAes" AutoPostBack runat="server"/> 195 <br/> 196 <br/> 197 <asp:Button id="btnGo" Text="Zip checked files" AutoPostBack OnClick="btnGo_Click" runat="server"/> 198 199 <br/> 200 <br/> 201 <span style="color:red" id="ErrorMessage" runat="server"/> 202 <br/> 203 204 <asp:ListView ID="FileListView" runat="server"> 205 206 <LayoutTemplate> 207 <table> 208 <tr ID="itemPlaceholder" runat="server" /> 209 </table> 210 </LayoutTemplate> 211 212 <ItemTemplate> 213 <tr> 214 <td><asp:Checkbox ID="include" runat="server"/></td> 215 <td><asp:Label id="label" runat="server" Text="<%# Container.DataItem %>" /></td> 216 </tr> 217 </ItemTemplate> 218 219 <EmptyDataTemplate> 220 <div>Nothing to see here...</div> 221 </EmptyDataTemplate> 222 223 </asp:ListView> 224 225 226 </form> 227 228 </body> 229 230</html>