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.
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.