说明:
最近用net 2.0开发萝莉云,遇到个问题。下载下来的字幕rar文件。无法解压!不管是DotNetZip还是ICSharpCode.SharpZipLib 都无法解压rar!我凑,不科学。然后找来找去。终于找到这个支持2.0,且支持rar的方法。需要可以拿去。案例demo还没写。不过有调用代码。
目录截图:
调用代码:
[C#] 纯文本查看 复制代码 #region 7zZip压缩、解压方法
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="objectPathName">压缩对象(即可以是文件夹|也可以是文件)</param>
/// <param name="objectZipPathName">保存压缩文件的路径</param>
/// <param name="strPassword">加密码</param>
/// 测试压缩文件夹:压缩文件(objectZipPathName)不能放在被压缩文件(objectPathName)内,否则报“文件夹被另一进程使用中”错误。
/// <returns></returns>
public CompressResults Compress7zZip(String objectPathName, String objectZipPathName, String strPassword) {
try {
//http://sevenzipsharp.codeplex.com/releases/view/51254 下载sevenzipsharp.dll
//SevenZipSharp.dll、zLib1.dll、7z.dll必须同时存在,否则常报“加载7z.dll错误”
string libPath = Application.StartupPath + @"\7z.dll";
SevenZip.SevenZipCompressor.SetLibraryPath(libPath);
SevenZip.SevenZipCompressor sevenZipCompressor = new SevenZip.SevenZipCompressor();
sevenZipCompressor.CompressionLevel = SevenZip.CompressionLevel.Fast;
sevenZipCompressor.ArchiveFormat = SevenZip.OutArchiveFormat.Zip;
//被压缩对象是否存在
int beforeObjectNameIndex = objectPathName.LastIndexOf('\\');
string objectPath = objectPathName.Substring(0, beforeObjectNameIndex);
//System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName);
if (Directory.Exists(objectPathName)/*directoryInfo.Exists*/ == false && System.IO.File.Exists(objectPathName) == false) {
return CompressResults.SourceObjectNotExist;
}
int beforeObjectRarNameIndex = objectZipPathName.LastIndexOf('\\');
int objectRarNameIndex = beforeObjectRarNameIndex + 1;
//string objectZipName = objectZipPathName.Substring(objectRarNameIndex);
string objectZipPath = objectZipPathName.Substring(0, beforeObjectRarNameIndex);
//目标目录、文件是否存在
if (System.IO.Directory.Exists(objectZipPath) == false) {
System.IO.Directory.CreateDirectory(objectZipPath);
} else if (System.IO.File.Exists(objectZipPathName) == true) {
System.IO.File.Delete(objectZipPathName);
}
if (Directory.Exists(objectPathName)) //压缩对象是文件夹
{
if (String.IsNullOrEmpty(strPassword)) {
sevenZipCompressor.CompressDirectory(objectPathName, objectZipPathName);
} else {
sevenZipCompressor.CompressDirectory(objectPathName, objectZipPathName, strPassword);
}
} else //压缩对象是文件 无加密方式
{
sevenZipCompressor.CompressFiles(objectZipPathName, objectPathName);
}
return CompressResults.Success;
} catch (Exception ex) {
MessageBox.Show("压缩文件失败!" + ex.Message);
return CompressResults.UnKnown;
}
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipFilePathName">zip文件具体路径+名</param>
/// <param name="unCompressDir">解压路径</param>
/// <param name="strPassword">解密码</param>
/// <returns></returns>
public UnCompressResults UnCompress7zZip(String zipFilePathName, String unCompressDir, String strPassword) {
try {
//SevenZipSharp.dll、zLib1.dll、7z.dll必须同时存在,否则常报“加载7z.dll错误”而项目引用时,只引用SevenZipSharp.dll就可以了
string libPath = Application.StartupPath + @"\7z.dll";
SevenZip.SevenZipCompressor.SetLibraryPath(libPath);
bool isFileExist = File.Exists(zipFilePathName);
if (false == isFileExist) {
MessageBox.Show("解压文件不存在!" + zipFilePathName);
return UnCompressResults.SourceObjectNotExist;
}
File.SetAttributes(zipFilePathName, FileAttributes.Normal); //去掉只读属性
if (Directory.Exists(unCompressDir) == false) {
Directory.CreateDirectory(unCompressDir);
}
SevenZip.SevenZipExtractor sevenZipExtractor;
if (String.IsNullOrEmpty(strPassword)) {
sevenZipExtractor = new SevenZip.SevenZipExtractor(zipFilePathName);
} else {
sevenZipExtractor = new SevenZip.SevenZipExtractor(zipFilePathName, strPassword);
}
sevenZipExtractor.ExtractArchive(unCompressDir);
sevenZipExtractor.Dispose();
return UnCompressResults.Success;
} catch (Exception ex) {
MessageBox.Show("解压缩文件失败!" + ex.Message);
return UnCompressResults.UnKnown;
}
}
#endregion
案例源码下载:
|