asp.net使用winrar.exe解压、压缩文件

  • asp.net使用winrar.exe解压、压缩文件已关闭评论
  • 245 views
  • A+
所属分类:C#.NET 编程技术
【腾讯云】11.11 云上盛惠,云产品限时抢购,1核2G云服务器首年88元

注意:系统环境必须含有winrar.exe

安装完后请打开电脑注册表,在注册表基项(HKEY_LOCAL_MACHINE)查看winrar调用地址

 

asp.net使用winrar.exe解压、压缩文件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
///
/// 压缩文件
///需要压缩的文件夹或者单个文件,生成压缩文件的文件名,生成压缩文件保存路径
///
    public static bool RAR(string DFilePath, string DRARName, string DRARPath)
    {
        String the_rar;
        RegistryKey the_Reg;
        Object the_Obj;
        String the_Info;
        ProcessStartInfo the_StartInfo;
        Process the_Process;
        try
        {
            //if (DFilePath.EndsWith("\"))
            //{
            //    DFilePath = DFilePath.Substring(0, DFilePath.Length - 1);
            //}
            DFilePath = Path.GetFullPath(DFilePath);
            DRARPath = Path.GetFullPath(DRARPath);
            //在注册表基项(HKEY_CLASSES_ROOT)匹配WINRAR的注册表路径
            the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
            bool isFirst = true;
            if (the_Reg == null)
            {
                isFirst = false;
                //在注册表基项(HKEY_LOCAL_MACHINE)匹配WINRAR的注册表路径
                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 (isFirst == true)
            {
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
            }
            the_Info = " a " + " " + DRARName + " " + DFilePath + "  -ep1"; //命令 + 压缩后文件名 + 被压缩的文件或者路径
            the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = the_rar;
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。
            the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            long m = getDirectorySize(DFilePath) / 4000;
            System.Threading.Thread.Sleep(Convert.ToInt32(m == 0 ? 500 : m + 200));
            bool bln = false;
            if (the_Process.HasExited)
                if (the_Process.ExitCode == 0)
                    bln = true;
            the_Process.Close();
            //the_Process.Kill();
            //删除.zip文件
            if (!bln)
            {
                DeleteFile(DRARPath + DRARName);
            }
            return bln;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
///
/// 解压缩到指定文件夹
///
///压缩文件存在的目录 ///压缩文件名称 ///解压到文件夹 ///
    public static bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)
    {
        //解压缩
        String the_rar;
        RegistryKey the_Reg;
        Object the_Obj;
        String the_Info;
        ProcessStartInfo the_StartInfo;
        Process the_Process;
        try
        {
            bool flag = true;
            //在注册表基项(HKEY_CLASSES_ROOT)匹配WINRAR的注册表路径
            the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
            if (the_Reg == null)
            {
                //在注册表基项(HKEY_LOCAL_MACHINE)匹配WINRAR的注册表路径
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                flag = false;
            }
            the_Obj = the_Reg.GetValue("");
            the_rar = the_Obj.ToString();
            the_Reg.Close();
            if (flag)
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
            //the_Info = @"x -o-" + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
            the_Info = @"x " + RARFilePath + RARFileName + " " + UnRARFilePath + "\\ -y";//+ RARFileName.Substring(0, RARFileName.LastIndexOf("."))
            the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = the_rar;
            the_StartInfo.Arguments = the_Info;
            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            the_Process.WaitForExit();
            long m = getFileSize(RARFilePath + RARFileName) / 5000;
            System.Threading.Thread.Sleep(Convert.ToInt32(m == 0 ? 500 : m + 200));
            bool bln = false;
            if (the_Process.HasExited)
                if (the_Process.ExitCode == 0)
                    bln = true;
            the_Process.Close();
            //删除.zip文件
            if (!bln)
            {
                DeleteFile(RARFilePath + RARFileName);
            }
            return bln;
        }
        catch (Exception ex)
        {
            //删除.zip文件
            DeleteFile(RARFilePath + RARFileName);
            return false;
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
///
/// 根据文件路径删除
///
    public static bool DeleteFile(string fPath)
    {
        if (File.Exists(fPath))
        {
            FileInfo fi = new FileInfo(fPath);
            fi.Attributes = FileAttributes.Normal;
            //删除设置时间控制
            long m = fi.Length / 100000;
            fi.Delete();
            System.Threading.Thread.Sleep(Convert.ToInt32(m == 0 ? 500 : m));
        }
        return true;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
///
/// 创建目录
///
    public static void CreateDir(string path)
    {
        //建立目录
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        else
        {
            //设置文件夹为可读
            DirectoryInfo tempDir = new DirectoryInfo(path);
            tempDir.Attributes = FileAttributes.Normal;
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
///
/// 删除目录下的所以文件及目录
///
    public static void DeleteDir(string path)
    {
        if (! Directory.Exists(path))
        {
            return;
        }
        //获取到目录
        DirectoryInfo dirinfo = new DirectoryInfo(path);
        //获取目录下的文件
        FileInfo[] fi = dirinfo.GetFiles();
        //获取目录下的目录
        DirectoryInfo[] dir = dirinfo.GetDirectories();
        foreach (FileInfo fii in fi)
        {
            fii.Attributes = FileAttributes.Normal;
            //删除设置时间控制
            long m = fi.Length / 100000;
            fii.Delete();
            System.Threading.Thread.Sleep(Convert.ToInt32(m == 0 ? 500 : m));
        }
        foreach (DirectoryInfo dirr in dir)
        {
            dirr.Attributes = FileAttributes.Normal;
            DeleteDir(dirr.FullName);
        }
        dirinfo.Delete(true);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
///
/// 根据目录路径获取某个文件夹的大小
///
    public static long getDirectorySize(string path)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(path);
        long sumSize = 0;
        foreach (FileSystemInfo fsInfo in dirInfo.GetFileSystemInfos())
        {
            if (fsInfo.Attributes.ToString().ToLower() == "directory")
            {
                sumSize += getDirectorySize(fsInfo.FullName);
            }
            else
            {
                FileInfo fiInfo = new FileInfo(fsInfo.FullName);
                sumSize += fiInfo.Length;
            }
        }
        return sumSize;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
///
/// 根据目录路径获取某个文件的大小
///
    public static long getFileSize(string path)
    {
        long sumSize = 0;
        if (File.Exists(path))
        {
            FileInfo fiInfo = new FileInfo(path);
            sumSize = fiInfo.Length;
        }
        return sumSize;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
///
/// 获取某个文件夹的所有文件和文件夹
///
    public static void GetDirectoryFilesORDirs(string path, ref List<string> files, ref List<string> dirs)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(path);
        foreach (FileSystemInfo fsInfo in dirInfo.GetFileSystemInfos())
        {
            if (fsInfo.Attributes.ToString().ToLower() == "directory")
            {
                dirs.Add(fsInfo.FullName);
                GetDirectoryFilesORDirs(fsInfo.FullName, ref files, ref dirs);
            }
            else
            {
                files.Add(fsInfo.FullName);
            }
        }
    }

文末附源码下载

文件下载
下载密码:发表评论并刷新可见!
下载地址

文件下载

【腾讯云】618云聚惠,百款云产品限量抢购,1核2G云服务器首年95元!
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin