Dotnet GZIPstream file reading


#1

Hey Guys,

I’m trying to read the contents of a .GZ file in max by using the dotnet GZIPStream class. Now I am a little inexperienced when it comes to .NET so I am currently looking at this example for extracting a set of files provided with the class description and I am trying to put that into proper MXS:

public static void Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
    {
        string currentFileName = fileToDecompress.FullName;
        string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
        using (FileStream decompressedFileStream = File.Create(newFileName))
        {
            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(decompressedFileStream);
                Console.WriteLine($"Decompressed: {fileToDecompress.Name}");
            }
        }
    }
}

I know how to use dotnet classes and objects, but I cannot wrap my head around the bits where the GZIPStream class is being used in the following way and how to translate that to MXS:

using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))

What I have so far is this;

fn decompress fileName newFileName= (
    currentFileName = fileName
    newFileName = newFileName

    decompressedFileStream = undefined
    (dotnetClass "System.IO.FileStream") decompressedFileStream = ((dotnetClass "System.IO.File").create fileName)

    decompressionStream = dotnetClass "System.IO.Compression.GZIPStream" decompressedFileStream (dotnetClass "System.IO.Compression)"CompressionMode.Decompress
)

obviously that doesn’t work and isn’t finished but can anybody point me in the right direction?


#2

Ok, I now have extracting GZip Files working, now on to the live reading instead of creating a file. I’ll just post the extraction function for those interested. Note that I have thrown the decompression in a try/catch because I didn’t want max to lock the files each time the function would error. But basically, you can remove this:

fn decompress fileName newFileName = (
    compressedFileStream = (dotnetClass "System.IO.File").open fileName (dotnetClass "system.io.filemode").open
    decompressedFileStream = (dotnetClass "System.IO.File").create newFileName
    try (
	decompressionStream = dotnetobject "System.IO.Compression.GZipStream" compressedFileStream (dotnetclass "System.IO.Compression.Compressionmode").decompress
	decompressionStream.CopyTo(decompressedFileStream)
    )
    catch (
	print "yeah, that didn't work obviously"
    )
    compressedFileStream.Close()
    decompressedFileStream.Close()
)

decompress fileName newFileName