Zip文件在尝试提取时显示为空

问题描述 投票:0回答:1

我使用以下代码通过将它们放入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文件时它显示以下错误:

The Compressed (Zipped) folder is empty.有人可以指出我做错了什么吗?

c# sql asp.net zipfile zipoutputstream
1个回答
0
投票

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");
}
© www.soinside.com 2019 - 2024. All rights reserved.