asp.net中实现文件压缩及解压代码

作者:袖梨 2022-06-25
 代码如下 复制代码

using System;
 using System.IO;
 using ICSharpCode.SharpZipLib.Checksums;
 using ICSharpCode.SharpZipLib.Zip;
 using ICSharpCode.SharpZipLib.GZip;
 
 namespace ZipFile
 {
     ///


     /// 压缩文件
     ///

     public class ZipHelp
     {
         public string ZipName { get; set; }
         ///
         /// 压缩文件夹
         ///

         /// 需要压缩的文件夹路径(全路径)
         /// 压缩后保存的路径且必须带后缀名如:D:aa.zip(如果为空字符则默认保存到同级文件夹名称为源文件名)
         public void ZipFileMain(string zipSourcePath,string zipToFilePath)
         {
             string[] filenames = Directory.GetFiles(zipSourcePath);
             ZipName = zipSourcePath.Substring(zipSourcePath.LastIndexOf("")+1);
             //定义压缩更目录对象
             Crc32 crc = new Crc32();
             ZipOutputStream s = new ZipOutputStream(File.Create(zipToFilePath.Equals("")? zipSourcePath+".zip":zipToFilePath));
 
             s.SetLevel(6); // 设置压缩级别
             //递归压缩文件夹下的所有文件和字文件夹
             AddDirToDir(crc, s,zipSourcePath);
 
             s.Finish();
             s.Close();
         }
         ///
         /// 压缩单个文件
         ///

         /// 需要压缩的文件路径(全路径)
         /// 压缩后保存的文件路径(如果是空字符则默认压缩到同目录下文件名为源文件名)
         public void ZipByFile(string zipSourcePath,string zipToFilePath)
         {
             //定义压缩更目录对象
             Crc32 crc = new Crc32();
             string dirName = zipSourcePath.Substring(zipSourcePath.LastIndexOf("") + 1, zipSourcePath.LastIndexOf(".") - (zipSourcePath.LastIndexOf("") + 1)) + ".zip";
             ZipOutputStream s = new ZipOutputStream(File.Create(zipToFilePath.Equals("")? zipSourcePath.Substring(0,zipSourcePath.LastIndexOf(""))+""+ dirName:zipToFilePath));
             s.SetLevel(6); // 设置压缩级别
             AddFileToDir(crc,s,zipSourcePath,0);
             s.Finish();
             s.Close();
         }
         ///
         /// 压缩单个文件到指定压缩文件夹下(内部调用)
         ///

         ///
         ///
         /// 文件路径
         public void AddFileToDir(Crc32 crc,ZipOutputStream s,string file,int dotype)
         {
             FileStream fs = File.OpenRead(file);
             byte[] buffer = new byte[fs.Length];
             fs.Read(buffer, 0, buffer.Length);
             string filename="";
             if (dotype == 0)
                 filename = file.Substring(file.LastIndexOf("") + 1);
             else
                 filename = file.Substring(file.IndexOf(ZipName));
             ZipEntry entry = new ZipEntry(filename);
             entry.DateTime = DateTime.Now;
             entry.Size = fs.Length;
             fs.Close();
             crc.Reset();
             crc.Update(buffer);
             entry.Crc = crc.Value;
             s.PutNextEntry(entry);
             s.Write(buffer, 0, buffer.Length);
         }
         ///
         /// 递归文件夹层级(内部调用)
         ///

         ///
         ///
         ///
         public void AddDirToDir(Crc32 crc, ZipOutputStream s, string file)
         {
             //添加此文件夹下的文件
             string[] files = Directory.GetFiles(file);
             foreach (string i in files)
             {
                 AddFileToDir(crc,s,i,1);
             }
             //查询此文件夹下的子文件夹
             string[] dirs=Directory.GetDirectories(file);
             foreach (string i in dirs)
             {
                 AddDirToDir(crc,s,i);
             }
         }
     }
 }


-------------------------------------------接下来是解压的类------------------------------------------------------

 代码如下 复制代码

using System;
 using System.Text;
 using System.Collections;
 using System.IO;
 using System.Diagnostics;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Data;
 
 using ICSharpCode.SharpZipLib.BZip2;
 using ICSharpCode.SharpZipLib.Zip;
 using ICSharpCode.SharpZipLib.Zip.Compression;
 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
 using ICSharpCode.SharpZipLib.GZip;
 
 namespace ZipFile
 {
     ///


     /// 解压文件
     ///

     public class UnZipFile
     {
         ///
         /// 解压文件方法
         ///

         /// 源文件
         /// 解压到目录路径(如果为空字符则是解压到当前目录)
         public void UnZip(string UnSourceZip,string UnZipToPath)
         {
             ZipInputStream s = new ZipInputStream(File.OpenRead(UnSourceZip));
 
             ZipEntry theEntry;
             while ((theEntry = s.GetNextEntry()) != null)
             {
 
                 string fileName = Path.GetFileName(theEntry.Name);
 
                 //生成解压目录
                 if (!UnZipToPath.Equals(""))
                     Directory.CreateDirectory(UnZipToPath);
                 else
                     UnZipToPath = UnSourceZip.Substring(0,UnSourceZip.LastIndexOf("")>0?UnSourceZip.LastIndexOf(""):0);
                 if (fileName != String.Empty)
                 {
                     //创建文件夹
                     int startIndex = 0;
                     while(theEntry.Name.IndexOf("/",startIndex)>0)
                     {
                         //计算文件夹路径
                         string dirpath=theEntry.Name.Substring(0, theEntry.Name.IndexOf("/", startIndex));
                         //添加文件夹
                         Directory.CreateDirectory(UnZipToPath.Equals("")? dirpath: UnZipToPath + "//" + dirpath);
                         startIndex = theEntry.Name.IndexOf("/", startIndex)+1;
                     }
                     //解压文件到指定的目录
                     FileStream streamWriter = File.Create(UnZipToPath.Equals("")? theEntry.Name: UnZipToPath + "//" + theEntry.Name);
                     int size = 2048;
                     byte[] data = new byte[2048];
                     while (true)
                     {
                         size = s.Read(data, 0, data.Length);
                         if (size > 0)
                         {
                             streamWriter.Write(data, 0, size);
                         }
                         else
                         {
                             break;
                         }
                     }
 
                     streamWriter.Close();
                 }
             }
             s.Close();
         }
     }
 }

相关文章

精彩推荐