项目中,直接获取7z的项目
Install-Package SevenZipSharp -Version 0.64.0
然后,下载7z.dll
https://github.com/gdoujkzz/7zdll
然后,项目会用SevenZipSharp的方法调用,7z.dll,实现对文件的7z算法压缩。
/// <summary>
/// 7z压缩
/// </summary>
public class R7z
{
public R7z()
{
var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "7z\\7z.dll");
SevenZipBase.SetLibraryPath(path);
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="targetName">target.7z</param>
/// <param name="files">xxx.txt,xxx.txt</param>
/// <returns></returns>
public bool CompressFiles(string targetName, params string[] files)
{
try
{
var tmp = new SevenZipCompressor();
tmp.ScanOnlyWritable = true;
tmp.CompressFiles(targetName, files);
return true;
}
catch (Exception)
{ }
return false;
}
/// <summary>
/// 加密压缩文件
/// </summary>
/// <param name="targetName">target.7z</param>
/// <param name="files">xxx.txt,xxx.txt</param>
/// <returns></returns>
public bool CompressFilesEncrypted(string targetName, string password, params string[] files)
{
try
{
var tmp = new SevenZipCompressor();
tmp.ScanOnlyWritable = true;
tmp.CompressFilesEncrypted(targetName, password, files);
return true;
}
catch (Exception)
{ }
return false;
}
/// <summary>
/// 解压文件
/// </summary>
/// <param name="CompressFile">压缩的文件</param>
/// <param name="targetAddress">解压的目标地址下</param>
/// <param name="password">解密密码</param>
/// <returns></returns>
public bool Decompression(string CompressFile, string targetAddress, string password = "")
{
try
{
if (string.IsNullOrWhiteSpace(password))
{
using (var tmp = new SevenZipExtractor(CompressFile))
{
for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
{
tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
}
}
}
else
{
using (var tmp = new SevenZipExtractor(CompressFile, password))
{
for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
{
tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
}
}
}
}
catch (Exception)
{ }
return false;
}
}
使用方式
new R7z().CompressFiles(@"C:\Users\Administrator\Desktop\a.7z", @"C:\Users\Administrator\Desktop\2a2baf80-8ded-4a09-80d4-880a740ebb49");
new R7z().Decompression(@"C:\Users\Administrator\Desktop\a.7z", @"C:\Users\Administrator\Desktop\");
new R7z().CompressFilesEncrypted(@"C:\Users\Administrator\Desktop\a.7z", "123456", @"C:\Users\Administrator\Desktop\2a2baf80-8ded-4a09-80d4-880a740ebb49");
new R7z().Decompression(@"C:\Users\Administrator\Desktop\a.7z", @"C:\Users\Administrator\Desktop", "123456");
经测试正常,下面提供具体的demo
链接:https://pan.baidu.com/s/1r1UzH7xtqWEI8ngtjXWxqg
提取码:zz81
标签:tmp,Administrator,string,C#,压缩,Desktop,new,7z From: https://blog.51cto.com/kesshei/6287433