2017-07-07 11 views
1

データベース(BLOB)に.zipファイルがあります。私は私のAPIで同じものを取得したい。以下のコードスニペットをご覧ください。私はzipファイルをダウンロードできますが、それを解凍することはできません。抽出中にエラーが発生しました。C#Ado.netを使用してデータベースから.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; 
} 
+0

ので、あなたが得ているエラーは何です..:それは基本的にJavaライブラリですか? – MethodMan

+0

次のファイルを開くことができなかったか、有効なzipファイルではありません。 – thegautamnayak

答えて

0

C#アプリケーションでは、J#ライブラリを使用してzipファイルを抽出できます。

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()); 
     } 
    } 
} 
+0

私はメモリストリームに入っています。どのように私はこれを使用できますか? – thegautamnayak

+0

FileInputStreamの代わりにByteArrayInputStreamを使用します。 – JazzSoft

+0

ありがとう@ジャズソフト。それはうまくいった。 – thegautamnayak

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; 
} 
関連する問題