C#封装的文件压缩和解压缩类

C#封装的文件压缩和解压缩类,第1张

概述C#封装的文件压缩和解压缩类

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

using System;using System.IO;using System.Diagnostics;using Microsoft.Win32; using ICSharpCode.SharpZiplib.Checksums;using ICSharpCode.SharpZiplib.Zip; ///压缩、解压缩类namespace DotNet.UtilitIEs{    public class SharpZip    {        public SharpZip()        { }         /// <summary>        /// 压缩        /// </summary>        /// <param name="filename"> 压缩后的文件名(包含物理路径)</param>        /// <param name="directory">待压缩的文件夹(包含物理路径)</param>        public static voID Packfiles(string filename,string directory)        {            try            {                FastZip fz = new FastZip();                fz.CreateEmptyDirectorIEs = true;                fz.CreateZip(filename,directory,true,"");                fz = null;            }            catch (Exception)            {                throw;            }        }         /// <summary>        /// 解压缩        /// </summary>        /// <param name="file">待解压文件名(包含物理路径)</param>        /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param>        public static bool Unpackfiles(string file,string dir)        {            try            {                if (!Directory.Exists(dir))                {                    Directory.CreateDirectory(dir);                }                ZipinputStream s = new ZipinputStream(file.OpenRead(file));                ZipEntry theEntry;                while ((theEntry = s.GetNextEntry()) != null)                {                    string directoryname = Path.GetDirectoryname(theEntry.name);                    string filename = Path.Getfilename(theEntry.name);                    if (directoryname != String.Empty)                    {                        Directory.CreateDirectory(dir + directoryname);                    }                    if (filename != String.Empty)                    {                        fileStream streamWriter = file.Create(dir + theEntry.name);                        int size = 2048;                        byte[] data = new byte[2048];                        while (true)                        {                            size = s.Read(data,data.Length);                            if (size > 0)                            {                                streamWriter.Write(data,size);                            }                            else                            {                                break;                            }                        }                        streamWriter.Close();                    }                }                s.Close();                return true;            }            catch (Exception)            {                throw;            }        }    }     public class ClassZip    {        #region 私有方法        /// <summary>        /// 递归压缩文件夹方法        /// </summary>        private static bool ZipfileDictory(string FolderToZip,ZipOutputStream s,string ParentFoldername)        {            bool res = true;            string[] folders,filenames;            ZipEntry entry = null;            fileStream fs = null;            Crc32 crc = new Crc32();            try            {                entry = new ZipEntry(Path.Combine(ParentFoldername,Path.Getfilename(FolderToZip) + "/"));                s.PutNextEntry(entry);                s.Flush();                filenames = Directory.Getfiles(FolderToZip);                foreach (string file in filenames)                {                    fs = file.OpenRead(file);                    byte[] buffer = new byte[fs.Length];                    fs.Read(buffer,buffer.Length);                    entry = new ZipEntry(Path.Combine(ParentFoldername,Path.Getfilename(FolderToZip) + "/" + Path.Getfilename(file)));                    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,buffer.Length);                }            }            catch            {                res = false;            }            finally            {                if (fs != null)                {                    fs.Close();                    fs = null;                }                if (entry != null)                {                    entry = null;                }                GC.Collect();                GC.Collect(1);            }            folders = Directory.GetDirectorIEs(FolderToZip);            foreach (string folder in folders)            {                if (!ZipfileDictory(folder,s,Path.Combine(ParentFoldername,Path.Getfilename(FolderToZip))))                {                    return false;                }            }            return res;        }         /// <summary>        /// 压缩目录        /// </summary>        /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>        /// <param name="Zipedfile">压缩后的文件名,全路径格式</param>        private static bool ZipfileDictory(string FolderToZip,string Zipedfile,int level)        {            bool res;            if (!Directory.Exists(FolderToZip))            {                return false;            }            ZipOutputStream s = new ZipOutputStream(file.Create(Zipedfile));            s.SetLevel(level);            res = ZipfileDictory(FolderToZip,"");            s.Finish();            s.Close();            return res;        }         /// <summary>        /// 压缩文件        /// </summary>        /// <param name="fileToZip">要进行压缩的文件名</param>        /// <param name="Zipedfile">压缩后生成的压缩文件名</param>        private static bool Zipfile(string fileToZip,int level)        {            if (!file.Exists(fileToZip))            {                throw new System.IO.fileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");            }            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,buffer.Length);                Zipfile.Close();                 Zipfile = file.Create(Zipedfile);                ZipStream = new ZipOutputStream(Zipfile);                ZipEntry = new ZipEntry(Path.Getfilename(fileToZip));                ZipStream.PutNextEntry(ZipEntry);                ZipStream.SetLevel(level);                 ZipStream.Write(buffer,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;        }        #endregion         /// <summary>        /// 压缩        /// </summary>        /// <param name="fileToZip">待压缩的文件目录</param>        /// <param name="Zipedfile">生成的目标文件</param>        /// <param name="level">6</param>        public static bool Zip(String fileToZip,String Zipedfile,int level)        {            if (Directory.Exists(fileToZip))            {                return ZipfileDictory(fileToZip,Zipedfile,level);            }            else if (file.Exists(fileToZip))            {                return Zipfile(fileToZip,level);            }            else            {                return false;            }        }         /// <summary>        /// 解压        /// </summary>        /// <param name="fileToUpZip">待解压的文件</param>        /// <param name="ZipedFolder">解压目标存放目录</param>        public static voID UnZip(string fileToUpZip,string ZipedFolder)        {            if (!file.Exists(fileToUpZip))            {                return;            }            if (!Directory.Exists(ZipedFolder))            {                Directory.CreateDirectory(ZipedFolder);            }            ZipinputStream s = null;            ZipEntry theEntry = null;            string filename;            fileStream streamWriter = null;            try            {                s = new ZipinputStream(file.OpenRead(fileToUpZip));                while ((theEntry = s.GetNextEntry()) != null)                {                    if (theEntry.name != String.Empty)                    {                        filename = Path.Combine(ZipedFolder,theEntry.name);                        if (filename.EndsWith("/") || filename.EndsWith("\"))                        {                            Directory.CreateDirectory(filename);                            continue;                        }                        streamWriter = file.Create(filename);                        int size = 2048;                        byte[] data = new byte[2048];                        while (true)                        {                            size = s.Read(data,size);                            }                            else                            {                                break;                            }                        }                    }                }            }            finally            {                if (streamWriter != null)                {                    streamWriter.Close();                    streamWriter = null;                }                if (theEntry != null)                {                    theEntry = null;                }                if (s != null)                {                    s.Close();                    s = null;                }                GC.Collect();                GC.Collect(1);            }        }    }     public class ZipHelper    {        #region 私有变量        String the_rar;        RegistryKey the_Reg;        Object the_Obj;        String the_Info;        processstartinfo the_StartInfo;        Process the_Process;        #endregion         /// <summary>        /// 压缩        /// </summary>        /// <param name="zipname">要解压的文件名</param>        /// <param name="zippath">要压缩的文件目录</param>        /// <param name="dirpath">初始目录</param>        public voID EnZip(string zipname,string zippath,string dirpath)        {            try            {                the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");                the_Obj = the_Reg.GetValue("");                the_rar = the_Obj.ToString();                the_Reg.Close();                the_rar = the_rar.Substring(1,the_rar.Length - 7);                the_Info = " a    " + zipname + "  " + zippath;                the_StartInfo = new processstartinfo();                the_StartInfo.filename = the_rar;                the_StartInfo.Arguments = the_Info;                the_StartInfo.windowstyle = Processwindowstyle.HIDden;                the_StartInfo.WorkingDirectory = dirpath;                the_Process = new Process();                the_Process.StartInfo = the_StartInfo;                the_Process.Start();            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }         /// <summary>        /// 解压缩        /// </summary>        /// <param name="zipname">要解压的文件名</param>        /// <param name="zippath">要解压的文件路径</param>        public voID DeZip(string zipname,string zippath)        {            try            {                the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");                the_Obj = the_Reg.GetValue("");                the_rar = the_Obj.ToString();                the_Reg.Close();                the_rar = the_rar.Substring(1,the_rar.Length - 7);                the_Info = " X " + zipname + " " + zippath;                the_StartInfo = new processstartinfo();                the_StartInfo.filename = the_rar;                the_StartInfo.Arguments = the_Info;                the_StartInfo.windowstyle = Processwindowstyle.HIDden;                the_Process = new Process();                the_Process.StartInfo = the_StartInfo;                the_Process.Start();            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }    }}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C#封装的文件压缩和解压缩类全部内容,希望文章能够帮你解决C#封装的文件压缩和解压缩类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1238372.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存