com.alutam.ziputils
Class ZipEncryptOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.alutam.ziputils.ZipEncryptOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class ZipEncryptOutputStream
extends OutputStream

Output stream that can be used to password-protect zip files.

Example usage:

Creating a password-protected zip file:

  ZipEncryptOutputStream zeos = new ZipEncryptOutputStream(new FileOutputStream(fileName), password);
  ZipOutputStream zos = new ZipOuputStream(zdis);
  ... create zip file using the standard JDK ZipOutputStream in zos variable ...
 

Converting a plain zip file to a password-protected zip file:

  FileInputStream src = new FileInputStream(srcFile);
  ZipEncryptOutputStream dest = new ZipEncryptOutputStream(new FileOutputStream(destFile), password);

  // should wrap with try-catch-finally, do the close in finally
  int b;
  while ((b = src.read()) > -1) {
      dest.write(b);
  }

  src.close();
  dest.close();
 

Author:
Martin Matula (martin at alutam.com)

Constructor Summary
ZipEncryptOutputStream(OutputStream delegate, char[] password)
          Safer version of the constructor.
ZipEncryptOutputStream(OutputStream delegate, String password)
          Convenience constructor taking password as a string.
 
Method Summary
 void close()
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
flush, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ZipEncryptOutputStream

public ZipEncryptOutputStream(OutputStream delegate,
                              String password)
Convenience constructor taking password as a string.

Parameters:
delegate - Output stream to write the password-protected zip to.
password - Password to use for protecting the zip.

ZipEncryptOutputStream

public ZipEncryptOutputStream(OutputStream delegate,
                              char[] password)
Safer version of the constructor. Takes password as a char array that can be nulled right after calling this constructor instead of a string that may stay visible on the heap for the duration of application run time.

Parameters:
delegate - Output stream to write the password-protected zip to.
password - Password to use for protecting the zip.
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException


Copyright © 2012. All Rights Reserved.