Archive

Posts Tagged ‘uncompress’

Uncompressing UNIX *.Z files

December 6th, 2009 8 comments

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)) {
            int read;
            while ((read = inStream.Read(buffer, 0, buffer.Length)) > 0) {
                outStream.Write(buffer, 0, read);
            }
        }
    }
}