LoginSignup
1
1

More than 1 year has passed since last update.

C#でファイルをtar.gzに圧縮

Posted at

はじめに

C#でtar.gzでファイル圧縮します。

目次

  1. 使用ライブラリ
  2. 実装

Chapter1

軽く調べたところSharpZipLibが良さそう
https://github.com/icsharpcode/SharpZipLib

Chapter2

ファイル名.拡張子
        public void CreateTarGz(string outputTarFilePath, List<string> targetFilePathList)
        {
            using (FileStream fileStrem= new FileStream(outputTarFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
            using (GZipOutputStream gzipStream = new GZipOutputStream(fileStrem))
            {
                // 圧縮率を指定
                // 1が一番早くて底圧縮、9が一番遅くて高圧縮
                gzipStream.SetLevel(1);
                using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
                {
                    try
                    {
                        foreach(var targetFilePath in targetFilePathList)
                        {
                            var tarEntry = TarEntry.CreateEntryFromFile(targetFilePath);
                            tarEntry.Name = Path.GetFileName(targetFilePath);
                            tarArchive.WriteEntry(tarEntry, false);
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1