求C#操作rar文件的代码,代码要实现向压缩文件下的某个文件下添加图片,添加文件,同时能删除压缩文件下的某个文件,不知哪位高手做过这样的程序,请指教
解决方案 »
- C# 如何实现连续注册多个DLL文件
- 请问能否用.NET 2.0开发可以在WINCE 5.0平台运行的程序?
- 我中病毒了(网页被加上恶意脚本语句),我想写个程序改回来
- c#如何读写excel文件呢?
- directx,想把动画画到一个多媒体应用的一个图形区域,不用directx画窗口,行不?
- 在javascript中怎么样定义一个两维数组?
- 怎样在删除之前做确认?????????
- 调用新中新身份证读卡器接口API里的函数Syn_ReadMsg报错
- 这些符号在vs.net2003.cs文件里面显示成?号
- 新建vc#的asp.net application时的问题
- TabControl容器的问题!
- SendKeys.SendWait()发送CTRL+数字组合键问题
http://www.zu14.cn/2008/12/31/net_package/
{
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();
}
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;参考
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));
可以先解压,然后一起打包起来。
删除压缩文件下的某个文件
先解压,删除后,再打包。
没弄过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 }
}