0

Is there a way to use ZipOutputStream (from java.util.zip) with a password?

I'm working on copying files from a (unencrypted) zip file into another encrypted zipfile. Using ZipInpuStream/ZipOutputStream it is possible to read from the original zip file into the target zip file but I haven't seen a recipe that talks about how to use password on the ZipOutputStream.

        ZipInputStream zis = new ZipInputStream(new FileInputStream(inputFile));
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile));
        byte[] buff = new byte[1024];
        while (true) {
          int bytesRead = zis.read(buff, 0, 1024);
          if (bytesRead < 0) {
            // finished
            break;
          }
          zos.write(buff, 0, bytesRead);
        }
        zos.flush();
        zos.close();
        zis.close();
Naman
  • 27,789
  • 26
  • 218
  • 353
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • [password-protected-zip-file-in-java](https://stackoverflow.com/questions/10587561/password-protected-zip-file-in-java) – Eritrean Jun 17 '20 at 23:06

1 Answers1

0

I'm afraid it's difficult to set a password for Java.util.zip, Try to use zip4j https://github.com/srikanth-lingala/zip4j

jack
  • 11
  • 1