【问题标题】:C# Asp.Net Create text file, zip it, and save to Blob - Without writing anything to diskC# Asp.Net 创建文本文件,将其压缩并保存到 Blob - 无需将任何内容写入磁盘
【发布时间】:2012-08-04 05:19:58
【问题描述】:

这里很复杂,反正对我来说很好:)

基本上我想要实现的是生成一些文本,将此文本文件压缩到两个目录中,然后将其上传到 MySQL blob 字段 - 所有这些都无需向磁盘写入任何内容。我对这一切都比较陌生,因此非常感谢任何指针。到目前为止,这是我整理的内容,它显然会崩溃和烧毁,但希望能更好地了解 id 喜欢做什么。哦,我目前正在使用 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;
            }
        }

** 编辑 - 靠近 ***

我现在可以创建一个文本文件并将其压缩到内存中,问题是文本没有显示在文件中 - 即它的空白我现在将文件放在两个目录中:)

以下修改代码

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

【问题讨论】:

  • 哪一部分崩溃了,如果有错误消息/堆栈跟踪怎么办?
  • 在这个版本上,我无法在 zip.Save(ms2) 访问封闭的流 - 但我不确定我是否使用这种方法走上了正确的道路:(

标签: c# asp.net mysql zip streamwriter


【解决方案1】:

基本上这是我想要实现的全部编译,所以我认为 id 为任何需要类似东西的人整理了这个 - 这是一段工作代码

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

        }

【讨论】:

    【解决方案2】:

    您可以使用开源 c# 压缩库 SharpZipLib 创建内存中的 zip 文件,如下所述:In Memory compression using SharpZipLib

    // zip XElement xdoc and add to requests MTOM value
    using (MemoryStream ms = new System.IO.MemoryStream())
    {   
       xdoc.Save(ms);  
       ms.Position = 0;
    
       // create the ZipEntry archive from the xml doc store in memory stream ms
       using (MemoryStream outputMS = new System.IO.MemoryStream())
       {
          using (ZipOutputStream zipOutput = new ZipOutputStream(outputMS))
          {
              ZipEntry ze = new ZipEntry("example.xml");
              zipOutput.PutNextEntry(ze);
              zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
              zipOutput.Finish();
              zipOutput.Close();
    
              // add the zip archive to the request
              SubmissionReceiptListAttachmentMTOM = new base64Binary();
              SubmissionReceiptListAttachmentMTOM.Value = outputMS.ToArray();
          }
    
          outputMS.Close();
       }
    
       ms.Close();
    }
    

    现在您只需要将内存流转换为字节数组并保存在数据库中。

    【讨论】:

    • SubmissionReceiptListAttachmentMTOM 似乎在最新版本中不存在
    • 我跟随您的代码并在编辑中进行了必要的更改 - 知道为什么文本文件是空白的吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 2020-07-15
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多