TarArchive Hangs When Trying to Extract Contents

303 Views Asked by At

I'm trying to create a tar archive, then extract all the contents to a file using SharpZipLib. I am able to create the tar archive, but the program hangs when trying to extract it. I'm wondering if anyone else can reproduce this problem and see why it is happening. I've also tried using an example that allows for full control, and while debugging, it seems that my program hangs when tarIn.GetNextEntry() is called.

Here is my code

public void CreateTarAndExtract()
        {
            // create tar file
            string tarFile = "path_to_desktop\\tartest.tar";
            string inputFolder = "path_to_desktop\\testfolder";
            using (var output = File.OpenWrite(tarFile))
            {
                using (var archive = TarArchive.CreateOutputTarArchive(output, Encoding.UTF8))
                {
                    var trimLength = inputFolder.Length + 1;

                    foreach (var fsEntry in Directory.GetFileSystemEntries(inputFolder, "*", SearchOption.AllDirectories))
                    {
                        var entry = TarEntry.CreateEntryFromFile(fsEntry);
                        entry.Name = fsEntry.Substring(trimLength);
                        archive.WriteEntry(entry, false);
                    }
                }
            }

            // extract file
            string outputPath = "path_to_desktop\\tartest";
            using (var input = File.OpenRead(tarFile))
            {
                using (var archive = TarArchive.CreateInputTarArchive(input, Encoding.UTF8))
                {
                    archive.ExtractContents(outputPath);
                }
            }
        }
1

There are 1 best solutions below

1
BouRHooD On

I get to unpack the tar archive this way:

    // extract file
    string outputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "tartest");
    using (var input = File.OpenRead(tarFile))
    {
        ExtractTar(input, outputPath);
    }

    /// <summary>
    /// Extractes a <c>tar</c> archive to the specified directory.
    /// </summary>
    /// <param name="stream">The <i>.tar</i> to extract.</param>
    /// <param name="outputDir">Output directory to write the files.</param>
    public static void ExtractTar(Stream stream, string outputDir)
    {
        var buffer = new byte[100];
        while (true)
        {
            stream.Read(buffer, 0, 100);
            var name = Encoding.ASCII.GetString(buffer).Trim('\0');
            if (String.IsNullOrWhiteSpace(name))
                break;
            stream.Seek(24, SeekOrigin.Current);
            stream.Read(buffer, 0, 12);
            var size = Convert.ToInt64(Encoding.UTF8.GetString(buffer, 0, 12).Trim('\0').Trim(), 8);

            stream.Seek(376L, SeekOrigin.Current);

            var output = Path.Combine(outputDir, name);
            if (!Directory.Exists(Path.GetDirectoryName(output)))
                Directory.CreateDirectory(Path.GetDirectoryName(output));
            if (!name.Equals("./", StringComparison.InvariantCulture))
            {
                using (var str = File.Open(output, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    var buf = new byte[size];
                    stream.Read(buf, 0, buf.Length);
                    str.Write(buf, 0, buf.Length);
                }
            }

            var pos = stream.Position;

            var offset = 512 - (pos % 512);
            if (offset == 512)
                offset = 0;

            stream.Seek(offset, SeekOrigin.Current);
        }
    }