码迷,mamicode.com
首页 > 其他好文 > 详细

解压缩

时间:2014-09-23 19:55:15      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   文件   div   

.zip解压缩:

    /// <summary>   
        /// 功能:解压zip格式的文件。   
        /// </summary>   
        /// <param >压缩文件路径</param>   
        /// <param >解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>   
        /// <param >出错信息</param>   
        /// <returns>解压是否成功</returns>   
        public bool UnZipFile(string path, string filename, out string err)
        {
            string zipFilePath = path + filename;
            string unZipDir = path + filename.Substring(0, filename.LastIndexOf("."));
            err = "";
            if (zipFilePath == string.Empty)
            {
                err = "压缩文件不能为空!";
                return false;
            }
            if (!File.Exists(zipFilePath))
            {
                err = "压缩文件不存在!";
                return false;
            }
            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹   
            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            if (!unZipDir.EndsWith("\\"))
                unZipDir += "\\";
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {

                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (!directoryName.EndsWith("\\"))
                            directoryName += "\\";
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(unZipDir + 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;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }

.rar解压缩:

        /// <summary>
        /// 解压rar
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string unCompressRAR(string path, string filename)
        {
            string rarPatch = path + filename;
            string unRarPatch = path + filename.Substring(0, filename.LastIndexOf(".")) + "\\";
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();

                if (Directory.Exists(unRarPatch) == false)
                {
                    Directory.CreateDirectory(unRarPatch);
                }

                the_Info = "x " + rarPatch + " " + unRarPatch + " -y";

                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径

                Process the_Process = new Process();

                the_Process.StartInfo = the_StartInfo;

                the_Process.Start();

                the_Process.WaitForExit();

                the_Process.Close();

            }

            catch (Exception ex)
            {

                throw ex;

            }

            return unRarPatch;

        }

其中前一种是不能解压.rar的,后一种是调用WinRar软件来做的,需要电脑装那软件才行

解压缩

标签:style   blog   color   io   os   ar   for   文件   div   

原文地址:http://www.cnblogs.com/xzshare/p/3988834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!