I'm using this method to zip and decrypt a file:
AesZipFileEncrypter.zipAndEncrypt
This code:
AesZipFileEncrypter.zipAndEncrypt(new File("C:\Test\Folder\MyFile.txt"), new File("C:\Test\Folder\MyZip.zip"), password, aesEncrypter);
compresses also the folder tree of my file, not just the file. For example:
Adding C:\Test\Folder\MyFile.txt in the created zip file I will find the folders C:\Test\Folder\MyFile.txt also if I would like to have just MyFile.txt in the root folder.
Is it possibile?
This is the solution:
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(password, 0);
AesZipFileEncrypter ze=new AesZipFileEncrypter(outputfilename, aesEncrypter);
ze.add(inputfilename,new FileInputStream(inputfilename), password);
ze.close();
In Windows (which it looks like you are in) I ran into the same problem and it seems to depend on where the file is relative to your application. To get around it, I copied the input file to the local directory, ziped and encrypted it, then moved the output file back to the where the output file was intended.
public static File aesEncrypt(String inFileFullPath, String outFileFullPath, String aesPassword) throws IOException{
File inFile = new File(inFileFullPath);
File localInput = new File(inFile.getName());
Files.copy(inFile, localInput);
File outFile = new File(outFileFullPath);
File localOutFile = new File(outFile.getName());
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(aesPassword, 255);
AesZipFileEncrypter ze = new AesZipFileEncrypter(localOutFile, aesEncrypter);
ze.add(localInput, aesPassword);
ze.close();
Files.move(localOutFile, outFile);
localInput.delete();
return outFile;
}