欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#解压文件的实例方法

程序员文章站 2023-12-18 23:57:34
复制代码 代码如下:#region 解压 文件 zip 格式 rar 格式        /// &l...
复制代码 代码如下:

#region 解压 文件 zip 格式 rar 格式
        /// <summary>
        ///解压文件
        /// </summary>
        /// <param name="filefromunzip">解压前的文件路径(绝对路径)</param>
        /// <param name="filetounzip">解压后的文件目录(绝对路径)</param>
        public static void unpackfile(string filefromunzip, string filetounzip)
        {
            //获取压缩类型
            string untype = filefromunzip.substring(filefromunzip.lastindexof(".") + 1, 3).tolower();
            switch (untype)
            {
                case "rar":
                    unrar(filefromunzip, filetounzip);
                    break;
                case "zip":
                    unzip(filefromunzip, filetounzip);
                    break;
            }
        }
        //解压rar格式的文件
        private static void unrar(string filefromunzip, string filetounzip)
        {
            using (process process1 = new process())// 开启一个进程 执行解压工作
            {
          string serverdir = configurationmanager.appsettings["unpackfile"].tostring();//rar工具的安装路径   必须要安装 winrar     //例于:c:\program files (x86)\winrar\rar.exe
                process1.startinfo.useshellexecute = false;
                process1.startinfo.redirectstandardinput = true;
                process1.startinfo.redirectstandardoutput = true;
                process1.startinfo.redirectstandarderror = true;
                process1.startinfo.createnowindow = true;
                process1.startinfo.filename = serverdir;
                process1.startinfo.arguments = " x -inul -y " + filefromunzip + " " + filetounzip;
                process1.start();//解压开始 
                process1.waitforexit();
                process1.close();
            }
        }
        // 解压zip 文件
        public static void unzip(string filefromunzip, string filetounzip)
        {
            zipinputstream inputstream = new zipinputstream(file.openread(filefromunzip));
            zipentry theentry;
            while ((theentry = inputstream.getnextentry()) != null)
            {
                filetounzip += "/";
                string filename = path.getfilename(theentry.name);
                string path = path.getdirectoryname(filetounzip) + "/";
                // directory.createdirectory(path);//生成解压目录
                if (filename != string.empty)
                {
                    filestream streamwriter = file.create(path + filename);//解压文件到指定的目录
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = inputstream.read(data, 0, data.length);
                        if (size > 0)
                        {
                            streamwriter.write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamwriter.close();
                }
            }
            inputstream.close();
        }
        #endregion

上一篇:

下一篇: