Skip to content

Can't untar result file #382

@haludi

Description

@haludi

Steps to reproduce

  1. Creating dotnet core app with the code included
  2. Installing SharpZipLib version 1.2.0 by NuGet
  3. Run the application on a directory that contains one text file
  4. Untar the result file by command line - tar -C ./ -xvf /mnt/c/work/temp/Haludi8.tar.gz

Expected behavior

When I use version 1.1.0 the operation works
and I can untar the result file
image

Actual behavior

I get an error

gzip: stdin: decompression OK, trailing garbage ignored
tar: Child returned status 2
tar: Error is not recoverable: exiting now

image

Version of SharpZipLib

1.2.0

Obtained from (only keep the relevant lines)

  • Package installed using NuGet
using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;

namespace SharpZipLibApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var stream = new MemoryStream())
            using (var file = File.OpenWrite(@"C:\work\temp\Haludi8.tar.gz"))
            {
                var dir = @"C:\Users\igal\AppData\Local\Temp\Haludi";
                TarHelper.CreateGzippedTarArchive(stream, sourceDirectory: dir);
                stream.CopyTo(file);
            }
        }
    }

    public class TarHelper
    {
        public static void CreateGzippedTarArchive(Stream outStream, 
            string sourceDirectory)
        {
            using (var gzoStream = new GZipOutputStream(outStream) { 
                IsStreamOwner = false 
            })
            using (var tarArchive = TarArchive.CreateOutputTarArchive(gzoStream))
            {
                tarArchive.IsStreamOwner = false;

                // Note that the RootPath is currently case sensitive 
                // and must be forward slashes e.g. "c:/temp"
                // and must not end with a slash, otherwise 
                // cuts off first char of filename
                // This is scheduled for fix in next release
                tarArchive.RootPath = sourceDirectory.Replace('\\', '/');
                if (tarArchive.RootPath.EndsWith("/"))
                    tarArchive.RootPath = tarArchive.RootPath
                        .Remove(tarArchive.RootPath.Length - 1);

                AddDirectoryFilesToTGZ(tarArchive, sourceDirectory);
            }
            outStream.Seek(0, SeekOrigin.Begin);
        }



        private static void AddDirectoryFilesToTGZ(TarArchive tarArchive, 
            string sourceDirectory)
        {
            AddDirectoryFilesToTGZ(tarArchive, sourceDirectory, string.Empty);
        }

        private static void AddDirectoryFilesToTGZ(TarArchive tarArchive, 
            string sourceDirectory, string currentDirectory)
        {
            var pathToCurrentDirectory = Path.Combine(sourceDirectory, currentDirectory);

            // Write each file to the tgz.
            var filePaths = Directory.GetFiles(pathToCurrentDirectory);
            foreach (string filePath in filePaths)
            {
                var tarEntry = TarEntry.CreateEntryFromFile(filePath);

                // Name sets where the file is written. 
                // Write it in the same spot it exists in the source directory
                tarEntry.Name = filePath.Replace(sourceDirectory, "");

                // If the Name starts with '\' then an extra folder (with a 
                // blank name) will be created, we don't want that.
                if (tarEntry.Name.StartsWith('\\'))
                {
                    tarEntry.Name = tarEntry.Name.Substring(1);
                }
                tarArchive.WriteEntry(tarEntry, true);
            }

            // Write directories to tgz
            var directories = Directory.GetDirectories(pathToCurrentDirectory);
            foreach (string directory in directories)
            {
                AddDirectoryFilesToTGZ(tarArchive, sourceDirectory, directory);
            }
        }
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions