Jaz*_*eff 5 c# mysql asp.net zip streamwriter
这里很复杂,反正对我来说:)
基本上我想要实现的是生成一些文本,将这个文本文件压缩到两个目录中,然后将其上传到MySQL blob字段 - 所有这些都没有写入磁盘.我对这一切都比较新,所以任何指针都非常感激.到目前为止,我有点放在一起,它显然崩溃和烧伤,但希望能更好地了解我喜欢做什么.哦,我现在使用DotNetZip :)
public void broadcastItem()
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);
System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
sw.Write("Some Text generated and placed in a file");
sw.Close(); //Text File Now Created
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(@"Directory1\Directory2");
//Zipping within two directories
ZipEntry e = zip.AddEntry("Test", ms);
e.
e.Comment = "The content for entry in the zip file was obtained from a stream";
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
zip.Save(ms2); //Trying to save to memory stream
}
try
{
OdbcConnection Server = new OdbcConnection("DSN=CentralServer");
Server.Open();
OdbcCommand DbCommand = Server.CreateCommand();
DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
param.Value = ms2;
DbCommand.Parameters.Add(param);
DbCommand.ExecuteNonQuery();
//Trying to save zip file from memory stream to blob field
}
catch (Exception ex)
{
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
** 编辑 - 移动更近 ***
我现在可以创建一个文本文件并将其压缩到内存中,问题是文本没有显示在文件中 - 即它的空白我现在有两个目录中的文件:)
修改后的代码
public void test3()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine("HELLO!");
sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
ms.Position = 0;
// create the ZipEntry archive from the xml doc store in memory stream ms
MemoryStream outputMS = new System.IO.MemoryStream();
ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
ZipEntry ze = new ZipEntry(@"Directory1\Directory2\example.txt");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
byte[] byteArrayOut = outputMS.ToArray();
outputMS.Close();
ms.Close();
try
{
OdbcConnection rstServer = new OdbcConnection("DSN=CentralServer");
Server.Open();
OdbcCommand DbCommand = Server.CreateCommand();
DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
param.Value = byteArrayOut;
DbCommand.Parameters.Add(param);
DbCommand.ExecuteNonQuery();
Response.Write(byteArrayOut.ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,这是我想要实现的目标的全部编译,所以我想我会将其放在一起,供任何需要类似功能的人使用 - 这是一段工作代码
public void diskLess()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine("HELLO!");
sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
// create the ZipEntry archive from the txt file in memory stream ms
MemoryStream outputMS = new System.IO.MemoryStream();
ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
ZipEntry ze = new ZipEntry(@"dir1\dir2\whatever.txt");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
byte[] byteArrayOut = outputMS.ToArray();
outputMS.Close();
ms.Close();
try
{
OdbcConnection rstServer = new OdbcConnection("DSN=CentralServer");
Server.Open();
OdbcCommand DbCommand = Server.CreateCommand();
DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
param.Value = byteArrayOut;
DbCommand.Parameters.Add(param);
DbCommand.ExecuteNonQuery();
Response.Write(byteArrayOut.ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)