首页 > 代码库 > 解压缩

解压缩

.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 = http://www.mamicode.com/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软件来做的,需要电脑装那软件才行

解压缩