zoukankan      html  css  js  c++  java
  • C# 压缩文件

       最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。

       十年河东十年河西,莫欺少年穷。 

       学无止境,精益求精

       上一节讲述了C# WebApi传参之Post请求-AJAX

       本节探讨C#压缩文件的方法,直接上代码

       如下

     public class ZipUtility
        {
            /// <summary>  
            /// 所有文件缓存  
            /// </summary>  
            List<string> files = new List<string>();
    
            /// <summary>  
            /// 所有空目录缓存  
            /// </summary>  
            List<string> paths = new List<string>();
    
            /// <summary>  
            /// 压缩单个文件  
            /// </summary>  
            /// <param name="fileToZip">要压缩的文件</param>  
            /// <param name="zipedFile">压缩后的文件全名</param>  
            /// <param name="compressionLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>  
            /// <param name="blockSize">分块大小</param>  
            public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
            {
                if (!System.IO.File.Exists(fileToZip))//如果文件没有找到,则报错  
                {
                    throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");
                }
    
                FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read);
                FileStream zipFile = File.Create(zipedFile);
                ZipOutputStream zipStream = new ZipOutputStream(zipFile);
                ZipEntry zipEntry = new ZipEntry(fileToZip);
                zipStream.PutNextEntry(zipEntry);
                zipStream.SetLevel(compressionLevel);
                byte[] buffer = new byte[blockSize];
                int size = streamToZip.Read(buffer, 0, buffer.Length);
                zipStream.Write(buffer, 0, size);
    
                try
                {
                    while (size < streamToZip.Length)
                    {
                        int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                    }
                }
                catch (Exception ex)
                {
                    GC.Collect();
                    throw ex;
                }
    
                zipStream.Finish();
                zipStream.Close();
                streamToZip.Close();
                GC.Collect();
            }
    
            /// <summary>  
            /// 压缩目录(包括子目录及所有文件)  
            /// </summary>  
            /// <param name="rootPath">要压缩的根目录</param>  
            /// <param name="destinationPath">保存路径</param>  
            /// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>  
            public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)
            {
                GetAllDirectories(rootPath);
    
                /* while (rootPath.LastIndexOf("\") + 1 == rootPath.Length)//检查路径是否以""结尾 
                { 
     
                rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"" 
     
                } 
                */
                //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。  
                string rootMark = rootPath + "\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。  
                Crc32 crc = new Crc32();
                ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));
                outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression  
                foreach (string file in files)
                {
                    FileStream fileStream = File.OpenRead(file);//打开压缩文件  
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
                    entry.DateTime = DateTime.Now;
     
                    entry.Size = fileStream.Length;
                    fileStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    outPutStream.PutNextEntry(entry);
                    outPutStream.Write(buffer, 0, buffer.Length);
                }
    
                this.files.Clear();
    
                foreach (string emptyPath in paths)
                {
                    ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
                    outPutStream.PutNextEntry(entry);
                }
    
                this.paths.Clear();
                outPutStream.Finish();
                outPutStream.Close();
                GC.Collect();
            }
    
            /// <summary>  
            /// 取得目录下所有文件及文件夹,分别存入files及paths  
            /// </summary>  
            /// <param name="rootPath">根目录</param>  
            private void GetAllDirectories(string rootPath)
            {
                string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录  
                foreach (string path in subPaths)
                {
                    GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List  
                }
                string[] files = Directory.GetFiles(rootPath);
                foreach (string file in files)
                {
                    this.files.Add(file);//将当前目录中的所有文件全名存入文件List  
                }
                if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录  
                {
                    this.paths.Add(rootPath);//记录空目录  
                }
            }
    
            /// <summary>  
            /// 解压缩文件(压缩文件中含有子目录)  
            /// </summary>  
            /// <param name="zipfilepath">待解压缩的文件路径</param>  
            /// <param name="unzippath">解压缩到指定目录</param>  
            /// <returns>解压后的文件列表</returns>  
            public List<string> UnZip(string zipfilepath, string unzippath)
            {
                //解压出来的文件列表  
                List<string> unzipFiles = new List<string>();
    
                //检查输出目录是否以“\”结尾  
                if (unzippath.EndsWith("\") == false || unzippath.EndsWith(":\") == false)
                {
                    unzippath += "\";
                }
    
                ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(unzippath);
                    string fileName = Path.GetFileName(theEntry.Name);
    
                    //生成解压目录【用户解压到硬盘根目录时,不需要创建】  
                    if (!string.IsNullOrEmpty(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }
    
                    if (fileName != String.Empty)
                    {
                        //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入  
                        if (theEntry.CompressedSize == 0)
                            continue;
                        //解压文件到指定的目录  
                        directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);
                        //建立下面的目录和子目录  
                        Directory.CreateDirectory(directoryName);
    
                        //记录导出的文件  
                        unzipFiles.Add(unzippath + theEntry.Name);
    
                        FileStream streamWriter = File.Create(unzippath + 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();
                GC.Collect();
                return unzipFiles;
            }
    
            public string GetZipFileExtention(string fileFullName)
            {
                int index = fileFullName.LastIndexOf(".");
                if (index <= 0)
                {
                    throw new Exception("The source package file is not a compress file");
                }
    
                //extension string
                string ext = fileFullName.Substring(index);
    
                if (ext == ".rar" || ext == ".zip")
                {
                    return ext;
                }
                else
                {
                    throw new Exception("The source package file is not a compress file");
                }
            }
        }

       上述代码便是压缩文件的方法,我的测试结果如下:

       

       这是项目源文件,我要把:高效程序员的45个习惯压缩.pdf 文件进行压缩,由图可知,确实压缩成了:高效程序员的45个习惯压缩.zip 但是当你尝试去解压这个.zip文件后,你会发现解压后得到的文件夹中包含N+1层文件夹,最后的一层文件夹中能够找到我压缩的pdf文件,这点匪夷所思,有兴趣的小虎斑可以把你们的见解贴在下面的评论上,供大家参考,同时也为LZ提供个思路,谢谢!

       我示例的代码如下:

      public ActionResult Index()
            {
                C2Global.Architect.Common.ZipUtility zip = new C2Global.Architect.Common.ZipUtility();
                zip.ZipFile(Server.MapPath("~/file/高效程序员的45个习惯.pdf"), Server.MapPath("~/file/高效程序员的45个习惯.zip"), 5, 10);
                return View();
            }

       程序声明:本段程序需要引用一个dll文件,这个dll文件的全名叫做:ICSharpCode.SharpZipLib.dll 小虎斑们可自行下载

       

       大家也可尝试用这个链接进行下载:http://files.cnblogs.com/files/chenwolong/ICSharpCode.SharpZipLib.zip  这是我上传至博客园的,应该可以下载,支持Net4.5框架

       关于:解压后得到的文件夹中包含N+1层文件夹的问题,欢迎大家指正,讨论,谢谢!

    2019年7月17日新增加一个方法如下:

    2019年7月17日19.56分   

    场景:压缩同一文件夹下的某几个文件:

            /// <summary>
            /// 压缩目录下的多个文件
            /// </summary>
            /// <param name="rootPath"></param>
            /// <param name="rootPath"></param>
            /// <param name="destinationPath"></param>
            /// <param name="compressLevel"></param>
            public void ZipDirectoryFiles(string rootMark, List<SharpZipLibFileModel> Files, string destinationPath, int compressLevel)
            {
                Crc32 crc = new Crc32();
                ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));
                outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression  
                foreach (var file in Files)
                {
                    FileStream fileStream = File.OpenRead(file.SharpZipLibFilePath);//打开压缩文件  
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    //构造函数中的文件名为新的文件名
                    ZipEntry entry = new ZipEntry(file.SharpZipLibFileName);
                    entry.DateTime = DateTime.Now;
    
                    entry.Size = fileStream.Length;
                    fileStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    outPutStream.PutNextEntry(entry);
                    outPutStream.Write(buffer, 0, buffer.Length);
                }
    
                this.files.Clear();
    
                foreach (string emptyPath in paths)
                {
                    ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
                    outPutStream.PutNextEntry(entry);
                }
    
                this.paths.Clear();
                outPutStream.Finish();
                outPutStream.Close();
                GC.Collect();
            }
    View Code

    //调用如下:

     /// <summary>
            /// 全部下载招标文件附件
            /// </summary>
            /// <param name="flowId"></param>
            /// <param name="compressFilePath">打包压缩后的文件路径 用于下载完成后删除</param>
            public static void downLoadTenderDocs(string flowId,string RootSource,out string
    compressfilePath,out string destinationfileName)
            {
                string sql = @"select *from CF_Document 
                                    where Refrecordid in
                                    (select CAST(Tenderdocumentid as varchar(50)) 
                                    from TMS_Tenderdocument 
                                    where Tenderflowid='" + flowId + "' and ISNULL  (MT_Coreinfo,'')!='')";
                var ds = DataAccessCommon.SelectDataSet(sql);
                var lst = JsonHelper.DataSetToIList<DocumentModel>(ds, 0);
                List<SharpZipLibFileModel> Files = new List<SharpZipLibFileModel>();
                foreach (var item in lst)
                {
                    string FilePath = RootSource + item.Url;
                    if (File.Exists(FilePath))
                    {
                        SharpZipLibFileModel M = new SharpZipLibFileModel();
                        M.SharpZipLibFilePath = FilePath;
                        M.SharpZipLibFileName = item.Documentname;
                        Files.Add(M);
    
                    }
                }
                destinationfileName = DateTime.Now.ToString("yyyyMMddhhmmssfff").ToString() + "招标文件.zip";
                compressfilePath = RootSource + "/Uploads/" + destinationfileName;
                string path = RootSource + "/Uploads/";
                SharpZipLib zipLib = new SharpZipLib();
                zipLib.ZipDirectoryFiles(path, Files, compressfilePath, 9);
    
            }
    View Code

    //调用如下:

            /// <summary>
            /// 下载所有招标哦文件
            /// </summary>
            /// <param name="flowId"></param>
            /// <param name="DownloadCate"></param>
            /// <returns></returns>
            public ActionResult DownloadTenderDocs(string flowId)
            {
                ViewBag.Title = "附件下载";
                string _compressfilePath;
                string _destinationfileName;
                string path = Server.MapPath("/");
                //完成打包压缩
                TenderDiscussingService.downLoadTenderDocs(flowId,path,out _compressfilePath,out _destinationfileName);
                if (System.IO.File.Exists(_compressfilePath))
                {
                    return new downLoadFileResult(_compressfilePath, _destinationfileName);
                }
                else
                {
                    ViewBag.Title = "未找到相关附件";
                    ViewBag.files = "None";
                    return View();
                }
            }
    View Code

    关于下载方法,请阅读大附件上传:webuploader-异步切片上传(支持断点续传)及 下载方法!C#/.NET

    一个小的Model如下:

        public class SharpZipLibFileModel
        {
            public string SharpZipLibFilePath { get; set; }
            public string SharpZipLibFileName { get; set; }
        }
    View Code

    2019年7月17日20.33分

    如果需要将某几个文件移动到另外一个文件夹,需要用到 File.Copy(SourcePth, DestPth, true);方法,如下:

            public static void GetDocuments(string path,out string DirectoryPath,out string DestinationPath)
            {
                using (ShiMaoZhaoCaiEntities context = new ShiMaoZhaoCaiEntities())
                {
                    string sql = @"select *from CF_Document 
                                    where Refrecordid in
                                    (select CAST(Tenderdocumentid as varchar(50)) 
                                    from TMS_Tenderdocument 
                                    where Tenderflowid='AC273672-ED2C-4F5D-9731-45C96EE39238' and ISNULL  (MT_Coreinfo,'')!='')";
                    var lst = context.Database.SqlQuery<CF_Document>(sql).ToList();
                    string NewFilePaht = path+ "Uploads/";
                    CombineFile.CombineFiles(ref NewFilePaht);
                    DirectoryPath = NewFilePaht;
    
                    foreach (var item in lst)
                    {
                        if (File.Exists(path + item.Url))
                        {
                            string SourcePth = path + item.Url;
                            string DestPth = NewFilePaht + "/" + item.Documentname;
                            File.Copy(SourcePth, DestPth, true);
                        }
                    }
                    string destinationPath = NewFilePaht + "/" + Guid.NewGuid().ToString()+".zip" ;
                    DestinationPath = destinationPath;
                    SharpZipLib zipLib = new SharpZipLib();
                    zipLib.ZipFileFromDirectory(NewFilePaht, destinationPath, 9);
    
                }
            }
    View Code

    调用如下:

     public ActionResult Index()
            {
                string path=Server.MapPath("/");
                string DirectoryPath;
                string DestinationPath;
                DocumentService.GetDocuments(path,out DirectoryPath,out DestinationPath);
    }
    View Code

    辅助方法:

        public class CombineFile
        {
            public static void CombineFiles(ref string filePath)
            {
                string ChildFilePath = Guid.NewGuid().ToString() ;
                filePath = filePath+ ChildFilePath;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
            }
        }
    View Code

    数据库截图如下:

    @陈卧龙的博客

  • 相关阅读:
    随笔35 内联函数
    随笔32 内部类,外部类,局部内部类
    随笔31 Spring的依赖注入的三种方式
    随笔30 抽象类与接口
    随笔29 Statement对象
    随笔28 Spring中的事务
    随笔27 面向对象的五大基本原则
    随笔26 java中的泛型
    html5学习笔记——HTML5 web存储
    html5学习笔记——HTML 5 视频
  • 原文地址:https://www.cnblogs.com/chenwolong/p/zip.html
Copyright © 2011-2022 走看看