关闭

(C#)文件操作--解压文件夹和文件

241人阅读 评论(0) 收藏 举报

using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;

namespace DataReportedFeedBack
{
    public class UnZipClass
    {
 

        public void UnZip(string[] args)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(args[1]);
                string fileName = Path.GetFileName(theEntry.Name);

                //生成解压目录
                // create directory
                if (directoryName != "")
                {
                    Directory.CreateDirectory(directoryName);
                }
                if (fileName != String.Empty)
                {
                    //if (theEntry.Name.IndexOf(".ini") < 0)
                    //{
                    string fullPath = directoryName + "//" + theEntry.Name;
                    fullPath = fullPath.Replace("// ", "//");
                    string fullDirPath = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                    FileStream streamWriter = File.Create(fullPath);
                    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;
                        }
                    }
                    streamWriter.Close();
                    //}
                }
            }
            s.Close();
         }
    }
}

 

调用:

private void button3_Click(object sender, EventArgs e)
   {                         

                          string[] FileProperties = new string[2];

                           待解压的文件

                            FileProperties[0] = strToBak1Directory + "//" + f.Name;

                           解压后放置的目标目录
                            FileProperties[1] = strTempDiretory+"//";    

                            UnZipClass UnZc = new UnZipClass();
                            UnZc.UnZip(FileProperties);

   }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12367次
    • 积分:218
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条