gpt4 book ai didi

java - 如何使用java在具有相同密码的目录中解压缩多个受密码保护的zip文件?

转载 作者:行者123 更新时间:2023-11-30 06:37:21 28 4
gpt4 key购买 nike

我对java很陌生,我正在尝试编写一个java程序来解压缩一个目录中的所有zip文件,所有这些文件都有相同的密码。我编写了一个程序,通过提供密码来解压缩特定的 zip 文件。

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 Extraction() {

try {

ZipFile zipFile = new
ZipFile("C:\\Users\\Desktop\\ZipFile\\myzip.zip");

if (zipFile.isEncrypted()) {

zipFile.setPassword("MYPASS!");
}

List fileHeaderList = zipFile.getFileHeaders();

for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);

zipFile.extractFile(fileHeader, "C:\\Users\\Desktop\\ZipFile");
System.out.println("Extracted");
}

} catch (Exception e) {
System.out.println("Please Try Again");
}

}
public static void main(String[] args) {
new Extraction();

}
}

我还编写了一个代码,用于解压缩文件夹中的所有 zip 文件(不受密码保护)。

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");
}
}

}

我有点困惑如何集成这两个代码来制作一个程序,该程序将所有 zip 文件解压到受密码保护且具有相同密码的目录中。你们中的任何人都可以帮我整合这两个代码吗?我搜索了很多,但没有得到正确的解决方案,所以我发布了这个。我希望我的问题很清楚。

最佳答案

谢谢大家,虽然没有人回答我的问题。我找到了我发布此答案的答案,因为可能还有其他人正在寻找类似的答案。

import java.io.File;
import java.util.List;

import javax.swing.filechooser.FileNameExtensionFilter;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.FileHeader;

public class SamExtraction {

public static void main(String[] args) {

final FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("N/A", "zip");
//Folder where zip file is present
final File file = new File("C:/Users/Desktop/ZipFile");
for (final File child : file.listFiles()) {
try {
ZipFile zipFile = new ZipFile(child);
if (extensionFilter.accept(child)) {
if (zipFile.isEncrypted()) {
//Your ZIP password
zipFile.setPassword("MYPASS!");
}
List fileHeaderList = zipFile.getFileHeaders();

for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
//Path where you want to Extract
zipFile.extractFile(fileHeader, "C:/Users/Desktop/ZipFile");
System.out.println("Extracted");
}
}
} catch (Exception e) {
System.out.println("Please Try Again");
}
}

}
}

关于java - 如何使用java在具有相同密码的目录中解压缩多个受密码保护的zip文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982824/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com