Java
Chilkat2 Java
Encrypt Already Existing Zip
See more Zip Examples
To encrypt an already existing non-encrypted .zip, the application must open the .zip, set the encryption related properties, and then re-write.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Zip;
public class ChilkatExample {
public static void main(String[] args) {
// This requires the Chilkat Zip API to have been previously unlocked.
// See Unlock Chilkat Zip for sample code.
Zip zip = new Zip();
// Open an unencrypted .zip
try {
zip.openZip("qa_data/zips/test.zip");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Unzip to a temp directory.
int numFilesUnzipped = zip.unzip("qa_output/tmp");
if (numFilesUnzipped < 0) {
System.out.println(zip.getLastErrorText());
return;
}
// Clear the zip object.
zip.newZip("qa_output/aesTest.zip");
// Indicate that 128-bit AES encryption is to be used when writing the .zip
zip.setEncryption(4);
zip.setEncryptKeyLength(128);
// Set the password.
zip.setEncryptPassword("secret");
// Append the files.
zip.setAppendFromDir("qa_output/tmp");
try {
zip.appendFiles("*.*", true);
// Write the .zip and close it.
zip.writeZipAndClose();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Success.");
}
}