我的代码应该从数据库读取二进制值并将其转换为zip文件。它只创建一个43kb大的文件,无法提取。我错过了什么?
com = new SqlCommand("SELECT BinFile FROM tbl_reports WHERE DBKey=411", conString);
byte[] blob = (byte[])com.ExecuteScalar();
File.WriteAllBytes("C:\\" + "test.zip", blob);
答案 0 :(得分:0)
使用DotNetZip,这是示例。
using (ZipFile zip = new ZipFile())
{
zip.Password = pwd;
zip.AddFile(saveFileDialog1.FileName + ".xml");
zip.Save(saveFileDialog1.FileName + ".zip");
}
此外,我认为您需要将byte[]
转换为字符串,然后转换为文本文件
您可以使用convert.frombase64string方法!
修改强>
如果要将byte[]
写入文件,请使用此功能
public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
try
{
System.IO.FileStream _FileStream =
new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);
_FileStream.Close();
return true;
}
catch (Exception _Exception)
{
Console.WriteLine("Exception caught in process: {0}",
_Exception.ToString());
}
return false;
}
并将此功能称为
com = new SqlCommand("SELECT BinFile FROM tbl_reports WHERE DBKey=411", conString);
byte[] blob = (byte[])com.ExecuteScalar();
ByteArrayToFile("C:\\test.txt",blob);