C# zip文件

ICSharpCode.SharpZipLib.dll


using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;



      public static void ZipFolder(string zipFileName,string zipFolder)

            {
                try
                {
                    string zipFilePath = zipFileName  + ".zip";


                    string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    string keyLogPath = Path.Combine(AppPath, zipFolder);


                    string temp = Path.Combine(keyLogPath, "Translation");
                    string[] fileNames = Directory.GetFileSystemEntries(keyLogPath);


                    // Zip up the files - From SharpZipLib Demo Code
                    using (ZipOutputStream s = new
                    ZipOutputStream(File.Create(zipFilePath)))
                    {
                        s.SetLevel(4); // 0-9, 9 being the highest compression


                        byte[] buffer = new byte[4096];


                        foreach (string file in fileNames)
                        {
                            if (file.Substring(file.Length-3) == "txt")
                            {
                                ZipEntry entry = new
                                ZipEntry(Path.GetFileName(file));


                                entry.DateTime = DateTime.Now;
                                s.PutNextEntry(entry);


                                using (FileStream fs = File.OpenRead(file))
                                {
                                    int sourceBytes;


                                    do
                                    {
                                        sourceBytes = fs.Read(buffer, 0,
                                        buffer.Length);


                                        s.Write(buffer, 0, sourceBytes);


                                    }
                                    while (sourceBytes > 0);
                                }
                            }
                        }


                        s.Finish();
                        s.Close();
                    }


                    //if (File.Exists(zipFilePath))
                    //    Utilities.Paths.SyncReportZipPath = zipFilePath;
                }
                catch (IOException ioex)
                {
                    bool rethrow =
ExceptionPolicy.HandleException(ioex, "BFPolicy");


                    if (rethrow)
                        throw;
                    return;
                }
                catch (Exception ex)
                {
                    bool rethrow =
 ExceptionPolicy.HandleException(ex, "BFPolicy");


                    if (rethrow)
                        throw;
                    return;
                }
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值