如何将Blob数据写入zip并在C#中下载? [英] How can I write blob datas to zip and download it in C#?

查看:43
本文介绍了如何将Blob数据写入zip并在C#中下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我从数据库中得到了一些斑点.然后,我将它们放入Byte数组中.例如:

Assume that I got some blobs from database. Then I put them in Byte arrays. For example:

Byte[] lol1=(Byte[])reader["data1"];
Byte[] lol2=(Byte[])reader["data2"];

现在如何将这些字节数组作为文件写入zip并从C#中的浏览器中下载为文件?

Now how can i write these byte arrays as file into zip and download it as file from browser in C#?

//为清楚起见进行编辑

// Edit for clarity

"Manager.cs"文件中的相关代码,例如:

Relevant codes in "Manager.cs" file like:

    public Byte[] FileDownload(string userName)
    {
        try
        {
            MySqlDataReader reader = new MySqlCommand("SELECT veri FROM veriler WHERE kullanici_id = (SELECT id FROM uyeler WHERE kullanici_adi='" + userName + "')", con).ExecuteReader();
            MemoryStream ms = new MemoryStream();
            GZipStream gzs = new GZipStream(ms, CompressionMode.Compress);
            while (reader.Read())
                gzs.Write((Byte[])reader["veri"], 0, ((Byte[])reader["veri"]).Length);
            return ms.ToArray();
        }
        catch (Exception)
        {
            return Encoding.UTF8.GetBytes(string.Empty);
        }
    }

"DataDown.aspx.cs"文件中的相关代码,例如:

Relevant codes in "DataDown.aspx.cs" file like:

protected void download_Click(object sender, EventArgs e)
{
    Response.AddHeader("Content-type", ContentType);
    Response.AddHeader("Content-Disposition", "attachment; filename=Archive.zip");
    Response.BinaryWrite(new Manager().FileDownload(Session["user"].ToString()));
    Response.Flush();
    Response.End();
}

它返回一个.zip文件,它只是其中的文件.它必须是两个文件.而且,这个文件已损坏.

It returns a .zip file which is only file in it. It must be two files. Moreover, this one file is corrupted.

推荐答案

要干净整洁,您需要

To do it cleanly, you'll need System.IO.Compression which is only available in .Net 4.5 forward.

string blobName = "data1";
string zipName = "database.zip";
Byte[] blob = (Byte[])reader[blobName];

using(MemoryStream zs = new MemoryStream())
{
  // Build the archive
  using(System.IO.Compression.ZipArchive zipArchive = new ZipArchive(zs, ZipArchiveMode.Create, true))
  {
    System.IO.Compression.ZipArchiveEntry archiveEntry = zipArchive.CreateEntry(blobName);
    using(Stream entryStream = archiveEntry.Open())
    {
      entryStream.Write(blob, 0/* offset */, blob.Length);
    }
  }

  //Rewind the stream for reading to output.
  zs.Seek(0,SeekOrigin.Begin);

  // Write to output.
  Response.Clear();
  Response.ContentType = "application/zip";
  Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", zipName));
  Response.BinaryWrite(zs.ToArray());
  Response.End();
 }

如果数据提供者支持将blob作为流打开,则可以避免读入缓冲区中的条目,而使用

If your data provider supports opening the blob as a stream, you can probably avoid reading the entry into a buffer, and instead use Stream.CopyTo()

这篇关于如何将Blob数据写入zip并在C#中下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆