3

I have downloaded the SharpCompress source code and created a simple console application to decompress a small .xz file. Following several different examples on the github site and other examples here on stackoverflow, I can't find any combinations that actually work for "unzipping" a .xz file, nor any instructions or documentation. Has anyone actually been able to "unzip" a .xz file using SharpCompress?

        using (Stream stream = File.OpenRead(@"C:\temp\ot.xz"))
        {
            using (var reader = ReaderFactory.Open(stream))
            {
                while (reader.MoveToNextEntry())
                {
                    if (!reader.Entry.IsDirectory)
                    {
                        Console.WriteLine(reader.Entry.Key);
                        reader.WriteEntryToDirectory(@"C:\temp", new ExtractionOptions()
                        {
                            ExtractFullPath = true,
                            Overwrite = true
                        });
                    }
                }
            }
        }

This particular code throws an exception 'Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ'

The following code works better (doesn't throw an error) but the Entry.Key value is unexpected or gibberish.

        using (Stream stream = File.OpenRead(@"C:\temp\ot.xz"))
        {
            var xzStream = new XZStream(stream);
            using (var reader = TarReader.Open(xzStream))// ReaderFactory.Open(stream))
            {
                while (reader.MoveToNextEntry())
                {
                    if (!reader.Entry.IsDirectory)
                    {
                        Console.WriteLine(reader.Entry.Key);
                    }
                }
            }
        }
mobiletonster
  • 407
  • 1
  • 6
  • 10
  • Are you certain it's a valid .XZ file? I would suggest opening it in a hex editor and checking for the magic (first six bytes), which should be FD 37 7A 58 5A 00. https://tukaani.org/xz/xz-file-format-1.0.4.txt – glenebob Apr 26 '18 at 21:00
  • Yes. I have done that and it does match the magic first six bytes. I have even used the sharpcompress XZStream.IsXZStream(stream) to check it and it returns true. – mobiletonster Apr 26 '18 at 21:13
  • I was able to repro this issue with a .XZ file of my own. It appears .XZ support is not complete, and ReaderFactory is extremely broken. I found a reference to this repo: https://github.com/sambott/XZ.NET. Using that library, I am able to decompress my file. Maybe that would work for you. – glenebob Apr 26 '18 at 22:25
  • Interesting. I cloned the XZ.NET repo and it looks like it references SharpCompress as a dependency. Not sure what this particular "wrapper" is doing or how to use it. If it worked for you, would you mind sharing what code you used to decompress your xz file? – mobiletonster Apr 27 '18 at 05:41

1 Answers1

4

As it turns out, both SharpCompress and XZ.NET will work using the same simple client code.

Note however, that one of the files I tested with caused XZ.NET to throw an exception when attempting to read past end of stream, as both examples below do. SharpCompress handled both files correctly.

using (Stream xz = new XZStream(File.OpenRead(@"\temp\server.crt.xz")))
using (Stream stream = new MemoryStream())
{
    xz.CopyTo(stream);
}

Or, for compressed plain text:

using (Stream xz = new XZStream(File.OpenRead(@"\temp\server.crt.xz")))
using (TextReader reader = new StreamReader(xz))
{
    Debug.WriteLine(reader.ReadToEnd());
}
glenebob
  • 1,943
  • 11
  • 11
  • Thanks for this. I noticed that as well. Seems like I can just use the SharpCompress library (and I used xz.CopyTo(fileStream) in my case since I want to "unzip" the contents and it works fine. Ultimately, I will be using the xz library code in a UWP application, so for the XZ.NET library (which targets .net standard 1.1) is compatible as is, but it looks like I'm going to need to do some work on SharpCompress to make UWP happy, but it shouldn't be too hard. – mobiletonster Apr 27 '18 at 19:55
  • Thank you for sharing the code, I think this is the only available code sample throughout the internet. :D – Bimal Das Dec 27 '18 at 12:58