C# 用Ionic.Zip壓縮文件卡死問題

1 篇文章 0 订阅

Ionic.Zip.dll  链接:https://pan.baidu.com/s/1p2ziRd38gVmgReXpGIWiXw 提取码:d563

ICSharpCode.SharpZipLib  net2.0 链接:https://pan.baidu.com/s/1lJSe2W9hmIRBZsdkldufHw 提取码:3mpt

ICSharpCode.SharpZipLib  net4.0 链接:https://pan.baidu.com/s/14HKQr0YfRDD67MKeN4bcSA 提取码:8947

1.問題描述

採用:Ionic.Zip.dll的方法

zipFile.Save(ZIPFile); 卡死,無反應,

問題是某些文件在一起時,壓縮卡死,文件單獨壓縮又沒有問題,,具體原因未知

解決方案:換其他的DLL如ICSharpCode.SharpZipLib方法,Ionic.Zip.dll的方法為找有效的方式

Ionic.Zip代碼如下

/// <summary>
        /// 文件壓縮 ,小概率事件zipFile.Save 卡死
        /// </summary>
        /// <param name="files"></param>
        /// <param name="ZIPFile"></param>
        public void OldZIP(string[] files, string ZIPFile)
        {
            try
            {
                using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        string path = files[i];
                        using (System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
                        {
                            byte[] array = new byte[fileStream.Length];
                            fileStream.Read(array, 0, array.Length);
                            string fileName = System.IO.Path.GetFileName(path);
                            zipFile.AddEntry(fileName, array);
                        }
                    }
                    zipFile.Save(ZIPFile);
                }

                // 以下壓縮,中文字出現亂碼,沒出現之前的卡死現象
                //using (System.IO.FileStream fStream = System.IO.File.Create(ZIPFile))
                //{
                //    using (Ionic.Zip.ZipOutputStream zipStream = new Ionic.Zip.ZipOutputStream(fStream))
                //    {
                //        for (int i = 0; i < files.Length; i++)
                //        {
                //            string path = files[i];
                //            using (System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
                //            {
                //                byte[] array = new byte[fileStream.Length];
                //                fileStream.Read(array, 0, array.Length);
                //                string fileName = System.IO.Path.GetFileName(path);
                //                fileStream.Close();
                //                byte[] filbyte = System.Text.Encoding.UTF8.GetBytes(fileName);
                //                zipStream.PutNextEntry(System.Text.Encoding.UTF8.GetString(filbyte));

                //                zipStream.Write(array, 0, array.Length);
                //            }
                //        }
                //    }
                //}
            }
            catch (System.Exception innerException)
            {
                throw new System.Exception("Error zipping folder ", innerException);
            }
        }

換用ICSharpCode.SharpZipLib的方法如下

/// <summary>
        ///  文件壓縮
        /// </summary>
        /// <param name="files">文件集合</param>
        /// <param name="ZIPFile">壓縮文件名稱</param>
        public static void ZIP(string[] files, string ZIPFile)
        {
            try
            {
                ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();

                using (System.IO.FileStream fStream = System.IO.File.Create(ZIPFile))
                {
                    using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fStream))
                    {
                        for (int i = 0; i < files.Length; i++)
                        {
                            string FileToZip = files[i];
                            using (FileStream fs = File.OpenRead(FileToZip))
                            {
                                byte[] buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);
                                string fileName = System.IO.Path.GetFileName(FileToZip);
                                ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
                                entry.DateTime = DateTime.Now;
                                entry.Size = fs.Length;
                                fs.Close();
                                crc.Reset();
                                crc.Update(buffer);
                                entry.Crc = crc.Value;
                                zipStream.PutNextEntry(entry);
                                zipStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }
                }
            }
            catch (System.Exception innerException)
            {
                throw new System.Exception("Error zipping folder ", innerException);
            }
        }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值