import java.io.*; import java.util.zip.*; public class ZipGenerator { public static byte[] generateZip(String fileName, byte[] content) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { // Create a new entry inside the ZIP ZipEntry entry = new ZipEntry(fileName); zos.putNextEntry(entry); // Write content to the entry zos.write(content); zos.closeEntry(); } return baos.toByteArray(); } } Use code with caution. Copied to clipboard Alternatives
: Recommended if you need advanced features or support for more archive formats beyond standard ZIP. J Lzip
A library for creating, reading and editing . zip files with JavaScript, with a lovely and simple API. See https://stuk.github.io/ import java