如何将文件附加到dotnet zip库c#

时间:2015-04-22 10:59:13

标签: c# dotnetzip

我使用dotnet zip库创建zip文件。我的文件保存到数据库中,我正在读取每一行并从数据库将文件数据加载到内存中,并将内存流保存到zip文件中。我的下面的代码工作正常。

using System.IO;
using Ionic.Zip;

private void button2_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConn = new SqlConnection(@"Data Source=BBATRIDIP\SQLSERVER2008R2;Initial Catalog=test;Integrated Security=True"))
            {
                string query = String.Format(@"SELECT Name, ContentType, Data FROM [TestTable]");
                SqlCommand cmd = new SqlCommand(query, sqlConn);
                cmd.Connection.Open();

                System.IO.MemoryStream memStream = null;
                ZipFile zip = new ZipFile();
                zip.MaxOutputSegmentSize = 1024 * 1024; // 1MB each segment size would be
                // the above line would split zip file into multiple files and each file
                //size would be 1MB
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        byte[] data = (byte[])reader["Data"];
                        memStream = new System.IO.MemoryStream(data);
                        string strFile = reader["Name"].ToString() + "\\" + reader["ContentType"].ToString();
                        ZipEntry ze = zip.AddEntry(strFile, memStream);
                        int xx = 0;
                    }
                }
                zip.Save(@"e:\MyCustomZip.zip");

                memStream.Dispose();
                MessageBox.Show("Job Done");
                // here u can save the zip in memory stream also there is a overload insteaa of saving in HD
            }
        }

但如果我改变了一些代码,那么损坏的zip文件正在创建。如果我更改此行zip.Save(@"e:\MyCustomZip.zip");,则会出现问题。

    using (SqlConnection sqlConn = new SqlConnection(@"Data Source=BBATRIDIP\SQLSERVER2008R2;Initial Catalog=test;Integrated Security=True"))
    {
        string query = String.Format(@"SELECT Name, ContentType, Data FROM [TestTable]");
        SqlCommand cmd = new SqlCommand(query, sqlConn);
        cmd.Connection.Open();

        System.IO.MemoryStream memStream = null;
        ZipFile zip = new ZipFile();
        zip.MaxOutputSegmentSize = 1024 * 1024; // 1MB each segment size would be
        // the above line would split zip file into multiple files and each file
        //size would be 1MB
        zip.Save(@"e:\MyCustomZip.zip");
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                byte[] data = (byte[])reader["Data"];
                memStream = new System.IO.MemoryStream(data);
                string strFile = reader["Name"].ToString() + "\\" + reader["ContentType"].ToString();
                ZipEntry ze = zip.AddEntry(strFile, memStream);
                int xx = 0;
            }
        }


        memStream.Dispose();
        MessageBox.Show("Job Done");
        // here u can save the zip in memory stream also there is a overload insteaa of saving in HD
    }

我想最初创建零kb的zip,当我将循环添加到zip时,zip文件大小将逐渐增加。如果你看到我上面的代码,我尝试这样做,但我没有成功。我在代码中做错了什么?

另一个问题

点对点压缩比不好。我使用dotnet zip压缩了一个152 KB的doc文件,当创建zip时,我的zip文件大小为136KB。是否有任何调整存在,创建小尺寸的zip文件。分享知识。感谢

1 个答案:

答案 0 :(得分:1)

因此,查看documentation for ZipFile.Save,我们会看到以下内容:

  

在创建新的zip文件或更新zip存档时使用此选项。

因此,您似乎需要反复调用此方法来获取您正在寻找的“更新”行为。因此,只需将Save移动到循环中:

//...
ZipFile zip = new ZipFile();
zip.MaxOutputSegmentSize = 1024 * 1024; 

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        byte[] data = (byte[])reader["Data"];
        memStream = new System.IO.MemoryStream(data);
        string strFile = 
            reader["Name"].ToString() + "\\" + reader["ContentType"].ToString();
        ZipEntry ze = zip.AddEntry(strFile, memStream);
        zip.Save(@"e:\MyCustomZip.zip");
    }
}

关于你的第二个问题(从来不是一个好主意,试着坚持每个问题一个问题):

在不知道您正在压缩什么的情况下,很难推测您可能实现的压缩率。如果数据已经被压缩(例如压缩的图像/视频,例如jpeg / h264),压缩不会给你任何收益。你对内容本质的唯一暗示是它是一个“doc”。如果您正在谈论一个现代Word文档(docx),这已经是一个zip压缩文件夹结构。收益将微乎其微。如果它是另一种文档,也许它包含嵌入式(压缩)媒体?这也是相对不可压缩的。