C
Cal Who
This code works OK if the file is small but if the "do" loop has to iterate
a second time the file is NG.
When I open the zip the file is there but if I try to copy it or open it the
system ignores my command.
I debug and see the loop traversed more than once to produce a problem file.
The file for which it only passes the code once are OK
I copied this code. It did not have the CloseEntry and had the problem
before I added it.
Better with it or without?
Can you tell me how to fix this?
Or something to try?
Thanks
private void ZipAllFiles()
{
byte[] buffer = new byte[4096];
// the path on the server where the temp file will be created!
var tempFileName = Server.MapPath(@"~/Tmp.zip");
var zipOutputStream = new ZipOutputStream(File.Create(tempFileName));
var filePath = String.Empty;
var fileName = String.Empty;
var readBytes = 0;
foreach(GridViewRow row in gvFiles.Rows)
{
var isChecked = (row.FindControl("chkSelect") as CheckBox).Checked;
if (!isChecked) continue;
filePath = (row.FindControl("lblFilePath") as Label).Text;
fileName = (row.FindControl("lblFileName") as Label).Text;
var zipEntry = new ZipEntry(fileName);
zipOutputStream.PutNextEntry(zipEntry);
using(var fs = File.OpenRead(filePath))
{
do
{
readBytes = fs.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer,0,readBytes);
} while (readBytes > 0);
zipOutputStream.CloseEntry(); //CAG added
}
}
if (zipOutputStream.Length == 0)
{
lblMessage.Text = "Please select at least one file!";
return;
}?
zipOutputStream.Finish();
zipOutputStream.Close();
Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("Content-Disposition", "attachment;
filename=YourFile.zip");
Response.WriteFile(tempFileName);
Response.Flush();
Response.Close();
// delete the temp file
if(File.Exists(tempFileName))
File.Delete(tempFileName);
}
}
a second time the file is NG.
When I open the zip the file is there but if I try to copy it or open it the
system ignores my command.
I debug and see the loop traversed more than once to produce a problem file.
The file for which it only passes the code once are OK
I copied this code. It did not have the CloseEntry and had the problem
before I added it.
Better with it or without?
Can you tell me how to fix this?
Or something to try?
Thanks
private void ZipAllFiles()
{
byte[] buffer = new byte[4096];
// the path on the server where the temp file will be created!
var tempFileName = Server.MapPath(@"~/Tmp.zip");
var zipOutputStream = new ZipOutputStream(File.Create(tempFileName));
var filePath = String.Empty;
var fileName = String.Empty;
var readBytes = 0;
foreach(GridViewRow row in gvFiles.Rows)
{
var isChecked = (row.FindControl("chkSelect") as CheckBox).Checked;
if (!isChecked) continue;
filePath = (row.FindControl("lblFilePath") as Label).Text;
fileName = (row.FindControl("lblFileName") as Label).Text;
var zipEntry = new ZipEntry(fileName);
zipOutputStream.PutNextEntry(zipEntry);
using(var fs = File.OpenRead(filePath))
{
do
{
readBytes = fs.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer,0,readBytes);
} while (readBytes > 0);
zipOutputStream.CloseEntry(); //CAG added
}
}
if (zipOutputStream.Length == 0)
{
lblMessage.Text = "Please select at least one file!";
return;
}?
zipOutputStream.Finish();
zipOutputStream.Close();
Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("Content-Disposition", "attachment;
filename=YourFile.zip");
Response.WriteFile(tempFileName);
Response.Flush();
Response.Close();
// delete the temp file
if(File.Exists(tempFileName))
File.Delete(tempFileName);
}
}