winform压缩rtf文档,压缩二进制流。存储richtextBox的RTF到数据库,占用空间太大了,求解决方案。

子聪同学 2016-02-25 01:12:35
如题。
我把richTextBox插入SqlServer数据库的代码如下:

MemoryStream ms = new MemoryStream();
rtbText.SaveFile(ms, RichTextBoxStreamType.RichText);
Byte[] bt = ms.ToArray();
sql = string.Format(@"insert Essay Values('{0}','{1}',{2},{3}) SELECT @@identity", id, txtTitle.Text, " @photo", IsPublicNum);
conn = new SqlConnection(strsql);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
SqlParameter sp = new SqlParameter("@photo", bt);
comm.Parameters.Add(sp);
//result = db.ExecuteScalar(sql);
result = comm.ExecuteScalar();
//判断是否成功
if (result != null)
{
fdo.lbText.Text = "保存文章成功!";
}
else
{
fdo.lbText.Text = "保存文章失败!";
}

不过插入到数据库的数据十分庞大...仅插入14张图片占用60MB,不知道在插入的时候能不能对rtf文档进行压缩。
如果能够进行压缩取出来时如何进行解压缩等等
求助各位大神解决
...全文
306 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qbilbo 2016-02-25
  • 打赏
  • 举报
回复
2.0中有GZip
private byte[] GetRichTextBoxData(RichTextBox txt)
        {
            byte[] bs = null;
            using (MemoryStream ms = new MemoryStream())
            {
                txt.SaveFile(ms, RichTextBoxStreamType.RichText);
                bs = ms.ToArray();
            }
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress))
                {
                    zip.Write(bs, 0, bs.Length);
                }
                return ms.ToArray();
            }
        }

        private void LoadRichTextBoxData(RichTextBox txt, byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (MemoryStream ms2 = new MemoryStream())
                    {
                        int b = -1;
                        while ((b = zip.ReadByte()) != -1)
                        {
                            ms2.WriteByte((byte)b);
                        }
                        ms2.Position = 0;
                        txt.LoadFile(ms2, RichTextBoxStreamType.RichText);
                    }
                }
            }
        }
xdashewan 2016-02-25
  • 打赏
  • 举报
回复
刚才有个人发的https://msdn.microsoft.com/zh-cn/library/ms404280(v=vs.110).aspx
子聪同学 2016-02-25
  • 打赏
  • 举报
回复
引用 3 楼 qbilbo 的回复:
引用:System.IO.Compression.dll
//压缩
private byte[] GetRichTextBoxData(RichTextBox txt)
{
        byte[] bs = null;
        using (MemoryStream ms = new MemoryStream())
        {
            txt.SaveFile(ms, RichTextBoxStreamType.RichText);
            bs = ms.ToArray();
        }
        using (MemoryStream ms = new MemoryStream())
        {
            using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
            {
                ZipArchiveEntry readMeEntry = archive.CreateEntry("doc");
                using (Stream s = readMeEntry.Open())
                {
                    s.Write(bs, 0, bs.Length);
                }
            }
            return ms.ToArray();
        }
}

//解压缩
private void LoadRichTextBoxData(RichTextBox txt, byte[] data)
{
            byte[] bs = null;
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read))
                {
                    ZipArchiveEntry entry = archive.GetEntry("doc");
                    using (MemoryStream ms2 = new MemoryStream())
                    {
                        int b = -1;
                        using (var s = entry.Open())
                        {
                            while ((b = s.ReadByte()) != -1)
                            {
                                ms2.WriteByte((byte)b);
                            }
                        }
                        bs = ms2.ToArray();
                    }
                }
            }
            using (MemoryStream newData = new MemoryStream(bs))
            {
                txt.LoadFile(newData, RichTextBoxStreamType.RichText);
            }
}
ZipArchive类只能在.Net4.5后使用。有其它方法压缩他吗。
xdashewan 2016-02-25
  • 打赏
  • 举报
回复
引用 4 楼 a2528490975 的回复:
可惜是.NET 2.0 qvq
找第三方的压缩类库
子聪同学 2016-02-25
  • 打赏
  • 举报
回复
引用 2 楼 qbilbo 的回复:
如果Framework是4.5,试试用ZipArchive先把内容压缩一下,再写入数据库看会不会好点。
可惜是.NET 2.0 qvq
qbilbo 2016-02-25
  • 打赏
  • 举报
回复
引用:System.IO.Compression.dll
//压缩
private byte[] GetRichTextBoxData(RichTextBox txt)
{
        byte[] bs = null;
        using (MemoryStream ms = new MemoryStream())
        {
            txt.SaveFile(ms, RichTextBoxStreamType.RichText);
            bs = ms.ToArray();
        }
        using (MemoryStream ms = new MemoryStream())
        {
            using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
            {
                ZipArchiveEntry readMeEntry = archive.CreateEntry("doc");
                using (Stream s = readMeEntry.Open())
                {
                    s.Write(bs, 0, bs.Length);
                }
            }
            return ms.ToArray();
        }
}

//解压缩
private void LoadRichTextBoxData(RichTextBox txt, byte[] data)
{
            byte[] bs = null;
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read))
                {
                    ZipArchiveEntry entry = archive.GetEntry("doc");
                    using (MemoryStream ms2 = new MemoryStream())
                    {
                        int b = -1;
                        using (var s = entry.Open())
                        {
                            while ((b = s.ReadByte()) != -1)
                            {
                                ms2.WriteByte((byte)b);
                            }
                        }
                        bs = ms2.ToArray();
                    }
                }
            }
            using (MemoryStream newData = new MemoryStream(bs))
            {
                txt.LoadFile(newData, RichTextBoxStreamType.RichText);
            }
}
qbilbo 2016-02-25
  • 打赏
  • 举报
回复
如果Framework是4.5,试试用ZipArchive先把内容压缩一下,再写入数据库看会不会好点。
Justin-Liu 2016-02-25
  • 打赏
  • 举报
回复
主要是图片大吧,看看能不能对图片进行压缩
crystal_lz 2016-02-25
  • 打赏
  • 举报
回复
谁说 GZIP 只能4.5的 我一直用 2.0 写代码 一样用

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧