Skip to content

Instantly share code, notes, and snippets.

@ntk1000
Created August 19, 2014 01:15
Show Gist options
  • Save ntk1000/7dc379bd41074267f0b4 to your computer and use it in GitHub Desktop.
Save ntk1000/7dc379bd41074267f0b4 to your computer and use it in GitHub Desktop.
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
public void CreateGz(string outputFilePath, string inputFilePath)
{
using (FileStream infs = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (FileStream outfs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (GZipOutputStream gzipStream = new GZipOutputStream(outfs))
{
gzipStream.SetLevel(1);// 圧縮レベル: デフォルト6で遅いので強制的に1にセット
try
{
infs.CopyTo(gzipStream);
}
catch
{
throw;// 例外処理
}
}
}
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
public void CreateTarGz(string outputTarFilePath, string sourceDirectory)
{
using (FileStream fs = new FileStream(outputTarFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (GZipOutputStream gzipStream = new GZipOutputStream(fs))
{
gzipStream.SetLevel(1);
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
{
try
{
Directory.EnumerateFiles(sourceDirectory)
.ToList()
.ForEach(x =>
{
var tarEntry = TarEntry.CreateEntryFromFile(x);
tarEntry.Name = Path.GetFileName(x);
tarArchive.WriteEntry(tarEntry, false);
});
}
catch
{
throw;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment