2

Functionality : Create .tar.gz file using windows service. Upload it to SFTP server. Download it in Linux machine and extract using Linux command.

I have a windows service where I compress file to .tar.gz using SharpZipLib library. Now issue is that on Linux machine 90% of files are being extracted successfully but for few files it gives error :

"A lone zero block at 80"

I used another library SharpCompression. When I compress files using this library, all files are being extracted successfully.

So what could be the reason for file not being extracted which is compressed using SharpZipLib ?

Edit : Code Added

Code :

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);
    }
}
SpiderCode
  • 10,062
  • 2
  • 22
  • 42

0 Answers0