SharpZipLib

M

Mike P

I am using SharpZipLib to zip some files, and now I want to give the
user the option of where to save the zip file. My problem is that I am
not sure how to refer to the zip file in my code, when I refer to the
zipfile name and allow the user to save the file, all I get is an empty
zip file.

Here is my code :

private void BuildZip(string ZipName)
{
try
{
using (ZipOutputStream s = new
ZipOutputStream(File.Create(ConfigurationManager.AppSettings["ZipCreateL
ocation"].ToString() + ZipName.ToString())))
{
s.SetLevel(9); // 0 - store only to 9 - means best
compression
byte[] buffer = new byte[4096];

foreach (GridViewRow gvr in Results.Rows)
{
CheckBox chkBox =
(CheckBox)gvr.FindControl("RowLevelCheckBox");

if (chkBox.Checked == true)
{
string strFilename =
gvr.Cells[0].Text.ToString();
string strFullPath = Session["Location"] + "\\"
+ strFilename;

ZipEntry entry = new
ZipEntry(Path.GetFileName(strFullPath));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);

using (FileStream fs =
File.OpenRead(strFullPath))
{
// Using a fixed size buffer here makes no
noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0,
buffer.Length);
s.Write(buffer, 0, sourceBytes);
}
while (sourceBytes > 0);
}
}
}

Response.AddHeader("Content-Disposition", "attachment;
filename=" + ZipName.ToString());

// Finish/Close arent needed strictly as the using
statement does this automatically
// Finish is important to ensure trailing information
for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish();
// Close is important to wrap things up and unlock the
file.
s.Close();
}

lblDownloadResult.Visible = true;
lblDownloadResult.Text = "Download successful";

}
catch (Exception exc)
{

}
}

The code works as far as creating the zip file at a location set in the
code, but using this line to allow the user to save the zip file only
results in an empty zip file :

Response.AddHeader("Content-Disposition", "attachment; filename=" +
ZipName.ToString());


Any help would be really appreciated.
 
G

Guest

filename=" + ZipName.ToString());

is only the filename, it doesn't provide the full path to the file. You have
the full path earlier in your code. Use that.
Peter
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mike said:
I am using SharpZipLib to zip some files, and now I want to give the
user the option of where to save the zip file. My problem is that I am
not sure how to refer to the zip file in my code, when I refer to the
zipfile name and allow the user to save the file, all I get is an empty
zip file.
The code works as far as creating the zip file at a location set in the
code, but using this line to allow the user to save the zip file only
results in an empty zip file :

Response.AddHeader("Content-Disposition", "attachment; filename=" +
ZipName.ToString());

You code saves the zip file on the server and then set
a content-disposition header in the response.

That does not make sense to me.

You can do one of:

1) stream the bytes directly back to the client instead of
saving on the server and set content-disposition header
in the response.

2) save to file on the server and send an A HREF to the
client with an URL that get mapped to the saved file.

I would recommend #1.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top