求C#操作rar文件的代码,代码要实现向压缩文件下的某个文件下添加图片,添加文件,同时能删除压缩文件下的某个文件,不知哪位高手做过这样的程序,请指教

解决方案 »

  1.   

    利用WinRAR提供的rar.exe/unrar.exe(此文件在winrar的安装目录下,是一个console程序)对于 rar.exe 的解压和压缩,下面先给出它的 命令行格式对照表,便于了解:rar命令行的使用方法: rar <命令> -<开关 1> -<开关 N> <压缩文件> <文件…> <@列表文件…> <解压路径\>..........
    http://www.zu14.cn/2008/12/31/net_package/
      

  2.   

    2楼正解!        你再百度下“C# 命令行”。甚至可以把winrar的免费版本和你的程序一起打包发布。就解决了。
      

  3.   

     private void button1_Click(object sender, EventArgs e) 
            { 
                product(@"c:\1.RAR", @"c:\1"); 
                
            }         private void product(string p_RarFile,string p_FilePath) 
            { 
                Process prar = new Process(); 
                prar.StartInfo.UseShellExecute = false; 
                prar.StartInfo.CreateNoWindow = true; 
                string _RarPath = @"C:\Program Files\WinRAR\winrar.exe";             prar.StartInfo.Arguments = " E " + p_RarFile + " " + p_FilePath; 
                prar.StartInfo.FileName = _RarPath; 
                prar.Start(); 
            }
      

  4.   

    System.Diagnostics.Process Process1 = new System.Diagnostics.Process(); 
    Process1.StartInfo.FileName = "Winrar.exe"; 
            Process1.StartInfo.CreateNoWindow = true; 
            Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strPath; 
            Process1.StartInfo.Arguments = " a -afzip " + strzipPath + " " + strPath; 
            Process1.StartInfo.Arguments = " a -r "+strzipPath+" " + strPath; 
            Process1.StartInfo.Arguments = " m " + strzipPath + " " + strPath; 
            Process1.StartInfo.Arguments = " a -ep " + strzipPath + " " + strPath;参考 
      

  5.   

    //取得系统临时目录
    string sysTempDir = Path.GetTempPath();//要解压的文件路径,请自行设置
    string rarFilePath = @"d:\test.rar";//确定要解压到的目录,是系统临时文件夹下,与原压缩文件同名的目录里
    string unrarDestPath = Path.Combine(sysTempDir,
        Path.GetFileNameWithoutExtension(rarFilePath));//组合出需要shell的完整格式
    string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"",
        rarFilePath, unrarDestPath);//用Process调用
    using (Process unrar = new Process())
    {
        unrar.StartInfo.FileName = "rar.exe";
        unrar.StartInfo.Arguments = shellArguments;
        //隐藏rar本身的窗口
        unrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        unrar.Start();
        //等待解压完成
        unrar.WaitForExit();
        unrar.Close();
    }//统计解压后的目录和文件数
    DirectoryInfo di = new DirectoryInfo(unrarDestPath);MessageBox.Show(string.Format("解压完成,共解压出:{0}个目录,{1}个文件",
        di.GetDirectories().Length, di.GetFiles().Length));
      

  6.   

    参考:http://www.cnblogs.com/wequst/archive/2009/01/08/1371760.html
      

  7.   

    实现向压缩文件下的某个文件下添加图片,添加文件
    可以先解压,然后一起打包起来。
    删除压缩文件下的某个文件
    先解压,删除后,再打包。
    没弄过rar的,用过操作zip压缩的。
    供楼主参考
    添加引用ICSharpCode.SharpZipLib.dll
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;
    using ICSharpCode.SharpZipLib.Zip;
    using Lib;namespace Lib
    {
        /// <summary>
        /// 文件压缩、解压缩
        /// </summary>
        public class FileCompression
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public FileCompression()
            {
            }        #region 加密、压缩文件        /// <summary>
            /// 压缩文件
            /// </summary>
            /// <param name="fileNames">要打包的文件列表</param>
            /// <param name="GzipFileName">目标文件名</param>
            /// <param name="CompressionLevel">压缩品质级别(0~9)</param>
            /// <param name="SleepTimer">休眠时间(单位毫秒)</param>     
            public static void Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer)
            {
                ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
                try
                {
                    s.SetLevel(CompressionLevel);   //0 - store only to 9 - means best compression                foreach (FileInfo file in fileNames)
                    {
                        FileStream fs = null;
                        try
                        {
                            fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
                        }
                        catch
                        { continue; }                                       //  方法二,将文件分批读入缓冲区
                        byte[] data = new byte[2048];
                        int size = 2048;
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
                        entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);                    s.PutNextEntry(entry);                    while (true)
                        {
                            size = fs.Read(data, 0, size);
                            if (size <= 0) break;                                               s.Write(data, 0, size);
                        }                    fs.Close();
                        file.Delete();                    Thread.Sleep(SleepTimer);
                    }
                }
                finally
                {
                    s.Finish();
                    s.Close();
                }
            }        #endregion        #region 解密、解压缩文件        /// <summary>
            /// 解压缩文件
            /// </summary>
            /// <param name="GzipFile">压缩包文件名</param>
            /// <param name="targetPath">解压缩目标路径</param>       
            public static void Decompress(string GzipFile, string targetPath)
            { 
                string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";            if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
                string CurrentDirectory = directoryName;            byte[] data = new byte[2048];
                int size = 2048;            ZipEntry theEntry = null;           
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
                {
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        if (theEntry.IsDirectory)
                        {// 该结点是目录
                            if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
                        }
                        else
                        {
                            if (theEntry.Name != String.Empty)
                            {
                                //解压文件到指定的目录
                                using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
                                {
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size <= 0) break;
                                       
                                        streamWriter.Write(data, 0, size);
                                    }
                                    streamWriter.Close();
                                }
                            }
                        }
                    }
                    s.Close();
                }           
            }        #endregion    }
    }