gpt4 book ai didi

java - 无法使用java创建文件夹来压缩文件?

转载 作者:行者123 更新时间:2023-12-01 12:54:24 25 4
gpt4 key购买 nike

这里,我在 Books 文件夹内有文件夹(Books)结构,我有名为物理、化学、科学、英语的文件夹。我将 Books 文件夹作为 zipDeleteFile 传递,但所有文件夹内都必须在同一文件夹(Books)中转换为物理.zip,化学.zip,科学.zip,english.zip。但是此代码不起作用。

'

public void foldertToZip(File zipDeleteFile) {
//System.out.println(zipDeleteFile);
File directoryToZip = zipDeleteFile;
List<File> fileList = new ArrayList<File>();
//System.out.println("---Getting references to all files in: " + directoryToZip.getCanonicalPath());
getAllFiles(directoryToZip, fileList);
//System.out.println("---Creating zip file");
writeZipFile(directoryToZip, fileList);
//System.out.println("---Done");
}

public static void getAllFiles(File dir, List<File> fileList) {
try {
File[] files = dir.listFiles();
for (File file : files) {
fileList.add(file);
if (file.isDirectory()) {
System.out.println("directory:" + file.getCanonicalPath());
getAllFiles(file, fileList);
} else {
System.out.println(" file:" + file.getCanonicalPath());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static void writeZipFile(File directoryToZip, List<File> fileList) {
try {
try (FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip")) {
ZipOutputStream zos = new ZipOutputStream(fos);

for (File file : fileList) {
if (!file.isDirectory()) { // we only zip files, not directories
addToZip(directoryToZip, file, zos);
}
}

zos.close();
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException,
IOException {
try (FileInputStream fis = new FileInputStream(file)) {
String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
file.getCanonicalPath().length());
System.out.println("Writing '" + zipFilePath + "' to zip file");
ZipEntry zipEntry = new ZipEntry(zipFilePath);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
}
}`'

最初,我将 zipDeleteFile 作为 C:\Books 传递到 Books 中,我拥有所有物理、英语、科学文件夹,这些文件夹必须在同一根文件夹(Books)中转换为 zip 文件。

最佳答案

因此,基本上,您希望将 Books 文件夹中的每个目录压缩到各自的 zip 文件中。有几种方法可以做到这一点,但最简单的可能是改变调用 foldertToZip

的方式

所以,而不是(类似的)......

foldertToZip(new File("C:\\Books"));

你可以做类似的事情......

for (File file : new File("C:\\Books").listFiles()) {
if (file.isDirectory()) {
foldertToZip(file);
}
}

这将导致 Books 中的每个目录都被添加到其自己的 zip 文件中,该文件将驻留在 Books

您可能需要进行的另一项更改是...

public static void writeZipFile(File directoryToZip, List<File> fileList) {
try {
//try (FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip")) {
File path = directoryToZip.getParentFile();
File zipFile = new File(path, directoryToZip.getName() + ".zip");
try (FileOutputStream fos = new FileOutputStream(zipFile)) {

这将在要压缩的目录的父目录(即 Books 目录)中创建 zip 文件

关于java - 无法使用java创建文件夹来压缩文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24006655/

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