C# ICSharpCode.SharpZipLib生成tar、tar.gz

2021-04-09 00:27

阅读:759

标签:目录   rpc   path   closed   c#   ase   one   using   delete   

原文地址:https://blog.csdn.net/nihao198503/article/details/9204115

将代码原封不动的copy过来,只是因为有关tar的文章太少,大多都是zip的文章

技术分享图片技术分享图片
///   
/// 生成 ***.tar.gz 文件  
///   
/// 文件基目录(源文件、生成文件所在目录)  
/// 待压缩的源文件夹名  
public bool CreatTarGzArchive(string strBasePath, string strSourceFolderName)  
{  
    if (string.IsNullOrEmpty(strBasePath)  
        || string.IsNullOrEmpty(strSourceFolderName)  
        || !System.IO.Directory.Exists(strBasePath)  
        || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))  
    {  
        return false;  
    }  
  
    Environment.CurrentDirectory = strBasePath;  
    string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);  
    string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar.gz");  
  
    Stream outTmpStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);  
  
    //注意此处源文件大小大于4096KB  
    Stream outStream = new GZipOutputStream(outTmpStream);  
    TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);  
    TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);  
    archive.WriteEntry(entry, true);  
  
    if (archive != null)  
    {  
        archive.Close();  
    }  
  
    outTmpStream.Close();  
    outStream.Close();  
  
    return true;  
}  
生成 ***.tar.gz 文件

 

技术分享图片技术分享图片
        /// 
        /// 文件解压
        /// 
        /// 压缩文件路径
        /// 解压到的目录
        /// 
        public static 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.Close();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("压缩出错!");
                return false;
            }
            finally
            {
                if (null != tarArchive) tarArchive.Close();
                if (null != gzipStream) gzipStream.Close();
                if (null != inStream) inStream.Close();
            }
        }
tar.gz解压

 

技术分享图片技术分享图片
///   
/// 生成 ***.tar 文件  
///   
/// 文件基目录(源文件、生成文件所在目录)  
/// 待压缩的源文件夹名  
public bool CreatTarArchive(string strBasePath, string strSourceFolderName)  
{  
    if (string.IsNullOrEmpty(strBasePath)  
        || string.IsNullOrEmpty(strSourceFolderName)  
        || !System.IO.Directory.Exists(strBasePath)  
        || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))  
    {  
        return false;  
    }  
  
    Environment.CurrentDirectory = strBasePath;  
    string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);  
    string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar");  
  
    Stream outStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);  
  
    TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);  
    TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);  
    archive.WriteEntry(entry, true);  
  
    if (archive != null)  
    {  
        archive.Close();  
    }  
  
    outStream.Close();  
  
    return true;  
}  
生成 ***.tar 文件

 

技术分享图片技术分享图片
///   
/// tar包解压  
///   
/// tar包路径  
/// 解压到的目录  
///   
public static bool UnpackTarFiles(string strFilePath, string strUnpackDir)  
{  
    try  
    {  
        if (!File.Exists(strFilePath))  
        {  
            return false;  
        }  
  
        strUnpackDir = strUnpackDir.Replace("/", "\\");  
        if (!strUnpackDir.EndsWith("\\"))  
        {  
            strUnpackDir += "\\";  
        }  
  
        if (!Directory.Exists(strUnpackDir))  
        {  
            Directory.CreateDirectory(strUnpackDir);  
        }  
  
        FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
        DhcEc.SharpZipLib.Tar.TarInputStream s = new DhcEc.SharpZipLib.Tar.TarInputStream(fr);  
        DhcEc.SharpZipLib.Tar.TarEntry theEntry;  
        while ((theEntry = s.GetNextEntry()) != null)  
        {  
            string directoryName = Path.GetDirectoryName(theEntry.Name);  
            string fileName = Path.GetFileName(theEntry.Name);  
  
            if (directoryName != String.Empty)  
                Directory.CreateDirectory(strUnpackDir + directoryName);  
  
            if (fileName != String.Empty)  
            {  
                FileStream streamWriter = File.Create(strUnpackDir + theEntry.Name);  
  
                int size = 2048;  
                byte[] data = new byte[2048];  
                while (true)  
                {  
                    size = s.Read(data, 0, data.Length);  
                    if (size > 0)  
                    {  
                        streamWriter.Write(data, 0, size);  
                    }  
                    else  
                    {  
                        break;  
                    }  
                }  
  
                streamWriter.Close();  
            }  
        }  
        s.Close();  
        fr.Close();  
  
        return true;  
    }  
    catch (Exception)  
    {  
        return false;  
    }  
} 
tar包解压

 

技术分享图片技术分享图片
///   
/// zip压缩文件  
///   
/// filename生成的文件的名称,如:C\123\123.zip  
/// directory要压缩的文件夹路径  
///   
public static bool PackFiles(string filename, string directory)  
{  
    try  
    {  
        directory = directory.Replace("/", "\\");  
  
        if (!directory.EndsWith("\\"))  
            directory += "\\";  
        if (!Directory.Exists(directory))  
        {  
            Directory.CreateDirectory(directory);  
        }  
        if (File.Exists(filename))  
        {  
            File.Delete(filename);  
        }  
  
        FastZip fz = new FastZip();  
        fz.CreateEmptyDirectories = true;  
        fz.CreateZip(filename, directory, true, "");  
  
        return true;  
    }  
    catch (Exception)  
    {  
        return false;  
    }  
} 
zip压缩文件

 

技术分享图片技术分享图片
///   
/// zip解压文件  
///   
/// 压缩文件的名称,如:C:\123\123.zip  
/// dir要解压的文件夹路径  
///   
public static bool UnpackFiles(string file, string dir)  
{  
    try  
    {  
        if (!File.Exists(file))  
            return false;  
  
        dir = dir.Replace("/", "\\");  
        if (!dir.EndsWith("\\"))  
            dir += "\\";  
  
        if (!Directory.Exists(dir))  
            Directory.CreateDirectory(dir);  
  
        FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
        DhcEc.SharpZipLib.Zip.ZipInputStream s = new DhcEc.SharpZipLib.Zip.ZipInputStream(fr);  
        DhcEc.SharpZipLib.Zip.ZipEntry theEntry;  
        while ((theEntry = s.GetNextEntry()) != null)  
        {  
            string directoryName = Path.GetDirectoryName(theEntry.Name);  
            string fileName = Path.GetFileName(theEntry.Name);  
  
            if (directoryName != String.Empty)  
                Directory.CreateDirectory(dir + directoryName);  
  
            if (fileName != String.Empty)  
            {  
                FileStream streamWriter = File.Create(dir + theEntry.Name);  
  
                int size = 2048;  
                byte[] data = new byte[2048];  
                while (true)  
                {  
                    size = s.Read(data, 0, data.Length);  
                    if (size > 0)  
                    {  
                        streamWriter.Write(data, 0, size);  
                    }  
                    else  
                    {  
                        break;  
                    }  
                }  
  
                streamWriter.Close();  
            }  
        }  
        s.Close();  
        fr.Close();  
  
        return true;  
    }  
    catch (Exception)  
    {  
        return false;  
    }  
}
zip解压文件

 

C# ICSharpCode.SharpZipLib生成tar、tar.gz

标签:目录   rpc   path   closed   c#   ase   one   using   delete   

原文地址:https://www.cnblogs.com/vichin/p/9071239.html


评论


亲,登录后才可以留言!