Created
September 19, 2022 09:47
-
-
Save peterkir/64feef5bad0de6d60b035ff8e680b99e to your computer and use it in GitHub Desktop.
EncryptedZipArchiveExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.klib.fileservlet; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.file.DirectoryStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import org.osgi.service.component.annotations.Activate; | |
import org.osgi.service.component.annotations.Component; | |
import net.lingala.zip4j.io.outputstream.ZipOutputStream; | |
import net.lingala.zip4j.model.ZipParameters; | |
import net.lingala.zip4j.model.enums.AesKeyStrength; | |
import net.lingala.zip4j.model.enums.CompressionMethod; | |
import net.lingala.zip4j.model.enums.EncryptionMethod; | |
@Component | |
public class ZipOutputStreamExample { | |
public static final String WRK_DIR = System.getProperty("user.dir"); | |
@Activate | |
public void activate() { | |
try { | |
Path rootPath = Paths.get(WRK_DIR, "data"); | |
List<Path> pathList = Files.find(rootPath, Integer.MAX_VALUE, | |
(filePath, fileAttr) -> fileAttr.isRegularFile()).collect(Collectors.toList()); | |
List<File> fileList = pathList.stream().map(Path::toAbsolutePath).map(Path::toFile) | |
.collect(Collectors.toList()); | |
zipOutputStreamExample(new File("hugo.zip"), rootPath, fileList, "1234".toCharArray(), CompressionMethod.DEFLATE, | |
true, EncryptionMethod.ZIP_STANDARD, AesKeyStrength.KEY_STRENGTH_256); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void zipOutputStreamExample(File outputZipFile, Path rootdirPath, List<File> filesToAdd, char[] password, | |
CompressionMethod compressionMethod, boolean encrypt, EncryptionMethod encryptionMethod, | |
AesKeyStrength aesKeyStrength) throws IOException { | |
ZipParameters zipParameters = buildZipParameters(compressionMethod, encrypt, encryptionMethod, aesKeyStrength); | |
byte[] buff = new byte[4096]; | |
int readLen; | |
try (ZipOutputStream zos = initializeZipOutputStream(outputZipFile, encrypt, password)) { | |
for (File fileToAdd : filesToAdd) { | |
String filenameInArchive = rootdirPath.relativize(fileToAdd.toPath()).toString(); | |
zipParameters.setFileNameInZip(filenameInArchive); | |
zos.putNextEntry(zipParameters); | |
try (InputStream inputStream = new FileInputStream(fileToAdd)) { | |
while ((readLen = inputStream.read(buff)) != -1) { | |
zos.write(buff, 0, readLen); | |
} | |
} | |
zos.closeEntry(); | |
} | |
} | |
} | |
private ZipOutputStream initializeZipOutputStream(File outputZipFile, boolean encrypt, char[] password) | |
throws IOException { | |
FileOutputStream fos = new FileOutputStream(outputZipFile); | |
if (encrypt) { | |
return new ZipOutputStream(fos, password); | |
} | |
return new ZipOutputStream(fos); | |
} | |
private ZipParameters buildZipParameters(CompressionMethod compressionMethod, boolean encrypt, | |
EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength) { | |
ZipParameters zipParameters = new ZipParameters(); | |
zipParameters.setCompressionMethod(compressionMethod); | |
zipParameters.setEncryptionMethod(encryptionMethod); | |
zipParameters.setAesKeyStrength(aesKeyStrength); | |
zipParameters.setEncryptFiles(encrypt); | |
return zipParameters; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment