Groovy web console

subscribe to the feed Subscribe
to this
site

Zip compression

Published 1 month ago by Ram with tags ZIP Compression
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.GZIPOutputStream;
import java.io.ByteArrayOutputStream;
 
def val = zip("Rama")

//def result = convertToBinaryData(val)
 
def zip(String s){
    String result = "";
    def targetStream = new ByteArrayOutputStream()
    ZipOutputStream zipStream = new ZipOutputStream(targetStream)
    zipStream.putNextEntry(new ZipEntry("name"))
    zipStream.write(s.getBytes('UTF-8'))
    zipStream.close()
    def zippedBytes = targetStream.toByteArray()
    for (byte b : zippedBytes) {             
         result = result + Integer.toBinaryString(b);         
    }
    targetStream.close()
    return result
}

// convert data into binary data
def convertToBinaryData(def data) {
        String result = ""
        if (data) {
            byte[] infoBin = data.getBytes("UTF-8");  
            for (byte b : infoBin) {             
                result = result + Integer.toBinaryString(b);         
            }
            result = "0x".concat(result)
        }
    return result;
}