Article Benjamin Thorne · 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/cls/objectscript/GZIP.cls

Comments

Benjamin Thorne  Jul 17, 2018 to Vitaliy Serdtsev

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

0
Akshay Pandey  Feb 28, 2023 to Benjamin Thorne

Hi @Benjamin Thorne 
I am intrested.

I have PDF scanned documents stored in HSREPO edge (HS_IHE_XDSb_Repository.Documents) which I need to compress before storing and decompress after fetching.

0
Nicki Vallentgoed  Jul 18, 2018 to Vitaliy Serdtsev

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.

0
Vitaliy Serdtsev  Jul 18, 2018 to Nicki Vallentgoed

Then try methods <FONT COLOR="#0000ff">$system</FONT><FONT COLOR="#008080">.Util</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Compress</FONT><FONT COLOR="#000000">/</FONT><FONT COLOR="#0000ff">Decompress</FONT>

0
Nicki Vallentgoed  Aug 2, 2018 to Vitaliy Serdtsev

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.

0