Ich bin sehr neu in Java und ich versuche ein Java-Programm zu schreiben, um alle Zip-Dateien in einem Verzeichnis zu entpacken, die alle dasselbe Passwort haben. Ich habe ein Programm geschrieben, das eine bestimmte Zip-Datei durch Eingabe des Passworts entpackt.Wie entpacken mehrere Pssword-geschützte Zip-Dateien in einem Verzeichnis, das gleiche Passwort mit Java?
Ich habe auch einen Code geschrieben, der alle Zip-Dateien (die nicht passwortgeschützt sind) in einem Ordner entpackt.
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
//import java.util.zip.ZipFile;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.FileHeader;
public class Extraction {
public static void main(String[] args) {
Extraction unzipper = new Extraction();
unzipper.unzipZipsInDirTo(Paths.get("D:/"), Paths.get("D:/unzipped"));
}
public void unzipZipsInDirTo(Path searchDir, Path unzipTo) {
final PathMatcher matcher = searchDir.getFileSystem().getPathMatcher("glob:**/*.zip");
try (final Stream<Path> stream = Files.list(searchDir)) {
stream.filter(matcher::matches).forEach(zipFile -> unzip(zipFile, unzipTo));
} catch (Exception e) {
System.out.println("Something went wrong, Please try again!!");
}
}
public void unzip(Path zipFile, Path outputPath) {
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile))) {
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
Path newFilePath = outputPath.resolve(entry.getName());
if (entry.isDirectory()) {
Files.createDirectories(newFilePath);
} else {
if (!Files.exists(newFilePath.getParent())) {
Files.createDirectories(newFilePath.getParent());
}
try (OutputStream bos = Files.newOutputStream(outputPath.resolve(newFilePath))) {
byte[] buffer = new byte[Math.toIntExact(entry.getSize())];
int location;
while ((location = zis.read(buffer)) != -1) {
bos.write(buffer, 0, location);
}
}
}
entry = zis.getNextEntry();
}
} catch (Exception e1) {
System.out.println("Please try again");
}
}
}
Ich bin etwas verwirrt, wie diese zwei Codes integrieren ein Programm zu machen, die alle Zip-Dateien in einem Verzeichnis entpackt, die Passwort geschützt sind und das gleiche Passwort hat. Kann jemand von Ihnen mir helfen, diese 2 Codes zu integrieren. Ich habe viel gesucht, aber keine richtige Auflösung bekommen, also poste ich das hier. Ich hoffe meine Frage ist klar.