Search code examples
javaencryptionzipaes

Zip byte array with password


I have pdf file in byte[] array. I want to compress it and encrypt with password. I don't want to create any temp files. But libraries like zip4j, winzipaes doesn't support it. They accept only File objects.

EDIT: code for simple zip:

public static byte[] zipBytes(String filename, byte[] input) throws IOException {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ZipOutputStream zos = new ZipOutputStream(baos);

ZipEntry entry = new ZipEntry(filename);

entry.setSize(input.length);

zos.putNextEntry(entry);

zos.write(input);

zos.closeEntry();

zos.close();

return baos.toByteArray();}

How add encryption and password?


Solution

  • I have found some sources and fit it to my problem. Load it to https://github.com/r331/memzipenc

    MemZipEnc.getEncryptZipByte(byte[] file , java.lang.String password, java.lang.String filename) this static method encrypts and zips single file in memory without saving files on hdd