Article
· Jul 17, 2018 1m read

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/...

Discussion (6)2
Log in or sign up to continue