1.SharpCompress安装
在vs的nuget下搜索安装SharpCompress,如图所示
2.解压缩包功能实现
csharp
/// <summary>
/// 解压压缩包
/// </summary>
/// <param name="filePath">压缩包文件路径</param>
/// <param name="directoryPath">解压路径</param>
public static bool UnTar(string filePath,string directoryPath)
{
bool bRet = false;
try
{
string folderName = "";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
ReaderOptions options = new ReaderOptions();
//解决中文乱码问题
options.ArchiveEncoding.Default = Encoding.GetEncoding("utf-8");
ExtractionOptions extract = new ExtractionOptions { ExtractFullPath = true, Overwrite = true };
using (Stream stream = File.OpenRead(filePath))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
if (!string.IsNullOrEmpty(reader.Entry.Key) && reader.Entry.Size == 0 && (reader.Entry.Key.EndsWith("/") || reader.Entry.Key.EndsWith("\\")))
{
//过滤文件夹
continue;
}
reader.WriteEntryToDirectory(directoryPath, extract);
}
}
bRet = true;
}
}
catch (Exception ex)
{
}
return bRet;
}