C#解压tar.gz文件

需要依赖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();

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值