srikanth-lingala / zip4j

A Java library for zip files and streams
Apache License 2.0
2.06k stars 310 forks source link

zip files with stream #467

Closed pardeep131085 closed 2 years ago

pardeep131085 commented 2 years ago

Hi,

I have look at the example here https://github.com/srikanth-lingala/zip4j#adding-entries-with-zipoutputstream Where we are zipping file using stream, but it was doing password protected each file instead of the zip file itself?

Here is the sample code

ainfo has filename and byte []

final ZipParameters zipParameters = createZipParameters();
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ZipOutputStream zos = new ZipOutputStream(bos, password.toCharArray());

for (AttachmentInfo ainfo : attachments) {
    zipParameters.setFileNameInZip(ainfo.getName());
    zos.putNextEntry(zipParameters);
    zos.write(ainfo.getAttachment());
    zos.closeEntry();
}

zos.close();
bos.close();

return bos.toByteArray();

How I can just put password on zip file not on each file?

srikanth-lingala commented 2 years ago

Zip encryption does not encrypt the complete zip file. It encrypts each entry that is being added to the zip file and writes to the zip. This is an expected behaviour according to the zip specification.