1

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?

user3756506
  • 431
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [Password protected zip file in java](https://stackoverflow.com/questions/10587561/password-protected-zip-file-in-java). I downvoted because [no evident research](https://idownvotedbecau.se/noresearch/) was done. – Austin Schaefer Jun 18 '19 at 08:28
  • 2
    @AustinSchäfer OP says they are aware of libraries for zipping files but need a way for zipping with password in-memory. Your duplicate doesn't seem to be pertinent. – RealSkeptic Jun 18 '19 at 09:39
  • @Austin Schäfer, ok just added more code – user3756506 Jun 18 '19 at 10:56
  • in fact, it appears that winzipaes *does* support exactly what you're trying to do. Use a ByteArrayInputStream. – President James K. Polk Jun 18 '19 at 12:51

2 Answers2

1

A little late but hope this little snippet helps someone else. This is for zip4j on Java 7

private byte[] compressFileByZip(byte[] fileBytes, String filenameInZip, String zipFilePassword) throws ZipException {

  // Zip into a ByteArrayOutputStream
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try(ZipOutputStream zos = new ZipOutputStream(baos)) {

    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    zipParameters.setSourceExternalStream(true);
    zipParameters.setFileNameInZip(filenameInZip);

    // Set the encryption method to AES Zip Encryption
    zipParameters.setEncryptFiles(true);
    zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
    zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
    zipParameters.setPassword(zipFilePassword);

    // Zip, flush and clean up
    zos.putNextEntry(null, zipParameters);
    zos.write(fileBytes);
    zos.flush();
    zos.closeEntry();
    zos.close();
    zos.finish();

  } catch(IOException ioe) {
    ioe.printStackTrace();
    LOG.error("Error writing compressed file", ioe);
  }

  // Extract zipped file as byte array
  byte[] byteCompressedFile = baos.toByteArray();

  LOG.info("Zipped successfully");

  return byteCompressedFile;

}

The section on encryption is optional.

eruina
  • 853
  • 3
  • 9
  • 18
0

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

user3756506
  • 431
  • 1
  • 3
  • 12