需要依赖dll :ICSharpCode.SharpZipLib.dll
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
/// <summary>
/// 文件解压
/// </summary>
/// <param name="zipPath">压缩文件路径</param>
/// <param name="goalFolder">解压到的目录</param>
/// <returns></returns>
public bool UnzipTgz(string zipPath, string goalFolder)
{
Stream inStream = null;
Stream gzipStream = null;
TarArchive tarArchive = null;
try
{
using (inStream = File.OpenRead(zipPath))
{
using (gzipStream = new GZipInputStream(inStream))
{
tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
tarArchive.ExtractContents(goalFolder);
tarArchive.CloseArchive();
}
}
return true;
}
catch (Exception ex)
{
//LogManager.GetCurrentClassLogger().Error(ex);
return false;
}
finally
{
if (null != tarArchive) tarArchive.CloseArchive();
if (null != gzipStream) gzipStream.Close();
if (null != inStream) inStream.Close();
}
}
解压到指定文件
using System.IO;
using System.IO.Compression;
public void ungzip(string path, string decomPath, bool overwrite)
{
if (File.Exists(decomPath))
{
if (overwrite)
{
File.Delete(decomPath);
}
else
{
throw new IOException("The decompressed path you specified already exists and cannot be overwritten.");
}
}
//create our file streams
GZipStream stream = new GZipStream(new FileStream(path, FileMode.Open, FileAccess.ReadWrite), CompressionMode.Decompress);
FileStream decompressedFile = new FileStream(decomPath, FileMode.OpenOrCreate, FileAccess.Write);
int data;
while ((data = stream.ReadByte()) != -1)
{
decompressedFile.WriteByte((byte)data);
}
decompressedFile.Close();
stream.Close();
}