C# 压缩文件(全路径格式 和单独文件压缩格式)

本文介绍了如何使用C#进行文件压缩,包括单个文件的压缩和全路径压缩。提供了两个示例,第一个示例展示了如何压缩单个文件到指定路径,第二个示例演示了如何压缩目录下所有文件,并保留文件夹结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单文件压缩格式,表示加压后为你单独压缩的文件

=======================================================================

        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileToZip">要进行压缩的文件名</param>
        /// <param name="ZipedFile">压缩后生成的压缩文件名</param>
        /// <returns></returns>
        public bool ZipFile(string FileToZip, string ZipedFile)
        {
            //如果文件没有找到,则报错
            if (!File.Exists(FileToZip))
            {
                throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
            }
            //FileStream fs = null;
            FileStream ZipFile = null;
            ZipOutputStream ZipStream = null;
            ZipEntry ZipEntry = null;
            bool res = true;
            try
            {
                ZipFile = File.OpenRead(FileToZip);
                byte[] buffer = new byte[ZipFile.Length];
                ZipFile.Read(buffer, 0, buffer.Length);
                ZipFile.Close();
                ZipFile = File.Create(ZipedFile);
                ZipStream = new ZipOutputStream(ZipFile);           
                ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
                ZipStream.PutNextEntry(ZipEntry);
                ZipStream.SetLevel(6);
                ZipStream.Write(buffer, 0, buffer.Length);
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (ZipEntry != null)
                {
                    ZipEntry = null;
                }
                if (ZipStream != null)
                {
                    ZipStream.Finish();
                    ZipStream.Close();
                }
                if (ZipFile != null)
                {
                    ZipFile.Close();
                    ZipFile = null;
                }
                GC.Collect();
                GC.Collect(1);
            }

            return res;
        }

---------------------------------------------------

用法:

 ZipClass.ZipClass Zc = new ZipClass.ZipClass(); 

 //ZipClass是一个名为ZipClass类库中的ZipClass类  ,

//实例一个类的对象就可以调用里面的方法了


            Zc.ZipFile("D:\\下载程序\\WebDownloadService\\WebDownloadService\\UploadFile\\sjpt.txt", "D:\\下载程序\\WebDownloadService\\WebDownloadService\\UploadFile\\sjpt.zip");

 

=====================================================

全路径压缩格式,指的是 你解压后的文件 是一个带有压缩路径的文件,而你需要的文件在层层路径的文件夹里

---------

   //public void ZipFileMain(string[] args)
        //   {
        //    string[] filenames = Directory.GetFiles(args[0]);

        //    Crc32 crc = new Crc32();
        //    ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));

        //    s.SetLevel(6); // 0 - store only to 9 - means best compression

        //    foreach (string file in filenames)
        //    {
        //         //打开压缩文件
        //         FileStream fs = File.OpenRead(file);

        //         byte[] buffer = new byte[fs.Length];
        //         fs.Read(buffer, 0, buffer.Length);
        //         ZipEntry entry = new ZipEntry(file);

        //         entry.DateTime = DateTime.Now;

        //         // set Size and the crc, because the information
        //         // about the size and crc should be stored in the header
        //         // if it is not set it is automatically written in the footer.
        //         // (in this case size == crc == -1 in the header)
        //         // Some ZIP programs have problems with zip files that don't store
        //         // the size and crc in the header.
        //         entry.Size = fs.Length;
        //         fs.Close();

        //         crc.Reset();
        //         crc.Update(buffer);

        //         entry.Crc  = crc.Value;

        //         s.PutNextEntry(entry);

        //         s.Write(buffer, 0, buffer.Length);

        //    }

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

----------------------------------------------------

用法:

 //string[] FileProperties = new string[2];
            //FileProperties[0] = @"D:\下载程序\WebDownloadService\WebDownloadService\UploadFile";//待压缩文件目录
            //FileProperties[1] = @"D:\下载程序\WebDownloadService\WebDownloadService\UploadFile\sjpt.zip";  //压缩后的目标文件         
            //ZipClass.ZipClass Zc = new ZipClass.ZipClass();
            //Zc.ZipFileMain(FileProperties);
            //Response.Write("文件已经压缩成功!");

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值