压缩.zip格式
1 using System.IO.Compression; 2 3 //zipFilePath 需要压缩的文件夹路径 "c:\data" 4 //filepathname 压缩后zip文件的路径及名称 "c:\temp\gzip-test.tar.gz" 5 ZipFile.CreateFromDirectory(zipFilePath, filepathname);
压缩.tar格式
1 using SharpCompress.Common; 2 using SharpCompress.Writers; 3 4 //tgzFilename 压缩后zip文件的路径及名称 "c:\temp\gzip-test.tar.gz" 5 //sourceDirectory 需要压缩的文件夹路径 "c:\data" 6 public void TarCreateFromStream(string tgzFilename, string sourceDirectory) 7 { 8 string filesPath = sourceDirectory; 9 using (var zip = File.OpenWrite(tgzFilename)) 10 using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Tar, CompressionType.None)) 11 { 12 zipWriter.WriteAll(filesPath, "*", SearchOption.AllDirectories); 13 } 14 }
压缩.tar.gz格式
1 using SharpCompress.Common; 2 using SharpCompress.Writers; 3 4 //tgzFilename 压缩后zip文件的路径及名称 "c:\temp\gzip-test.tar.gz" 5 //sourceDirectory 需要压缩的文件夹路径 "c:\data" 6 private void CreateTarGZ(string tgzFilename, string sourceDirectory) 7 { 8 string filesPath = sourceDirectory; 9 using (var zip = File.OpenWrite(tgzFilename)) 10 using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Tar, CompressionType.GZip)) 11 { 12 zipWriter.WriteAll(filesPath, "*", SearchOption.AllDirectories); 13 } 14 }