You can do
this one by using the
sevenzipjbinding.jar
and winzipaes-
20100321.jar
You can down
load the jars by clicking the below link
You can copy the Code and Execute the code Directly by Adding the Jars to Classpath
Service Class:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import de.idyl.crypto.zip.AesZipFileEncrypter;
public class ZipUtil {
public static void zipDirWithPassword( String dirName , String zipFileName , String password)
{
if( zipFileName == null )
{
File tempFile = new File(dirName);
zipFileName = tempFile.getAbsoluteFile().getParent() +File.separator+tempFile.getName()+".zip";
}
zipDir(dirName, zipFileName);
String tempZipFileName = new File( dirName ).getAbsoluteFile() .getParent()+File.separator+"tempencrypted.zip";
try
{
AesZipFileEncrypter enc = new AesZipFileEncrypter (tempZipFileName);
enc.addEncrypted( new File(zipFileName), password);
new File(zipFileName).delete();
new File( tempZipFileName ).renameTo( new File (zipFileName));
}
catch (IOException e)
{
e.printStackTrace();
}
}//method ZipWith Password
public static void zipDir( String dirName , String zipFileName )
{
if( zipFileName == null )
{
File tempFile = new File(dirName);
zipFileName = tempFile.getAbsoluteFile().getParent()+ File.separator+tempFile.getName()+".zip";
}
try
{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName));
compressDir(dirName, zos, new File(dirName).getName()+File.separator);
zos.close();
}
catch( NullPointerException npe )
{
npe.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch( IOException ie )
{
ie.printStackTrace();
}
}//Method zipDir
private static void compressDir(String directory, ZipOutputStream zos, String path) throws IOException {
File zipDir = new File(directory);
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
for (int i = 0; i < dirList.length; i++)
{
File f = new File(zipDir, dirList[i]);
if (f.isDirectory())
{
String filePath = f.getPath();
compressDir(filePath, zos, path + f.getName() + "/");
continue;
}
FileInputStream fis = new FileInputStream(f);
try
{
ZipEntry anEntry = new ZipEntry(path + f.getName());
zos.putNextEntry(anEntry);
bytesIn = fis.read(readBuffer);
while (bytesIn != -1)
{
zos.write(readBuffer, 0, bytesIn);
bytesIn = fis.read(readBuffer);
}
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
fis.close();
}
}
}// Method CompressDir
}//Class
Main Class:
public class MainClass
{
public static void main(String[] args)
{
ZipUtil.zipDirWithPassword("F:/ZipaFolderUsingPassword/ZipaDirectoryUsingPassword/inputFiles","F:/ZipaFolderUsingPassword/ZipaDirectoryUsingPassword/ZipFolder/zipfile.zip", "abcd1234");
}//main method
}//class