C# 压缩、解压缩文件

该博客介绍了如何使用C#和ICSharpCode.SharpZipLib库来实现文件的压缩和解压缩操作。示例代码展示了如何创建ZipOutputStream进行文件压缩,并通过ZipInputStream解压缩文件到指定目录。同时,博客提到了如何处理可能出现的文件重名问题,通过检查并修改文件名避免重名冲突。
摘要由CSDN通过智能技术生成

用了 Log4net 记录日志,如果没有 Log4net 的,将 Log4net  注释即可运行

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip.Compression;
using log4net;


namespace FiSMDataClient.Service
{
    public class ZipLib
    {
        private static readonly ILog LOG = LogManager.GetLogger(typeof(FileLib));
        public ArrayList fileList = new ArrayList();
        string changname=null;
      
        #region 压缩文件
        /// <summary>
        /// 压缩多文件(调用之前先为fileList 赋值)
        /// </summary>
        /// <param name="outZipPath">输出Zip文件的全路径</param>
        public void Compress(string outZipPath)
        {
            try
            {
                Crc32 crc = new Crc32();
                ZipOutputStream zipStream = new ZipOutputStream(File.Create(outZipPath));
                zipStream.SetLevel(9); // 0 - store only to 9 - means best compression
                foreach (string file in FileList)
                {
                    FileStream fStream = File.OpenRead(file);
                    byte[] buffer = new byte[fStream.Length];
                    fStream.Read(buffer, 0, buffer.Length);
                    string filename = file.Substring(file.LastIndexOf("//") + 1, file.Length - file.LastIndexOf("//") - 1);
                    ZipEntry entry = new ZipEntry(filename);
                    entry.DateTime = DateTime.Now;
                    entry.Size = fStream.Length;
                    fStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zipStream.PutNextEntry(entry);
                    zipStream.Wri

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值