打包ZIP

本文介绍了一种使用C#实现文件和目录压缩的方法。通过示例代码展示了如何利用ZipOutputStream来压缩单个文件和整个目录,并支持设置压缩级别及密码保护。
    public static bool ZipFile(string fileToZip, string zipedFile, string password) 
     { 
           bool result = true; 
           ZipOutputStream zipStream = null; 
           FileStream fs = null; 
            ZipEntry ent = null;

            if (!File.Exists(fileToZip))
                return false; 
 
           try 
           { 
                fs = File.OpenRead(fileToZip); 
               byte[] buffer = new byte[fs.Length]; 
                fs.Read(buffer, 0, buffer.Length); 
                fs.Close(); 
 
                fs = File.Create(zipedFile); 
               zipStream = new ZipOutputStream(fs); 
               if (!string.IsNullOrEmpty(password)) zipStream.Password = password; 
                ent = new ZipEntry(Path.GetFileName(fileToZip)); 
                zipStream.PutNextEntry(ent);                 zipStream.SetLevel(6); 
 
                zipStream.Write(buffer, 0, buffer.Length); 
              } 
            catch 
            { 
                result = false; 
            } 
            finally 
            { 
              if (zipStream != null) 
                {                      zipStream.Finish(); 
                   zipStream.Close(); 
               } 
                if (ent != null) 
                { 
                    ent = null; 
                } 
               if (fs != null) 
               { 
                   fs.Close(); 
                    fs.Dispose(); 
                } 
            } 
            GC.Collect(); 
            GC.Collect(1); 
 
            return result;         
       
       
    } 



 public static bool ZipDirectory(string folderToZip, string zipedFile, string password) 
      { 
            bool result = false; 
           if (!Directory.Exists(folderToZip)) 
               return result; 

           ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)); 
            zipStream.SetLevel(6); 
           if (!string.IsNullOrEmpty(password)) zipStream.Password = password;

           result = ZipDirectory(folderToZip, zipStream, ""); 
 
            zipStream.Finish(); 
            zipStream.Close(); 
 
            return result; 
        } 



          /// <param name="folderToZip">要压缩的文件夹路径</param>  
        /// <param name="zipStream">压缩输出流</param>  
        /// <param name="parentFolderName">此文件夹的上级文件夹</param>  
        /// <returns></returns>  


private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName) 
       { 
           bool result = true; 
           string[] folders, files; 
            ZipEntry ent = null; 
           FileStream fs = null; 
            Crc32 crc = new Crc32(); 
 
           try 
           { 
              ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); 
                zipStream.PutNextEntry(ent); 
               zipStream.Flush(); 

               files = Directory.GetFiles(folderToZip); 
              foreach (string file in files) 
               { 
                   fs = File.OpenRead(file); 
 
                   byte[] buffer = new byte[fs.Length]; 
                   fs.Read(buffer, 0, buffer.Length); 
                    ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file))); 
                    ent.DateTime = DateTime.Now; 
                   ent.Size = fs.Length; 
 
                  fs.Close(); 
 
                   crc.Reset(); 
                   crc.Update(buffer); 
 
                   ent.Crc = crc.Value; 
                    zipStream.PutNextEntry(ent); 
                   zipStream.Write(buffer, 0, buffer.Length); 
               } 
 
           } 
           catch 
          { 
               result = false; 
          } 
            finally 
            { 
                if (fs != null) 
                { 
                   fs.Close(); 
                    fs.Dispose(); 
                } 
                if (ent != null) 
               { 
                   ent = null; 
                } 
               GC.Collect(); 
              GC.Collect(1); 
            } 
 
            folders = Directory.GetDirectories(folderToZip); 
           foreach (string folder in folders) 
                if (!ZipDirectory(folder, zipStream, folderToZip)) 
                    return false; 
 
            return result; 
        } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值