使用C#Ado.net从数据库下载.zip文件

时间:2017-07-07 19:34:01

标签: c# ado.net

我在数据库中有一个.zip文件(BLOB)。我想在我的api中检索相同的内容。请在下面找到代码段。我可以下载一个zip文件,但无法解压缩。提取时出错。

public IHttpActionResult GetDownloadLetter()
{
    DownloadDocument docInfo = blogicObj.DownloadLetter();
    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(docInfo.Document.GetBuffer())
    };
    result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
    {
        FileName = docInfo.DocumentName + ".zip"
    };

    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
    var response = ResponseMessage(result);
    return response;
}


public class DownloadDocument
{
    public MemoryStream Document { get; set; }
    public string DocumentType { get; set; }
    public string DocumentName { get; set; }
}

public DownloadDocument DownloadDocument()
{
    DownloadDocument letter = null;
    try
    {
        letter = GetDummyDownload();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return letter;
}

public MemoryStream GetMemoryStream(SqlDataReader reader, int columnIndex)
{
    using (MemoryStream stream = new MemoryStream())
    {
        long startIdx = 0;
        byte[] buffer = new byte[256];

        while (true)
        {
            long retBytes = reader.GetBytes(columnIndex, startIdx, buffer, 0, buffer.Length);
            stream.Write(buffer, 0, (int)retBytes);
            startIdx += retBytes;
            if (retBytes != buffer.Length)
                break;
        }
        return stream;
    }
}

public DownloadDocument GetDummyDownload()
{
    DownloadDocument letter = null;
    string strQuery = "select * from [dbo].[documents] where id=1";

    using (SqlConnection connection = new SqlConnection(connStr))
    {
        SqlCommand command = new SqlCommand(connStr);
        command.CommandType = CommandType.Text;
        command.CommandText = strQuery;
        command.Connection = connection;
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        // Call Read before accessing data.
        while (reader.Read())
        {
            letter = new DownloadDocument();
            letter.Document = GetMemoryStream(reader, 4); //4 is for document column
            letter.DocumentType = Convert.ToString(reader["DocumentType"]);
            letter.DocumentName = Convert.ToString(reader["Name"]);
        }
        // Call Close when done reading.
        reader.Close();
    }
    return letter;
}

2 个答案:

答案 0 :(得分:0)

您的C#应用​​程序可以使用J#库来提取zip文件。它基本上是Java库:

using(var fis = new java.io.FileInputStream(FileName))
{
    using(var zis = new java.util.zip.ZipInputStream(fis))
    {
        java.util.zip.ZipEntry ze;
        while((ze = zis.getNextEntry()) != null)
        {
            if (ze.isDirectory())
                continue;

            Console.WriteLine("File name: " + ze.getName());
        }
    }
}

答案 1 :(得分:0)

public IHttpActionResult GetDownloadLetter()
{
    DownloadDocument docInfo = blogicObj.DownloadLetter();
    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(docInfo.Document.ToArray())
    };
    result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
    {
        FileName = docInfo.DocumentName + ".zip"
    };

    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
    var response = ResponseMessage(result);
    return response;
}


public class DownloadDocument
{
    public MemoryStream Document { get; set; }
    public string DocumentType { get; set; }
    public string DocumentName { get; set; }
}

public DownloadDocument DownloadDocument()
{
    DownloadDocument letter = null;
    try
    {
        letter = GetDummyDownload();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return letter;
}

public MemoryStream GetMemoryStream(SqlDataReader reader, int columnIndex)
{
      byte[] buffer = (byte[])reader.GetValue(columnIndex);
            MemoryStream stream = new MemoryStream(buffer);
            return stream;
}

public DownloadDocument GetDummyDownload()
{
    DownloadDocument letter = null;
    string strQuery = "select * from [dbo].[documents] where id=1";

    using (SqlConnection connection = new SqlConnection(connStr))
    {
        SqlCommand command = new SqlCommand(connStr);
        command.CommandType = CommandType.Text;
        command.CommandText = strQuery;
        command.Connection = connection;
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        // Call Read before accessing data.
        while (reader.Read())
        {
            letter = new DownloadDocument();
            letter.Document = GetMemoryStream(reader, 4); //4 is for document column
            letter.DocumentType = Convert.ToString(reader["DocumentType"]);
            letter.DocumentName = Convert.ToString(reader["Name"]);
        }
        // Call Close when done reading.
        reader.Close();
    }
    return letter;
}