c\#압축 해제 파일 의 인 스 턴 스 방법


#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

좋은 웹페이지 즐겨찾기