Zip 文件在尝试提取时显示为空
Zip file shows empty on trying to extract
我正在使用以下代码下载一组存储在我的数据库中的文件,方法是将它们放入一个 zip 文件中:
SqlDataAdapter adp = new SqlDataAdapter("select FILE_NAME, FILE, content_type from tbl where id = " + "168", Configuration.getSQLConnString("ConnStr"));
DataTable dtFiles = new DataTable();
adp.Fill(dtFiles);
if (dtFiles.Rows.Count > 0)
{
using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
foreach (DataRow dr in dtFiles.Rows)
{
byte[] bytes;
string fileName, contentType;
fileName = dr["File_Name"].ToString();
bytes = (byte[])dr["File"];
contentType = dr["Content_Type"].ToString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("Content-Disposition", "attachment; filename=Files.zip");
Response.ContentType = "application/zip";
byte[] fileBytes = bytes;
ZipEntry fileEntry = new ZipEntry(ZipEntry.CleanName(fileName));
fileEntry.Size = fileBytes.Length;
zipStream.SetLevel(3);
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
zipStream.CloseEntry();
}
zipStream.Flush();
zipStream.Close();
}
}
生成并下载了 zip 文件,它还显示了大约 2Mb 的大小,但是当我提取 zip 文件时,它显示了以下错误:
有人可以指出我做错了什么吗?
ZipOutputStream
对我一点帮助都没有;试着打破我的头脑,但没有任何帮助。然后我遇到了这个名为 DotNetZip
的库,这是将存储在数据库中的文件下载到 zip 文件中的代码:
using (ZipFile zip = new ZipFile())
{
foreach (DataRow dr in dtResumes.Rows)
{
zip.AddEntry(dr["File_Name"].ToString(), (byte[])dr["Resume"]);
}
zip.Save("C:\Test\MyZipFile.zip");
}
我正在使用以下代码下载一组存储在我的数据库中的文件,方法是将它们放入一个 zip 文件中:
SqlDataAdapter adp = new SqlDataAdapter("select FILE_NAME, FILE, content_type from tbl where id = " + "168", Configuration.getSQLConnString("ConnStr"));
DataTable dtFiles = new DataTable();
adp.Fill(dtFiles);
if (dtFiles.Rows.Count > 0)
{
using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
foreach (DataRow dr in dtFiles.Rows)
{
byte[] bytes;
string fileName, contentType;
fileName = dr["File_Name"].ToString();
bytes = (byte[])dr["File"];
contentType = dr["Content_Type"].ToString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("Content-Disposition", "attachment; filename=Files.zip");
Response.ContentType = "application/zip";
byte[] fileBytes = bytes;
ZipEntry fileEntry = new ZipEntry(ZipEntry.CleanName(fileName));
fileEntry.Size = fileBytes.Length;
zipStream.SetLevel(3);
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
zipStream.CloseEntry();
}
zipStream.Flush();
zipStream.Close();
}
}
生成并下载了 zip 文件,它还显示了大约 2Mb 的大小,但是当我提取 zip 文件时,它显示了以下错误:
ZipOutputStream
对我一点帮助都没有;试着打破我的头脑,但没有任何帮助。然后我遇到了这个名为 DotNetZip
的库,这是将存储在数据库中的文件下载到 zip 文件中的代码:
using (ZipFile zip = new ZipFile())
{
foreach (DataRow dr in dtResumes.Rows)
{
zip.AddEntry(dr["File_Name"].ToString(), (byte[])dr["Resume"]);
}
zip.Save("C:\Test\MyZipFile.zip");
}