C# 无法提取使用SharpZip库创建的tar.gz文件
功能:使用windows服务创建.tar.gz文件。上传到SFTP服务器。在Linux机器中下载它,并使用Linux命令进行解压缩 我有一个windows服务,我使用SharpZipLib库将文件压缩到.tar.gz。现在的问题是,在Linux机器上,90%的文件被成功提取,但对于少数文件,它给出了错误: “一个单独的零块在80” 我使用了另一个库SharpCompression。当我使用此库压缩文件时,所有文件都被成功提取 那么,使用SharpZipLib压缩的文件未被提取的原因是什么呢 编辑:添加了代码 代码:C# 无法提取使用SharpZip库创建的tar.gz文件,c#,linux,gzip,tar,sharpziplib,C#,Linux,Gzip,Tar,Sharpziplib,功能:使用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);
}
}
private void按钮GeneratePackage\u单击(对象发送者,事件参数e)
{
if(this.radiosharpression.Checked)
{
这是。压缩使用SharpCompress();
}
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(“\\”),文件名);
this.CreateTar(this.selectedFolderPath,fullPath);
}
}
私有void压缩使用SharpCompress()
{
this.status.Text+=“\r\n生成.tar.gz”;
var fileName=new System.IO.DirectoryInfo(this.selectedFolderPath).Name+“.tar.gz”;
var fullPath=Path.Combine(this.selectedFolderPath.Substring(0,this.selectedFolderPath.LastIndexOf(“\\”),文件名);
使用(Stream=File.OpenWrite(fullPath))
使用(var writer=WriterFactory.Open(stream、ArchiveType.Tar、CompressionType.GZip))
{
writer.WriteAll(this.selectedFolderPath,“*”,SearchOption.AllDirectories);
}
this.status.Text+=“\r\n压缩已成功完成。”;
this.status.Text+=“\r\n位置:”+完整路径;
}
///
///为给定的源目录创建TAR GZIP文件。
///
///源目录路径。
///TAR文件名。
///如果输入参数路径无效,则引发参数异常。
public void CreateTar(字符串sourceDirectoryPath、字符串tarFileNamePath)
{
如果(!Directory.Exists(sourceDirectoryPath))
{
抛出新ArgumentException(“源目录不存在”);
}
var tarFileDirectoryPath=Path.GetDirectoryName(tarFileNamePath);
if(string.IsNullOrWhiteSpace(tarFileDirectoryPath)| |!Directory.Exists(tarFileDirectoryPath))
{
抛出新ArgumentException(“路径目录不存在”);
}
使用(var fileStream=newfilestream(tarFileNamePath,FileMode.Create,FileAccess.Write,FileShare.None))
使用(var gzipStream=new GZipOutputStream(fileStream))
使用(var-tarArchive=tarArchive.CreateOutputTarArchive(gzipStream))
{
var sourceDirectoryRootPath=sourceDirectoryPath.Replace(“\\”,“/”);
tarArchive.RootPath=sourceDirectoryRootPath;
AddDirectoryFileTotar(tarArchive,sourceDirectoryPath,true);
}
}
///
///递归地将文件和文件夹添加到存档中。
///
///TAR存档实例。
///源目录的路径。
///以递归方式添加要存档的文件夹和文件。
私有void AddDirectoryFilesToTar(TarArchive-TarArchive、string-sourceDirectory、bool-recurse)
{
if(递归)
{
var directories=Directory.GetDirectories(sourceDirectory);
foreach(目录中的var目录)
{
AddDirectoryFilesToTar(tarArchive,directory,true);
}
}
var fileNames=Directory.GetFiles(sourceDirectory);
foreach(文件名中的var文件名)
{
var tarEntry=tarEntry.CreateEntryFromFile(文件名);
////tarEntry.Name=tarEntry.Name.Replace(sourceDirectoryRootPath,string.Empty);
tarArchive.WriteEntry(tarEntry,true);
}
}
你有代码示例吗?@JimJeffries:我已经用我正在使用的代码更新了我的问题。代码包含使用这两个库的实现。如何提取.tar.gz文件?Linux中有一个选项用于提取.tar.gz而无需“零块检查”。。有关命令,请参见-i开关