首页 新闻 会员 周边

c# 文件压缩

0
悬赏园豆:10 [已解决问题] 解决于 2013-12-06 15:44

       以下代码是我在网上看到的一篇文件压缩、解压代码的 

        我试了一下  文件压缩

         我想压缩 E:\xx\xx\xx\C110_Temp\C110中的  C110文件夹到  E:\xx\xx\xx\C110_Temp\文件夹下,

         其结果是压缩是实现了!

         但我从打开压缩包文件C110。rar 一看:发现里面并没有C110 这个文件夹而是 

         出项了 C110\xx\xx\xx\C110_Temp\C110这样的文件结构,它把前面的文件路径全压缩进来了!

    问题点:我只要C110这个文件压缩,不带前面的父级文件,需要怎么来处理????

 

 

 

C# 调用WinRar解压与压缩文件或文件夹。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace JaveSystem
{
    public class WinRar
    {
        static WinRar()
        {
            //判断是否安装了WinRar.exe
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            _existSetupWinRar = !string.IsNullOrEmpty(key.GetValue(string.Empty).ToString());

            //获取WinRar.exe路径
            _winRarPath = key.GetValue(string.Empty).ToString();
        }

        static bool _existSetupWinRar;
        /// <summary>
        /// 获取是否安装了WinRar的标识
        /// </summary>
        public static bool ExistSetupWinRar
        {
            get { return _existSetupWinRar; }
        }

        static string _winRarPath;
        /// <summary>
        /// 获取WinRar.exe路径
        /// </summary>
        public static string WinRarPath
        {
            get { return _winRarPath; }
        }

        /// <summary>
        /// 压缩到.rar
        /// </summary>
        /// <param name="intputPath">输入目录</param>
        /// <param name="outputPath">输出目录</param>
        /// <param name="outputFileName">输出文件名</param>
        public static void CompressRar(string intputPath, string outputPath, string outputFileName)
        {
            //rar 执行时的命令、参数
            string rarCmd;
            //启动进程的参数
            ProcessStartInfo processStartInfo;
            //进程对象
            Process process;
            try
            {
                if (!ExistSetupWinRar)
                {
                    throw new ArgumentException("Not setuping the winRar, you can Compress.make sure setuped winRar.");
                }
                //判断输入目录是否存在
                if (!Directory.Exists(intputPath))
                {
                    throw new ArgumentException("CompressRar'arge : inputPath isn't exsit.");
                }
                //命令参数
                rarCmd = " a " + outputFileName + " " + intputPath + " -r";
                //创建启动进程的参数
                processStartInfo = new ProcessStartInfo();
                //指定启动文件名
                processStartInfo.FileName = WinRarPath;
                //指定启动该文件时的命令、参数
                processStartInfo.Arguments = rarCmd;
                //指定启动窗口模式:隐藏
                processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //指定压缩后到达路径
                processStartInfo.WorkingDirectory = outputPath;
                //创建进程对象
                process = new Process();
                //指定进程对象启动信息对象
                process.StartInfo = processStartInfo;
                //启动进程
                process.Start();
                //指定进程自行退行为止
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 解压.rar
        /// </summary>
        /// <param name="inputRarFileName">输入.rar</param>
        /// <param name="outputPath">输出目录</param>
        public static void UnCompressRar(string inputRarFileName, string outputPath)
        {
            //rar 执行时的命令、参数
            string rarCmd;
            //启动进程的参数
            ProcessStartInfo processStartInfo;
            //进程对象
            Process process;
            try
            {
                if (!ExistSetupWinRar)
                {
                    throw new ArgumentException("Not setuping the winRar, you can UnCompress.make sure setuped winRar.");
                }
                //如果压缩到目标路径不存在
                if (!Directory.Exists(outputPath))
                {
                    //创建压缩到目标路径
                    Directory.CreateDirectory(outputPath);
                }
                rarCmd = "x " + inputRarFileName + " " + outputPath + " -y";

                processStartInfo = new ProcessStartInfo();
                processStartInfo.FileName = WinRarPath;
                processStartInfo.Arguments = rarCmd;
                processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                processStartInfo.WorkingDirectory = outputPath;

                process = new Process();
                process.StartInfo = processStartInfo;
                process.Start();
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

RAR参数:

一、压缩命令
1、将temp.txt压缩为temp.rarrar a temp.rar temp.txt 
2、将当前目录下所有文件压缩到temp.rarrar a temp.rar *.* 
3、将当前目录下所有文件及其所有子目录压缩到temp.rarrar a temp.rar *.* -r 
4、将当前目录下所有文件及其所有子目录压缩到temp.rar,并加上密码123rar a temp.rar *.* -r -p123

二、解压命令
1、将temp.rar解压到c:\temp目录rar e temp.rar c:\temprar e *.rar c:\temp(支持批量操作) 
2、将temp.rar解压到c:\temp目录,并且解压后的目录结构和temp.rar中的目录结构一


压缩目录test及其子目录的文件内容 
Wzzip test.zip test -r -P 
WINRAR A test.rar test -r 

删除压缩包中的*.txt文件 
Wzzip test.zip *.txt -d 
WinRAR d test.rar *.txt 


刷新压缩包中的文件,即添加已经存在于压缩包中但更新的文件 
Wzzip test.zip test -f 
Winrar f test.rar test 


更新压缩包中的文件,即添加已经存在于压缩包中但更新的文件以及新文件 
Wzzip test.zip test -u 
Winrar u test.rar test 


移动文件到压缩包,即添加文件到压缩包后再删除被压缩的文件 
Wzzip test.zip -r -P -m 
Winrar m test.rar test -r 


添加全部 *.exe 文件到压缩文件,但排除有 a或b 
开头名称的文件 
Wzzip test *.exe -xf*.* -xb*.* 
WinRAR a test *.exe -xf*.* -xb*.* 


加密码进行压缩 
Wzzip test.zip test 
-s123。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到+号标记(附图1)。 
WINRAR A test.rar test -p123 
-r。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到*号标记(附图2)。 


按名字排序、以简要方式列表显示压缩包文件 
Wzzip test.zip -vbn 
Rar l test.rar 


锁定压缩包,即防止未来对压缩包的任何修改 
无对应命令 
Winrar k test.rar 


创建360kb大小的分卷压缩包 
无对应命令 
Winrar a -v360 test 


带子目录信息解压缩文件 
Wzunzip test -d 
Winrar x test -r 


不带子目录信息解压缩文件 
Wzunzip test 
Winrar e test 


解压缩文件到指定目录,如果目录不存在,自动创建 
Wzunzip test newfolder 
Winrar x test newfolder 


解压缩文件并确认覆盖文件 
Wzunzip test -y 
Winrar x test -y 


解压缩特定文件 
Wzunzip test *.txt 
Winrar x test *.txt 


解压缩现有文件的更新文件 
Wzunzip test -f 
Winrar x test -f 


解压缩现有文件的更新文件及新文件 
Wzunzip test -n 
Winrar x test -u 


批量解压缩文件 
Wzunzip *.zip 
WinRAR e *.rar

暮色纯香的主页 暮色纯香 | 初学一级 | 园豆:63
提问于:2013-12-06 14:49
< >
分享
最佳答案
0

因为你压缩的是 E:\xx\xx\xx\C110_Temp\C110,你应该压缩 C110 ,然后保存到 E:\xx\xx\xx\C110_Temp\

收获园豆:10
Launcher | 高人七级 |园豆:45045 | 2013-12-06 14:53

是的!我现在就是想 C110!  但不知道该如何来改!  

 

          intputPath:输入目录 : E:\xx\xx\xx\C110_Temp\C110  这个是不能变的

          那么在CompressRar()中需要如何操作啊??

  
        

暮色纯香 | 园豆:63 (初学一级) | 2013-12-06 15:10

@暮色纯香: 看下 DOS 命令吧

cd E:\xx\xx\xx\C110_Temp

WINRAR A test.rar test -r 

Launcher | 园豆:45045 (高人七级) | 2013-12-06 15:26

@Launcher: 谢了!

暮色纯香 | 园豆:63 (初学一级) | 2013-12-06 15:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册