Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将blob数据写入zip并用C#下载?_C#_File_Zip_Bytearray_Blob - Fatal编程技术网

如何将blob数据写入zip并用C#下载?

如何将blob数据写入zip并用C#下载?,c#,file,zip,bytearray,blob,C#,File,Zip,Bytearray,Blob,假设我从数据库中得到了一些斑点。 然后我把它们放在字节数组中。例如: Byte[] lol1=(Byte[])reader["data1"]; Byte[] lol2=(Byte[])reader["data2"]; 现在,我如何将这些字节数组作为文件写入zip并作为文件从C#浏览器下载 //为清晰起见编辑 “Manager.cs”文件中的相关代码如下: public Byte[] FileDownload(string userName) { try

假设我从数据库中得到了一些斑点。 然后我把它们放在字节数组中。例如:

Byte[] lol1=(Byte[])reader["data1"];
Byte[] lol2=(Byte[])reader["data2"];
现在,我如何将这些字节数组作为文件写入zip并作为文件从C#浏览器下载

//为清晰起见编辑

“Manager.cs”文件中的相关代码如下:

    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);
        }
    }
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();
}
“DataDown.aspx.cs”文件中的相关代码如下:

    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);
        }
    }
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文件,该文件是其中唯一的文件。它必须是两个文件。此外,这一个文件已损坏。

要干净地执行此操作,您需要一个仅在.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作为流打开,您可能可以避免将条目读入缓冲区,而是使用

您是否尝试过这样做?请看编辑后的问题。我使用了GZipStream类。System.IO.Compression在我的编译器中没有ZipArchiveEntry类。我正在使用MS Visual Studio 2012。我猜它的框架版本是.NET4.5。我编辑了我的问题。你可以看看。谢谢你的回答。它存在于.NET4.5中。只需确保在项目中包含对System.IO.Compression.dll的引用。我添加了它,你的代码对我有效。谢谢兄弟。