Tom
Tom

Reputation: 6697

asp.net how to zip a file and start uploading it to user

I have an existing ZIP file (with some files) where I want to add a few more files.

When user clicks a download button, this ZIP should be modified just a little and downloading to browser needs to start. ( There can be several users at time of course, and each one need to receive a little bit different zip file.)

Is there this kind of feature/library in asp.net to modify zip file

Upvotes: 1

Views: 764

Answers (3)

Edo
Edo

Reputation: 1841

If you are using .NET 3.5, you should be able to add/remove files to a zip archive. If you are using .NET 2.0, then you need to use a library such as SharpZipLib.

Upvotes: 0

tsilb
tsilb

Reputation: 8037

As for the downloading issue:

I posted this code for a Download page that you would populate in an IFrame (in the case of AJAX). You can take the stream code only if you don't use AJAX.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Net
Imports system.io

Partial Class DownloadFile
Inherits System.Web.UI.Page

Protected Sub page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim url As String = Request.QueryString("DownloadUrl")
If url Is Nothing Or url.Length = 0 Then Exit Sub

'Initialize the input stream
Dim req As HttpWebRequest = WebRequest.Create(url)
Dim resp As HttpWebResponse = req.GetResponse()
Dim bufferSize As Integer = 1 

'Initialize the output stream
Response.Clear()
Response.AppendHeader("Content-Disposition:", "attachment; filename=download.zip")
Response.AppendHeader("Content-Length", resp.ContentLength.ToString)
Response.ContentType = "application/download"

'Populate the output stream
Dim ByteBuffer As Byte() = New Byte(bufferSize) {}
Dim ms As MemoryStream = New MemoryStream(ByteBuffer, True)
Dim rs As Stream = req.GetResponse.GetResponseStream()
Dim bytes() As Byte = New Byte(bufferSize) {}
While rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0
Response.BinaryWrite(ms.ToArray())
Response.Flush()
End While

'Cleanup
Response.End()
ms.Close()
ms.Dispose()
rs.Dispose()
ByteBuffer = Nothing
End Sub
End Class

Of course, if the zip file has public access, you could simply do an <a href...&gt>.

Upvotes: 0

Pavel Nikolov
Pavel Nikolov

Reputation: 9541

Look at this thread:

The GZIP format is a standard, documented format. It does support putting multiple files into a single archive, however most GZIP tools produce a compressed (.gz) file that contains just one file. By convention, the root filename is the same, so that compressing Datafile.csv produces a file called Datafile.csv.gz.

The GZIP format is not the same as the .zip format. The GZipStream class can read or write files in the GZIP format, but the class does not produce or read .zip files, and the doc for the class clearly states this:

this class does not inherently provide functionality for adding files to or extracting files from .zip archives.

It is possible, with extra code, to produce or read a zip file, with the help of GZipStream, but it is not trivial.

If you want to read or write Zip files, the best bet is to use a third party library, like DotNetZip. This library is free, and enables your applications to read or write zip files that contain multiple compressed files. These are standard zip files that can be opened by Windows Explorer, or WinZip, or other zip tools. It's simple to use.

  Response.Clear();
  String ReadmeText= "README.TXT\n\nHello!\n\nThis is a zip file that was dynamically generated.";
  string archiveName= String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
  Response.ContentType = "application/zip";
  Response.AddHeader("content-disposition", "filename=" + archiveName);

  using (ZipFile zip = new ZipFile())
  {
    // add a named entry to the zip file, using a string for content
    zip.AddFileFromString("Readme.txt", "", ReadmeText);
    if (!String.IsNullOrEmpty(tbPassword.Text))
    {
      zip.Password = tbPassword.Text;
      if (chkUseAes.Checked)
         zip.Encryption = EncryptionAlgorithm.WinZipAes256;
    }

    // add a bunch of plain files, into the "files" directory
    foreach (var f in filesToInclude)
    {
      zip.AddFile(f, "files");
    }
    zip.Save(Response.OutputStream);
  }
  Response.End();

Upvotes: 3

Related Questions