Home > Uncategorized > Uncompressing UNIX *.Z files

## Uncompressing UNIX *.Z files

I searched online for a .NET library that can be used to decompress *.Z files (compressed by the UNIX compress utility) but was unable to find anything non-commercial. I found however some code in Java that looked promising, so I ported it to C#. I integrated the resulting code with the SharpZipLib library, but until that gets officially integrated and released, I decided to release a stand-alone version here.

The code is contained in a single file (download LzwInputStream.cs) and is pretty straight-forward to use. The following example shows how to uncompress a *.Z file using c-sharp.

using System;
using System.IO;

using Ebixio.LZW;

class MainClass
{
public static void Main(string[] args)
{
byte[] buffer = new byte[4096];
string outFile = Path.GetFileNameWithoutExtension(args[0]);

using (Stream inStream = new LzwInputStream(File.OpenRead(args[0])))
using (FileStream outStream = File.Create(outFile)) {
}
}
}
}

Categories: Uncategorized Tags:
1. April 13th, 2010 at 14:21 | #1

Thanks a lot for your code !!

2. June 17th, 2010 at 14:20 | #2

This is great! Any chance you could create a similar class and example that will compress a file to .Z format?

3. June 17th, 2010 at 22:13 | #3

@Kevin Gravelle
No such plans. Sorry.

4. September 27th, 2010 at 13:39 | #4

Hi Gabriel

When I try to uncompress my file I get the following errormessage:

“Wrong LZW header. Magic bytes don’t match. 0x1f 0x8b”

I have uncompressed it successfully with the code from http://www.example-code.com/csharp/csharp-unix-compress.asp, but like you I’m looking for something non-commercial.

5. September 28th, 2010 at 00:39 | #5

@Kristian Poulsen
The file is expected to start with the magic marker 0x1f 0x9d. Your file starts with 0x1f 0x8b instead. See if you can use the unix “uncompress” utility on it. There are a few file formats that fall under the LZW umbrella. This code is for the LZC variant.

I suspect you’re trying to feed a file created with “gzip” to this code. It expects files created with the “compress” utility. The SharpZipLib code mentioned in the post should already handle gzip’d files.

6. September 28th, 2010 at 06:54 | #6

@Gabriel Burca
Thanks a lot for your help … I’m new to compressed files, and hadn’t figured out that the file could be handled as a gzip file. As you said, the SharpZipLib code did the job.

7. April 17th, 2016 at 15:40 | #7

Greetings.

I’m the new maintainer of SharpZipLib, and as part of an effort to clean up and update the code, we are working on relicensing everything to use the MIT license.

Based on this post, it appears that you are the author of some LZW methods that were never completely integrated into the main distribution. If you would be so kind, please read over the discussion at https://github.com/icsharpcode/SharpZipLib/issues/63 and https://github.com/icsharpcode/SharpZipLib/issues/103 and give your consent to relicensing your code under the MIT license.

Thank you, and thank you for your contribution.

-Neil

8. April 18th, 2016 at 20:07 | #8

@Neil McNeight
I replied to he discussion on github.com. We can work out the details there.