How to decompress a GZip string on the server side script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2019 09:45 AM
Community,
I have a situation where I need to unzip the contents of a Gzip string from attachment_doc table
var strEncodedString1 = 'H4sIAAAAAAAAAA==';
var strEncodedString2 = 'c3RydgEApSAX2wQAAAA=';
//Got the above stings from a REST call to the sys_attachment_doc table. It is a text file with ABCD as content.
var objStringUtil = new GlideStringUtil();
var bytes1 = objStringUtil.base64Decode(strEncodedString1);
var bytes2 = objStringUtil.base64Decode(strEncodedString2);
//This gives a zip string. I would have to decompress it to get the contents as text ABCD
gs.log(bytes1 + bytes2);
I am stuck at the decompressing part.
Looking for suggestions on how to proceed.
Do I need to write a Java class to call the GZIPInputStream to return a decompressed string?
Suggestions please.
Thanks,
vkachineni
Vinod Kumar Kachineni
Community Rising Star 2022
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 03:06 AM
Hi Vkachineni,
Did you find a solution for this as I have a same type of requirement .
Please let me know if you find anything .
Thanks,
Anji
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 06:40 PM
Sorry. Could not find a native solution. Alternative is to write a Java or .NET program to get data and decompress it and write it to memory/disk. Here is an example java class.
//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.Base64.*;
import java.util.Collections;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.nio.*;
import java.nio.charset.StandardCharsets;
class Rextester
{
public static void main(String args[])
{
Stack<Integer> fullResponse = new Stack<Integer>();
byte[] bytes1 = Base64.getDecoder().decode("H4sIAAAAAAAAAA==");
byte[] bytes2 = Base64.getDecoder().decode("c3RydgEApSAX2wQAAAA=");
for(int i=0; i<=bytes1.length -1; i++)
{
int n = (bytes1[i] + 256) % 256;
fullResponse.push(n);
//System.out.println(n);
}
//System.out.println("************");
for(int i=0; i<=bytes2.length -1; i++)
{
int n = (bytes2[i] + 256) % 256;
fullResponse.push(n);
// System.out.println(n);
}
//System.out.println("Full =" + fullResponse);
/*Stack<Integer> fullResponseCorrectOrder = new Stack<Integer>();
while(!fullResponse.isEmpty())
{
fullResponseCorrectOrder.push(fullResponse.pop());
}
//System.out.println("Full =" + fullResponseCorrectOrder);*/
// Creating the array and using toArray()
Integer[] arr = new Integer[fullResponse.size()];
arr = fullResponse.toArray(arr);
for(int i = 0; i < arr.length; i++){
//System.out.println(arr[i]);
}
decompress(arr);
}
public static void decompress(Integer[] data)
{
try{
byte[] bytes = convertIntArrayToByteArray(data);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
GZIPInputStream gis = new GZIPInputStream(bis);
byte[] buffer = new byte[1024];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = gis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
byte[] result = bos.toByteArray();
//close resources
bis.close();
gis.close();
String string = new String(result, StandardCharsets.UTF_8);
System.out.println(string);
}catch (IOException e) {
e.printStackTrace();
}
}
public static int[] toPrimitives(Integer[] oInts)
{
int[] ints = new int[oInts.length];
for(int i = 0; i < oInts.length; i++){
ints[i] = oInts[i];
//System.out.println(bytes[i]);
}
return ints;
}
public static byte[] convertIntArrayToByteArray(Integer[] data) {
if (data == null) return null;
// ----------
byte[] byts = new byte[data.length * 4];
for (int i = 0; i < data.length; i++)
byts[i] = data[i].byteValue();
//System.arraycopy(convertIntToByteArray(data[i]), 0, byts, i * 4, 4);
return byts;
}
}
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 10:16 PM
How can we use the Java code in Servicenow?
Thanks,
Anji
