Created
August 19, 2014 01:15
-
-
Save ntk1000/7dc379bd41074267f0b4 to your computer and use it in GitHub Desktop.
C#におけるtar.gz ref: http://qiita.com/ntk1000@github/items/edbd7e85bde8f5cd2c9b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;// 例外処理 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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