功能:使用Windows服务创建.tar.gz文件。将其上传到SFTP服务器。在Linux机器上下载并使用Linux命令解压缩。
我有一个Windows服务,我使用 SharpZipLib 库将文件压缩到.tar.gz。现在的问题是,在Linux机器上,90%的文件被成功提取,但是对于少数文件,它会出错:
“80”处的单独零块
我使用了另一个库 SharpCompression 。当我使用此库压缩文件时,所有文件都被成功提取。
那么使用 SharpZipLib 压缩文件的原因可能是什么原因?
编辑:已添加代码
代码:
private void buttonGeneratePackage_Click(object sender, EventArgs e)
{
if (this.radioSharpCompression.Checked)
{
this.CompressUsingSharpCompress();
}
else if (this.radioSharpZip.Checked)
{
var fileName = new System.IO.DirectoryInfo(this.selectedFolderPath).Name + ".tar.gz";
var fullPath = Path.Combine(this.selectedFolderPath.Substring(0, this.selectedFolderPath.LastIndexOf("\\")), fileName);
this.CreateTar(this.selectedFolderPath, fullPath);
}
}
private void CompressUsingSharpCompress()
{
this.status.Text += "\r\nGenerating .tar.gz";
var fileName = new System.IO.DirectoryInfo(this.selectedFolderPath).Name + ".tar.gz";
var fullPath = Path.Combine(this.selectedFolderPath.Substring(0, this.selectedFolderPath.LastIndexOf("\\")), fileName);
using (Stream stream = File.OpenWrite(fullPath))
using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.GZip))
{
writer.WriteAll(this.selectedFolderPath, "*", SearchOption.AllDirectories);
}
this.status.Text += "\r\nCompression completed Successfully.";
this.status.Text += "\r\nLocation : " + fullPath;
}
/// <summary>
/// Create TAR GZIP file for the given source directory.
/// </summary>
/// <param name="sourceDirectoryPath">Source directory path.</param>
/// <param name="tarFileNamePath">TAR file name.</param>
/// <exception cref="ArgumentException">Throws argument exception if input parameter path are not valid.</exception>
public void CreateTar(string sourceDirectoryPath, string tarFileNamePath)
{
if (!Directory.Exists(sourceDirectoryPath))
{
throw new ArgumentException("Source Directory Does not exists");
}
var tarFileDirectoryPath = Path.GetDirectoryName(tarFileNamePath);
if (string.IsNullOrWhiteSpace(tarFileDirectoryPath) || !Directory.Exists(tarFileDirectoryPath))
{
throw new ArgumentException("Path Directory Does not exists");
}
using (var fileStream = new FileStream(tarFileNamePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (var gzipStream = new GZipOutputStream(fileStream))
using (var tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
{
var sourceDirectoryRootPath = sourceDirectoryPath.Replace("\\", "/");
tarArchive.RootPath = sourceDirectoryRootPath;
AddDirectoryFilesToTar(tarArchive, sourceDirectoryPath, true);
}
}
/// <summary>
/// Recursively add files and folders to the archive.
/// </summary>
/// <param name="tarArchive">TAR archive instance.</param>
/// <param name="sourceDirectory">Path of the source directory.</param>
/// <param name="recurse">Add folder and files to archive recursively.</param>
private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
{
if (recurse)
{
var directories = Directory.GetDirectories(sourceDirectory);
foreach (var directory in directories)
{
AddDirectoryFilesToTar(tarArchive, directory, true);
}
}
var fileNames = Directory.GetFiles(sourceDirectory);
foreach (var fileName in fileNames)
{
var tarEntry = TarEntry.CreateEntryFromFile(fileName);
////tarEntry.Name = tarEntry.Name.Replace(sourceDirectoryRootPath, string.Empty);
tarArchive.WriteEntry(tarEntry, true);
}
}