Use GZIP to compress/decompress files

The following code snippet uses GZIP to compress/decompress a file. Choose a file by specifying a file path in the "filename" variable.


Class objectscript.GZIP Extends %RegisteredObject
{
    classmethod test() {
        //Export Global(s) uncompressed
        set filename="C:\Temp\myglobal.xml"
        do $System.OBJ.Export("^oddEXTR.gbl",filename)
        //Open exported Globals
        set uncompressed = ##class(%FileBinaryStream).%New()
        set uncompressed.Filename=filename
        Set compressed = "C:\temp\mycomglobal.xml"
        //Open File Device over Gzip and Copy Uncompressed information to it
        Open compressed:("WUNK":::/GZIP=1:/NOXY=1:/OBUFSIZE=32768):0
        Use compressed
        do uncompressed.OutputToDevice()
        close compressed
        //Create New File  
        set out = ##class(%FileBinaryStream).%New()
        set out.Filename= "C:\Temp\decomp.xml"
        //Open compressed File and save information uncompressed over gzip
        Set file=##class(%File).%New(compressed)
        Do file.Open("RUK:::/GZIP=1:/NOXY=1")
        while ' file.AtEnd
        {
            set line = file.ReadLine()
            do out.Write(line)
        }
        do out.%Save()
    }
}

The link to the code on GitHub: https://github.com/intersystems-community/code-snippets/blob/master/src/...

Comments

This is an old post from a deprecated application - I'm just providing another way / explanation if anyone in the community is interested.

What would be nice though is if IS creates an in memory stream version of the zip classes.

That would be useful for generating files and zipping the stream for email etc, without hitting the disk.

That will not work. 

I am not in control of the software on a client pc, but most pc's will have a built in zip handler.

So zip would be more useful.