Zip byte array and return zipped byte array - Android File Input Output

Android examples for File Input Output:Byte Array Compress

Description

Zip byte array and return zipped byte array

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

    public static byte[] zip(byte[] data) {
        byte[] b = null;
        try {/*from  w  w  w . j a va  2  s .co m*/
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ZipOutputStream zip = new ZipOutputStream(bos);
            ZipEntry entry = new ZipEntry("zip");
            entry.setSize(data.length);
            zip.putNextEntry(entry);
            zip.write(data);
            zip.closeEntry();
            zip.close();
            b = bos.toByteArray();
            bos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return b;
    }
}

Related Tutorials